schoko Bong
schoko Bong

Reputation: 103

How to return multiple variables in propertys

I want to return multiple variables in a property

public class Cars
{
    public object value { get; set; }
    public bool worked { get; set; }
}

public Cars GetCar(object value, Cars c)
{
   c.value = value;
   return c;
}

public void Main(string[] args)
{
   Cars c = new Cars();
   string mycar = "ABC-12";
   int mycar2 = 123;

   if (GetCar(mycar, c).value == "ABC") // working
       GetCar(mycar, c).worked = false;

   if (GetCar(mycar, c).value == 1) // error
       GetCar(mycar, c).worked = false;
}

So that if the object is a string that works but if I use that int I get that exception

The == operator can not be applied to "object" and "int" operands

Note: I don't want use Convert.to every time

Upvotes: 3

Views: 105

Answers (6)

Legion
Legion

Reputation: 791

you ca try to edit the test like that:

object tmp = GetCar(mycar2,2).value;
if ((tmp as int?) != null && (int)tmp == 1)
            GetCar(mycar2,2).worked = false;

Now you do the cast only if your items is effectively an Integer.

If you like do it in a single line of code:

int? temp2 = 0;
if ((temp2 = (GetCar(mycar2,2).value as int?)).HasValue && temp2 == 1)
    GetCar(mycar2,2).worked = false;

L-

Upvotes: 0

Matěj Štágl
Matěj Štágl

Reputation: 1037

I guess you are just missing a cast. On the other hand I must question your code - what are you trying to achieve? The things you are doing make a very little sense to me.

    public class Cars
    {
        public object value { get; set; }
        public bool worked { get; set; }
    }

    public Cars GetCar(object value)
    {
        Cars c = new Cars();
        c.value = value;
        return c;
    }

    public void Main(string[] args)
    {
        string mycar = "ABC-12";
        int mycar2 = 123;

        if ((string)GetCar(mycar).value == "ABC")
            GetCar(mycar).worked = false;

        if ((int)GetCar(mycar2).value == 1) // changed mycar to mycar2 and 
                                            // added a cast to int
            GetCar(mycar2).worked = false;
    }

Upvotes: 0

sandesh lande
sandesh lande

Reputation: 1

  • Working because Class Cars is reference type i.e. object and String also reference type i.e. object
  • Not Working because Class Cars is reference type i.e. object and Int is value type i.e. object

That's why

    string mycar = "ABC-12";
    int mycar2 = 123;

    if (GetCar(mycar).value == "ABC") // working because reference type==reference type
        GetCar(mycar).worked = false;

    if (GetCar(mycar).value == 1) // error because reference type=value type
        GetCar(mycar).worked = false;

I think second if condition should be like

    if (GetCar(mycar2).value == 1) 
     GetCar(mycar2).worked = false;

Upvotes: 0

GvS
GvS

Reputation: 52538

You could use the object.Equals method, this will cast both operands to object first:

        if (object.Equals(GetCar(mycar, c).value,"ABC")) // working
            GetCar(mycar, c).worked = false;

        if (object.Equals(GetCar(mycar2, c).value,1)) // compiles, and does not throw exception
            GetCar(mycar2, c).worked = false;

Upvotes: 3

Attila Szász
Attila Szász

Reputation: 747

For example you can create two value if, you want to use string and int in the same time.

public class Cars
{
    public int intValue { get; set; }
    public string stringValue { get; set; }
    public bool worked { get; set; }
}
public Cars GetCar(int value, Cars c)
{
    c.intValue = value;
    return c;
}
public Cars GetCar(string value, Cars c)
{
    c.stringValue = value;
    return c;
}

public void Main(string[] args)
{
    Cars c = new Cars();
    string mycar = "ABC-12";
    int mycar2 = 123;

    if (GetCar(mycar, c).stringValue == "ABC")
        GetCar(mycar, c).worked = false;

    if (GetCar(mycar2, c).intValue == 1)
        GetCar(mycar2, c).worked = false;
}

This will work, but it's not a nice solution. I need more info about the project to be able to create a good design.

Upvotes: 0

apomene
apomene

Reputation: 14389

mycar is of type object. When you set string mycar = "ABC-12", you explicitly set it to be of type string. So once you compare it with an int value you get an exception.

This should also help: if (<object> == <int>)

Upvotes: 1

Related Questions