Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi,

Any one can help me how check the datatype of argument that we are passing now i am tried as
string st = Args.SortExpression.GetType();

But it wont works can u suggest me for any alternative in this...

Thanks in Advance

Seshu
Posted

It's "not working" as as object.GetType() does not return string; the return type is System.Type. It can work, but only if you need to use reflection seriously: for example check if the type supports certain interface, invoke type constructor to obtain instance and work with the instance using techniques which are agnostic to the type.

In more typical situation you just may need to know is the instance of the certain type (in case of class your can check if the instance is of the type belonging to some inheritance hierarchy) or perform dynamic type cast to conditionally down-case more abstract compile type to some more concrete run-time type. For these purposes, you need to use operators "is" and "as":

C#
object @object;

//...

@object = new //who knows what

if (@object is string) { //good check
    string @string = (string)@object); //correct, but not so good, see how to use "as" below
    //use value
}

string @string = @object as string; //however, this works only for nullable types
if (@string != null)
   //use @string;


Note: "@object" and "@string" in C# syntax are normal valid variable identifiers, "@" is just the way to create identifiers identical to language keywords.

—SA
 
Share this answer
 
use the GetType() and Typeof() to evaluate.

if object.getType== typeOf(System.Object)....
 
Share this answer
 
MIDL
if (Args.GetType() == Type.GetType(System.String))
{
      //Args is string
}
// you can get the Type of the object as a string by - Args.GetType.Name
string st = Args.GetType().Name;
if(st == "String")
{
     //Do something with string value
}
if(st == "Int32")
{
     // Do something with Interger value
}
if(st == "Single")
{
     // Do something with Float value
}
if(st == "Double")
{
     // Do something with Double value
}
//Or you can use Swith() case statements.....
 
Share this answer
 
Try that way

dgvMappedData.Columns.Cast<DataGridViewColumn>().ToList().Find(dc=>dc.Name == Args.SortExpression).ValueType
 
Share this answer
 
Try this.

string st = Args.SortExpression.GetType().ToString();
 
Share this answer
 
Comments
hiseshu 16-May-11 2:34am    
Hi
Thanks for spending time for me .. it wont works it returns always string type...

Seshu
try,

string st = Args.SortExpression.GetType().ToString();

Cheers
 
Share this answer
 
Comments
hiseshu 16-May-11 2:34am    
Hi
Thanks for spending time for me .. it wont works it returns always string type...

Seshu
Ra-one 19-May-11 2:35am    
I think you are getting It wrong, It will give the type in converted to string, If you want exact replace "string st" with "var st" and you are done
try this
C#
if (Args.GetType().Name == "String")
            {

               //Args is string
            }
            if (Args.GetType().Name == "Int32")
            {
               //Args is Integer

            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900