Reputation: 103
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
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
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
Reputation: 1
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
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
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
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