Get/set method for an array?
C#
4
Posts
3
Posters
1
Views
1
Watching
-
What exactly are you trying to do?
-
What exactly are you trying to do?
Well, I have a class Foo(). Inside Foo is a private array of another class, Bar(). public Foo() { private Bar[] myBars = new Bar[10]; } When I instantiate Foo, I need to be able to assign a value to the array of Bars: myFoo.myBars[n].someValue = 10; So how can I do this?
-
Such a property is called an indexer. You can read a tutorial on them in the MSDN documentation (under "indexers->tutorial (C#)")The following is an example...
public class MyArray {
... public Object this\[int index\] { get { return \_Items\[index\]; } set { \_Items\[index\] = value; } } private Object\[\] \_Items;
}