fac7orx
fac7orx

Reputation: 335

How to detect if var is a string

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.

Upvotes: 3

Views: 255

Answers (3)

Renan Ranelli
Renan Ranelli

Reputation: 414

Should you use the method's static overloading to work your solution ??

The use of "typeOf" is normaly frowned by OO purists.

Upvotes: 0

nvoigt
nvoigt

Reputation: 77354

You can use the "is"-operator:

if(variable is string)
{

}

Upvotes: 17

Cam Bruce
Cam Bruce

Reputation: 5689

if (sentNUD.Value.GetType() == typeof(string))
{
// string stuff
}

Upvotes: 7

Related Questions