user291660
user291660

Reputation: 49

Convert from VB.NET to C#

In C# you can not have indexed properties. That said, how do I convert the following code from VB.net to 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

Upvotes: 0

Views: 409

Answers (4)

Adam Lear
Adam Lear

Reputation: 38778

Or with methods instead of properties:

double[] _patchSpectrum = new double[49];

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

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

Upvotes: 1

Dave Swersky
Dave Swersky

Reputation: 34810

For future code conversions check out the Telerik Code Converter.

Upvotes: -1

dsolimano
dsolimano

Reputation: 9006

You can define an indexer on your object, which is how collection classes like List work. E.g:

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;
        }
    }

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564791

You'd do this like:

private double[] _PatchSpectrum = new double[49]

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

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

Upvotes: 9

Related Questions