if (sentNUD.Value.GetType() == typeof(string))
{
// string stuff
}
How to detect if var is a string
-
20-09-2022 - |
Question
I am having a bit of trouble using an if statement to detect if my numericupdown object is a string so I don't use a decimal variable to adjust it. I saw there was a type of commmand, but I am having trouble using it. Here is what I am looking at:
if(typeof sentNUD.Value == string)
{
//Do string arguments here
}
else
{
//do decimal arguments here
}
The syntax of the if statement is wrong though and I get an error. How do I set up that if statement correctly? I tried looking around a bit, but I am having trouble understanding how to do this correctly. Thank you.
Solution 2
OTHER TIPS
You can use the "is"-operator:
if(variable is string)
{
}
Should you use the method's static overloading to work your solution ??
The use of "typeOf" is normaly frowned by OO purists.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow