0

How can I set/get the value of an object in an object array?

Currently I get: "object does not contain a definition for 'value' and no extension method"

Example C#;

    public class myObjClass
    {
        public int value = 5;
    }

    public class myObjClass2
    {
        public float[] pos = new float[2];
    }

    public void test()
    {
        myObjClass myObj = new myObjClass();
        myObjClass2 myObj2 = new myObjClass2();

        object[] objArr = new object[2];
        objArr[0] = myObj;
        objArr[1] = myObj2;
        Debug.Print(myObj.value.ToString());
        Debug.Print(objArr[0].value.ToString()); // how?

    }
6
  • object does not contain a definition for 'value', myObjClass does. You need to cast. Or make it an myObjClass[] objArr. Class names should start with an Uppercase, by the way. Commented Sep 5, 2017 at 9:55
  • 1
    why are you using object array? Commented Sep 5, 2017 at 9:56
  • 1
    Don't use object it is creating the issue. Use a real class type. Use following : myObjClass[] objArr = new myObjClass[2]; Commented Sep 5, 2017 at 9:58
  • As far as the compiler knows, objArr[0] contains a plain object, not a myObjClass. Commented Sep 5, 2017 at 9:59
  • 1
    Then see the answer by EpicKip, in particular with the is and as operators. Commented Sep 5, 2017 at 10:01

5 Answers 5

4

Its because a generic object does not have the property value your class myObjClass has. To fix this you could cast the item to your class like so:

((myObjClass)objArr[0]).value.ToString()

Only do this ^ if you are sure of the type


Instead you could also check it first:

With as:

var item = objArr[0] as myObjClass;
if( item != null ) // Item will be null if its not a 'myObjClass'
{
    //Do stuff with item
}

Or with is:

if( objArr[0] is myObjClass )
{
    var item = (myObjClass)objArr[0];
    //Do stuff with item
}
Sign up to request clarification or add additional context in comments.

Comments

2

When using an object array you have to cast to the real type (here: myObjClass) before accessing the fields:

You can access the object like this

((myObjClass)objArr[0]).value

but I would not recommend. Can´t you have your array to be the concrete type

var array = new myObjClass[42]

A compact safe alternative to retrieve the value is

(objArr[0] as myObjClass)?.value

Comments

1

You need to cast object to known type which is myObjClass, like:

((myObjClass)objArr[0]).value.ToString();

Or you can use reflection

var valueString = objArr[0].GetType().GetProperty("value").GetValue(objArr[0]);
Debug.Print(valueString.ToString()); 

Hope helps,

Comments

1

Technically you can put it as

  Debug.Print((objArr[0] as myObjClass)?.value.ToString()); 

We try casting objArr[0] as myObjClass and if succeed get value and turn it to string. If objArr[0] is not myObjClass we return null as a string

However, a much better way is to implement ToString() in both classes of interest:

public class myObjClass
{
    public int value = 5;

    public override string ToString() {
      // When debugging we want to know "value"
      return value.ToString();
    }
}

public class myObjClass2
{
    public float[] pos = new float[2];

    public override string ToString() {
      // When debugging we want to know "pos[0]" and "pos[1]" values
      return $"{pos[0]} : {pos[1]}";
    }
}

And then put an easy

// Just print out debug info (we don't want to know the actual objArr[0] class)
Debug.Print(objArr[0].ToString());

Comments

0

You have a single object, that indeed is an instance of myObjClass, and has a value field, but you have two references to it.

One (myObj) is known to the compiler to be of type myObjClass, and it can guarantee that it has a value field.

The other (objArr[0]) is only known to the compiler to be of type object, and it cannot guarantee that it has a value field.

For example, you could do:

 objArr[0] = (random.Next() > 0.5) : myObj ? myObj2

where we're gonna decide at runtime, based on the value of a random number, which will be the type of the actual object at objArr[0].

So, if this was allowed, half of the time objArr[0].value would be correct, and half of the time it will be an error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.