سؤال

في C # لا يمكنك أن تحتوي على خصائص مفهرسة. ومع ذلك، كيف يمكنني تحويل التعليمات البرمجية التالية من VB.NET إلى 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
هل كانت مفيدة؟

المحلول

كنت تفعل هذا مثل:

private double[] _PatchSpectrum = new double[49]

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

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

نصائح أخرى

أو مع الأساليب بدلا من الخصائص:

double[] _patchSpectrum = new double[49];

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

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

يمكنك تحديد فهرس على الكائن الخاص بك، وهذا هو كيف مثل مجموعات جمع List الشغل. على سبيل المثال:

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

للحصول على تحويلات رمز المستقبل تحقق من Telerik Code Converter..

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top