0

I have a function which will return me an object array with different types in each element. This is a small reproducer of what i am trying to achieve.

public static object[] asset()
    {
        object[] tab = new object[11];
        tab[0] = bool;
        tab[1] = int;
        tab[2] = int;
        tab[3] = int;
        tab[4] = int;
        tab[5] = int;
        tab[6] = string;
        tab[7] = double;
        tab[8] = float;
        tab[9] = int;
        tab[10] = ""; //string
        return tab;
    }
    object[] testobject = new object[11];
    testobject = asset();
    If (testobject[0] = true)
    {

    }

So i am trying to performe a bool check to the object[0] which is a bool. Obviously it doesn't work like this. I tried converting object[0] to boolean and then test it which is what i do for integers or doubles but it doesn't work neither.

Any ideas?

5
  • 2
    If (testobject[0] = true), use lower case if and use == to compare, not =. Commented Jul 4, 2014 at 16:27
  • having an array of object with different types is really bad practise. you should rather declare a variable for each value. Commented Jul 4, 2014 at 16:57
  • Why do you need an array of objects of different types instead of defining a specific class that contains a property for each object of the appropriate type? Commented Jul 4, 2014 at 17:07
  • I have a lot of array objects with different values. I think i save a lot of coding using arrays of objects. And besides that i can change the objects value to whatever i want whenever i want. That is why i bear with array objects Commented Jul 4, 2014 at 17:31
  • No, you don't want to use an array to hold a bunch of different types. You're missing the point of object-oriented programming. Create a class that contains the properties you want to have available, which gives you type-safety, intellisense, and a whole slew of advantages over an array of object. Commented Jul 4, 2014 at 19:06

2 Answers 2

2

I think you want to cast before checking, although it is hard to say as you have some compile errors in your code.

if ((bool)testobject[0] == true)
{

}

Notice the use of == instead of = (which is an assignment).

You can even shorten this to:

if ((bool)testobject[0])
{

}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried Convert.ToBoolean() but it threw me an exception that it was not possible to convert it to boolean. Now it works. Dont know why... Thank you everyone for the help
1

object[0] is bool will produce true if it is a bool and false if not.

if (object[0] is bool) 
{
    bool myBool = (bool)object[0];
    // do something with myBool
}

If you use:

bool? myBool = object[0] as bool?;

then myBool will be null if the value was not a bool and it will have a boolean value if it was.

bool? myBool = object[0] as bool?;
if (myBool != null)
{
    // do something with myBool
}

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.