Object array question

rog_rickert

Member
Joined
Dec 17, 2020
Messages
11
Programming Experience
Beginner
hello,

I'm still pretty new to C# and teaching myself. I've searched around and haven't found an answer, so I'm wondering if anyone can let me know if this is possible.

I need to have multiple instances of an object that can be accessed by multiple forms. I know I can do this with static class or a singleton. What I need to know is if it possible to have an array of these objects so that I can reference them by index.

something like this:
(pseudocode)

public class Thing {

public int var1;
public int var2;

}

then create using

Thing[ ] thing = new Thing[4];


and reference them like:


thing[0].var1 = something;
thing[0].var2 = something;


thing[1].var1 = something;
thing[1].var2 = something;

etc.


any info to point me in the right direction is appreciated.
 
Firstly, there's no such thing as "multiple instances of an object". You can have multiple instances of a type and each instance is an object. Think about how objects in the real world work. That's how programming objects work too, because OOP is based on real-world object behaviour. In the real world "table" is a type of thing, right? "Table" is basically a set of attributes that describes something that is a table. If you go to a furniture store and buy a table and bring it home, you now have a table object. You can't have multiple instances of that object. It's one object. You can get other tables, so other objects of the same type that may or may not have all the same attributes, but they are all distinct objects. If you always think about how objects work in the real world while programming in an OO language, things will likely go smoother. There are some exceptions but, generally, programming objects will work the same way.
 
As for your question, of course. An array is an object like any other. If you can put an object somewhere then you can put an array there because an array is an object.

When dealing with arrays, I like to use egg cartons as an analogy. Just like having an egg carton provides multiple places where you can put an egg but it doesn't mean that you have any eggs, so having an array provides places where you can place other objects but it doesn't mean that you have those objects. If you create a Thing array with four elements then you have places to store four Thing objects but you still have to create those objects and place them there. Think of each array element as a variable. Just as a variable is null by default and requires you to create an object and assign it to the variable, so each array element is null by default and requires you to create an object and assign it to the element.

Unless you know for a fact that you will always have exactly four objects to store in the array, you probably ought to use a List<Thing> instead, which is basically dynamic array. It works just like an array in many ways, allowing you to index it to get or set an item. Unlike an array though, it will grow and shrink automatically as you call Add and Remove.
 

Latest posts

Back
Top Bottom