Pregunta

En C # no se puede tener propiedades indexados. Dicho esto, ¿cómo puedo convertir el código siguiente desde VB.net a C #?

Private _PatchSpectrum(49) As Double

Public ReadOnly  Property GetPatchSpectrum() As Double()
    Get
        Return _PatchSpectrum
    End Get
End Property

Public WriteOnly Default Property PatchSpectrum(idx As Integer) As Double
    Set(ByVal value as Double)
        _PatchSpectrum(idx) = value
    End Set
End Property
¿Fue útil?

Solución

Usted haría esto como:

private double[] _PatchSpectrum = new double[49]

public double[] GetPatchSpectrum
{
    get { return _PatchSpectrum; }
}

public double this[int index]
{
    set { this._PatchSpectrum[index] = value; }
}

Otros consejos

O con los métodos en lugar de propiedades:

double[] _patchSpectrum = new double[49];

public void SetPatchSpectrum(int index, double value) 
{
    _patchSpectrum[index] = value;
}

public double[] GetPatchSpectrum() 
{
    return _patchSpectrum;
}

Puede definir un indexador en su objeto, que es la forma de recolección de clases como el trabajo List. Por ejemplo:

public double this[int i]
    {
        get
        {
            // This indexer is very simple, and just returns or sets
            // the corresponding element from the internal array.
            return _PatchSpectrum[i];
        }
        set
        {
            _PatchSpectrum[i] = value;
        }
    }

Para las futuras conversiones de código echa un vistazo a la Telerik Convertidor de código .

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top