获取/设置数组属性的元素

时间:2017-01-30 11:16:39

标签: c#

我有一个用于定义/保存矩形的类。

我有2个包含4个点的数组。一个数组用于保存在文件中的点,另一个用于实际显示的点(可以由用户修改)。

我想为保存点的数组中的元素创建一个get / set。当我保存一个点时,它会自动复制到第二个数组中。

我不确定是否非常清楚,有我想要创建的代码:

public class MyRectangle
{
    public PointF[] TemporaryPoints = new PointF[4];

    protected PointF[] _savedPoints = new PointF[4];
    protected PointF[] SavedPoints;

    //How to do something like this ?
    protected PointF SavedPoints[int i]
    {
        get
        {
            return _savedPoints[i];
        }
        set
        {
            _savedPoints[i] = value;
            TemporaryPoints[i] = value;
        }
    }
}

像这样使用:

SavedPoints[0] = new PointF(1,1);     // SavedPoints = [1;1] TemporaryPoints = [1;1]
TemporaryPoints[0] = new PointF(4,4); // SavedPoints = [1;1] TemporaryPoints = [4;4]
SavedPoints[0] = TemporaryPoints[0];  // SavedPoints = [4;4] TemporaryPoints = [4;4]

3 个答案:

答案 0 :(得分:1)

C#中无法使用命名索引器。您可以使用索引器语法:

public class MyRectangle
{
    public PointF[] TemporaryPoints = new PointF[4];

    protected PointF[] _savedPoints = new PointF[4];

    protected PointF this[int i]
    {
        get
        {
            return _savedPoints[i];
        }
        set
        {
            _savedPoints[i] = value;
            TemporaryPoints[i] = value;
        }
    }
}

这不会为您提供访问名为SavedPoints的索引属性的选项。如果要这样做,则必须创建一个封装此功能的自定义集合SavedPointsCollection。然后,您可以定义该类型的SavedPoints属性。如有必要,您还可以创建TemporaryPintsCollection访问的SavedPointsCollection

要实现自定义集合,您可以覆盖System.Collections.ObjectModel.Collection<T>或实现IList<T>接口。当您想要为项类型使用标准集合时,Collection<T>很好。当您想要制作更多精美的功能时,可以使用IList<T>作为固定大小的集合。

以下是一个(不完整的)IList<T>实现的示例:

public class SavedPointsCollection : IList<PointF> {
    protected PointF[] _values = new PointF[4];

    public PointF this[int index] {
        get
        {
            return _values[index];
        }
        set
        {
            _values[index] = value;
            //Set temporary points here
        }
    }

    //Rest of the IList implementation
}


public class MyRectangle
{
    private SavedPointsCollection _savedPoints = new SavedPointsCollection();

    protected SavedPointsCollection SavedPoints
    {
        get {
            return _savedPoints;
        }
    }
}

答案 1 :(得分:0)

不是很清楚你想要什么,但你可以使用索引器:

protected PointF this[int index]    // Indexer declaration  
{  
     get
    {
        return _savedPoints[index];
    }
    set
    {
        _savedPoints[index] = value;
        TemporaryPoints[index] = value;
    } 
}  

然后你可以这样做:

 var rect = new MyRectangle();
rect[0] = new PointF (1,1);

答案 2 :(得分:0)

如果您想要执行此类操作,可以覆盖TemporaryPoints索引器属性,但应创建属性public field,而不是将其设为public class MyRectangle { private PointF[] _temporaryPoints = new PointF[4]; protected PointF[] _savedPoints = new PointF[4]; // get TemporaryPoint by it's index public PointF TemporaryPoint(int idx) { return (idx < _temporaryPoints.Length ? _temporaryPoints[idx] : default(PointF)); } // set TemporaryPoint value at index idx public void SetTemporaryPoint(int idx, PointF value) { if(idx < _temporaryPoints.Length) _temporaryPoints[idx] = value; } // get SavedPoint by it's index public PointF SavedPoint(int idx) { return (idx < _savedPoints.Length ? _savedPoints[idx] : default(PointF)); } // set SavedPoint value at index idx public void SetSavedPoint(int idx, PointF value) { if(idx < _savedPoints.Length) _savedPoints[idx] = value; } //How to do something like this ? protected PointF this[int idx] { get { return SavedPoint(idx); } set { SetSavedPoint(i, value); SetTemporaryPoint(i, value); } } } 。你可以结合起来创建这样的东西:

SetTemporaryPoint(int idx, PointF value)

由于您无法覆盖&#34;属性的索引器属性&#34;你应该创建允许你只设置特定索引值的接口。 这就是为什么有这种方法的原因:SetSavedPoint(0, new PointF(1,1)); // SavedPoints = [1;1] TemporaryPoints = [1;1] SetTemporaryPoint(0, new PointF(4,4)); // SavedPoints = [1;1] TemporaryPoints = [4;4] SetSavedPoint(0, TemporaryPoint(0)); // SavedPoints = [4;4] TemporaryPoints = [4;4] 。 毕竟你可以按照你想要的几乎一样使用它:

this[]

但是,如果你真的只使用public enum POINTS_STORAGE { TEMPORARY = 0, SAVED = 1 }; 索引器属性。你可以进行一些枚举:

this[]

然后在你public PointF this[POINTS_STORAGE s, int idx] { get { if(s == POINTS_STORAGE.TEMPORARY) return _temporaryPoints[idx]; else return _savedPoints[idx]; } set { if(s == POINTS_STORAGE.TEMPORARY) _temporaryPoints[idx] = value; else _savedPoints[idx] = value; } } 索引器属性中:

meRectangleReference[POINTS_STORAGE.SAVED, 0] = new PointF(1, 1);     // SavedPoints = [1;1] TemporaryPoints = [1;1]
meRectangleReference[POINTS_STORAGE.TEMPORARY, 0] = new PointF(4,4); // SavedPoints = [1;1] TemporaryPoints = [4;4]
meRectangleReference[POINTS_STORAGE.SAVED, 0] =  meRectangleReference[POINTS_STORAGE.TEMPORARY, 0];  // SavedPoints = [4;4] TemporaryPoints = [4;4]

然后你就可以使用它:

{{1}}