Frans
Frans

Reputation: 23

Access object property in array

class Program{

    static void Main(string[] args){

        object[] array = new object[1];

        CreateItem item = new CreateItem();
        item.name = "Necklace";
        item.value = 5;
        array[0] = item;

        Console.WriteLine(array[0].name); //This part of the code doesn't work. 
                                          //It can't find the property name. 
        Console.ReadLine();
    }
}

public class CreateItem {
    public string name;
    public int value;
}

Hi there! First of all I'd like to say that I'm not very familiar with objects, so excuse any mistakes you can see in the code (Although feel free to correct them, it'd be a great way to learn).

I've been working on making a small game using C#, but I came across a problem: I can't access my object properties when I put them in an array. Does anyone know which code I should use to be able to access my object properties while they're in an array?

Thanks for reading, and once again, excuse any silly mistakes I made, I'm fairly new to working with objects.

Upvotes: 2

Views: 6224

Answers (4)

Bojan Komazec
Bojan Komazec

Reputation: 9526

Line

object[] array = new object[1];

creates an array of elements of type Object which is the base class for all other classes in .NET.

When you do:

array[n] = item;

an implicit conversion to the base type occurs and through array[n] you can access only members of the Object type portion of the CreateItem object (like ToString() or GetType() - their overrides will be called).

If you want to access entire CreateItem object, you have to cast the reference to the base type back to the original type, by using cast operator for example:

var name = ((CreateItem)array[0]).name;

This explicit casting is error-prone, has a run-time overhead and it is a sign of the poor design. When you know the type of the collection in advance, declare the collection of that type as other answers are suggesting:

// you can use array if you know number of items in advance and that number of elements will not change
CreateItem[] array = new CreateItem[N];

// use list if number of elements might change
List<CreateItem> list = new List<CreateItem>();

Upvotes: 0

eol
eol

Reputation: 24555

You could cast the object to your type, i.e.: Console.WriteLine(((CreateItem)array[0]).name);

or (more effectively)

define your array as CreateItem[] array = new CreateItem[1];

Upvotes: 1

T McKeown
T McKeown

Reputation: 12847

You should probably look at using Generics and Lists, it is a very common and a valuable concept to grasp, as is the concept of Boxing and Unboxing which Generics solves.

class Program{

  static void Main(string[] args){

    List<CreateItem> list  = new List<CreateItem>();

    CreateItem item = new CreateItem();
    item.name = "Necklace";
    item.value = 5;
    list.Add( item );

    Console.WriteLine(list[0].name); //This part of the code doesn't work. 
                                      //It can't find the property name. 
    Console.ReadLine();
  }


}

Upvotes: 2

David L
David L

Reputation: 33815

You shouldn't use an object array when you have a strong type that you're interested in using (and you know the type already).

CreateItem[] array = new CreateItem[1];

CreateItem item = new CreateItem();
item.name = "Necklace";
item.value = 5;
array[0] = item;

Console.WriteLine(array[0].name);

Necklace will now be outputted as expected.

Upvotes: 6

Related Questions