Reputation: 57
I am attempting to make a suvat calculator and require to have some form of way to have two different types of variable stored in one variable.
For those unaware of suvat. suvat equations are a collection of equations which allows for each of the five variables to be found from only three known variables.
This means that I need to be able to have a variable hold a float and a null value. Is there any way of doing this?
Upvotes: 0
Views: 92
Reputation: 11489
In order for a variable to hold both a float or null, you need to use nullable types. For example, you can use "float?" as the type:
float? myFloat = null;
If you need to hold a double or null, use the nullable double type:
double? myDouble = null;
etc.
Upvotes: 3
Reputation: 37070
You can simply create a class that contains the types you need. For example, if you need a float and a string (you said null
, but that is not a type, so I'm using string
, which can be null):
class SomeClassName
{
public float FloatValue {get; set;}
public string StringValue {get; set;}
}
Now you can create a variable of type SomeClassName
and assign it both a float and a string:
SomeClassName suvat = new SomeClassName();
suvat.FloatValue = 1.2;
suvat.StringValue = null;
Upvotes: 0