C# 控制对数组属性元素的访问

发布于 2024-12-09 14:44:50 字数 2289 浏览 0 评论 0原文

我想使用属性来访问内部数组,但控制对数组元素的访问。

我写了一个简单的例子,可以比我自己更好地解释我的问题。

在示例中,我提供了“失败”类和“受控”类。第二个按照我的意愿运行,但方法有点不同,它仅对一个数组有用。

我的问题是下一个: 如果我必须有两个不同的数组并因此有两个不同的属性怎么办?

怎么办呢?

谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("The Fail Class:");

            MyFailClass MyFailTestClass = new MyFailClass(5);
            MyFailTestClass.MyList[2] = 11;
            if (MyFailTestClass.Modified) {
                Console.WriteLine("Right");
            } else {
                Console.WriteLine("Error");
            }



            Console.WriteLine("The Controlled Class:");

            MyControlledClass MyControlledTestClass = new MyControlledClass(5);
            MyControlledTestClass[2] = 11;
            if (MyControlledTestClass.Modified) {
                Console.WriteLine("Right");
            } else {
                Console.WriteLine("Error");
            }

            Console.ReadKey();
        }
    }

    public class MyFailClass
    {
        // Property 
        public byte[] MyList
        {
            get
            {
                return myList;
            }
            set  // <--------- Never enters here if I set a concrete array element
            {
                Modified = !myList.Equals(value);
                myList = value;
            }
        }
        public bool Modified { get; set; }

        // Constructor
        public MyFailClass(int elements)
        {
            myList = new byte[elements];
        }

        private byte[] myList;
    }

    public class MyControlledClass
    {
        // Property 
        public byte this[int index]
        {
            get
            {
                return myList[index];
            }
            set
            {
                Modified = !myList[index].Equals(value);
                myList[index] = value;
            }
        }
        public bool Modified { get; set; }

        // Constructor
        public MyControlledClass(int elements)
        {
            myList = new byte[elements];
        }

        private byte[] myList;
    }
}

I would like to bring access to an internal array with a Property, but controlling the access to array elements.

I have write a simple example that can explain my problem better than myself.

In the example, I provide a 'Fail' class and a 'Controlled' class. The second one runs as I would like, but the approach is a bit different and it is usefull only with one array.

My question is the next:
What about if I must to have two different arrays and therefore two differenct properties.

How to do it ?

Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("The Fail Class:");

            MyFailClass MyFailTestClass = new MyFailClass(5);
            MyFailTestClass.MyList[2] = 11;
            if (MyFailTestClass.Modified) {
                Console.WriteLine("Right");
            } else {
                Console.WriteLine("Error");
            }



            Console.WriteLine("The Controlled Class:");

            MyControlledClass MyControlledTestClass = new MyControlledClass(5);
            MyControlledTestClass[2] = 11;
            if (MyControlledTestClass.Modified) {
                Console.WriteLine("Right");
            } else {
                Console.WriteLine("Error");
            }

            Console.ReadKey();
        }
    }

    public class MyFailClass
    {
        // Property 
        public byte[] MyList
        {
            get
            {
                return myList;
            }
            set  // <--------- Never enters here if I set a concrete array element
            {
                Modified = !myList.Equals(value);
                myList = value;
            }
        }
        public bool Modified { get; set; }

        // Constructor
        public MyFailClass(int elements)
        {
            myList = new byte[elements];
        }

        private byte[] myList;
    }

    public class MyControlledClass
    {
        // Property 
        public byte this[int index]
        {
            get
            {
                return myList[index];
            }
            set
            {
                Modified = !myList[index].Equals(value);
                myList[index] = value;
            }
        }
        public bool Modified { get; set; }

        // Constructor
        public MyControlledClass(int elements)
        {
            myList = new byte[elements];
        }

        private byte[] myList;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢望 2024-12-16 14:44:50

您可以考虑将数组属性替换为 ObservableCollection

您可能会将该属性公开为 IList,因为它是可观察的这一事实是内部实现细节。

public class MyClass
{
    public IList<byte> MyList
    {
        get { return _myList; }
    }
    private IList<byte> _myList = new ObservableCollection<byte>();

    ...
}

MyClass 的实现应处理 _myListPropertyChangedCollectionChanged 事件。

请注意,通常不需要集合属性的设置器 - 如果调用者想要替换他可以调用的列表:

myClass.MyList.Clear();

然后添加新元素。

You might consider replacing your array property by, say, an ObservableCollection<T>.

You would probably expose the property as IList<T> since the fact that it's observable is an internal implementation detail.

public class MyClass
{
    public IList<byte> MyList
    {
        get { return _myList; }
    }
    private IList<byte> _myList = new ObservableCollection<byte>();

    ...
}

The implementation of MyClass should handle _myList's PropertyChanged and CollectionChanged events.

Note you don't generally need a setter for a collection property - if the caller wants to replace the list he can call:

myClass.MyList.Clear();

then add new elements.

泅渡 2024-12-16 14:44:50

如果您想要两个数组,那么按理说您以后可能需要三个或四个(我不确定,但事情似乎就是这样)。

在这种情况下,我会考虑创建一个类,它是“MyControlledClass”的聚合

public class IndexedClass
{
    // Property 
    public byte this[int index]
    {
        get
        {
            return myList[index];
        }
        set
        {
            Modified = !myList[index].Equals(value);
            myList[index] = value;
        }
    }

}

public class IndexedClassGroup
{
    // Property 
    public IndexedClass this[int index]
    {
        get
        {
            return myList[index];
        }
        set
        {
            Modified = !myList[index].Equals(value);
            myList[index] = value;
        }
    }

}

,然后,您可以像二维数组一样访问这些内容。

就我个人而言,从理论上讲,我对将数组公开为可获取/可设置的概念有点怀疑,所以我不太了解这样做的来龙去脉。类是否使用数组或列表或其他看起来像是私有实现细节而不是公共属性的东西。如果您要公开某些内容,请公开 ICollection<> 。或 IEnumerable<>并在内部将其解析为数组。无论如何,我的两分钱。

If you want two arrays, it stands to reason that you might later want three, or four (I don't know for sure, but that seems to be how things go).

In this case, I would consider making a class that's an aggregate of your "MyControlledClass"

public class IndexedClass
{
    // Property 
    public byte this[int index]
    {
        get
        {
            return myList[index];
        }
        set
        {
            Modified = !myList[index].Equals(value);
            myList[index] = value;
        }
    }

}

public class IndexedClassGroup
{
    // Property 
    public IndexedClass this[int index]
    {
        get
        {
            return myList[index];
        }
        set
        {
            Modified = !myList[index].Equals(value);
            myList[index] = value;
        }
    }

}

Then, you could access these things like a two dimensional array.

Personally, I'm a little leery of exposing an array as a gettable/settable concept, in theory, so I don't know a whole lot about the ins and outs of doing that. Whether a classes uses an array or a list or whatever seems like a private implementation detail rather than a public property. If you're going to expose something, expose an ICollection<> or IEnumerable<> and resolve it internally to an array. My two cents, anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文