开发者

C# class arrays and indexers, do multiple arrays in a class have to be public?

开发者 https://www.devze.com 2023-03-08 01:21 出处:网络
I have a class that\'s going to hold 3 parallel arrays. (For a class assignment we\'re basically coding a rudimentary xml parser...beginning programming class)

I have a class that's going to hold 3 parallel arrays. (For a class assignment we're basically coding a rudimentary xml parser...beginning programming class)

*Note, I'm doing this in very basic OOP. I've got an XMLObject class which has the arrays, and holds the xml elements in one array, the data values in another, and the ending elements in the third. I've also got an XMLParse object that does the actual parsing, and stores the strings to their various arrays as it finds them. I've been forbiddin from using .net's xml stuff for this assignment, has to be a byte by byte read in.

Now I was reading on MSDN about indexers, and as I understand it, I can either only have one array using an indexer(since that's the only way for properties to receive parameters), or I have to make my arrays public so that the parse class can add to them, and main or another class can read from them.

Do I have that right or am I missing something/not understanding how to get and set arrays of one class from another?

Does the开发者_JAVA技巧 same go for list as well?


If I understand your question you want to have many indexers on your class which will return the various elements from the arrays that the class holds.

you can have many indexers, but only if the type used by each indexer is different. So you could have an indexer by int and an indexer by string, but not 2 indexers by int.

From the sounds of things you won't be able to use indexers to access all the values your class holds, as they will probably all want to use int.

Publicly exposing the arrays is one option, but you could also provide different methods for reading each thing, so you could have GetXmlElement(int index), GetDataValue(int index) and GetEndingElement(int index) to provide access to the contents of the arrays.

Another option would be to store the data in arrays internally, but accept and return a class which bundled up all the data together. This way you could have a single indexer which returned all the data, as all the data would be a single object with the element, data value and end element in it.

you would have to provide similar methods for adding data and potentially removing and changing as well. Whether you want to do this, opr just exopse the underlying arrays/lists depends on if you want to be able to exercise control over the adding/accessing/deleting/changing of the arrays or not.


I suggest you use List<T> instead of array for storing the data. It is much easier to work with.

See here.

0

精彩评论

暂无评论...
验证码 换一张
取 消