Testing the 'type' of an Object???

  • Thread starter Thread starter Darryn Ross
  • Start date Start date
D

Darryn Ross

Hi...

I am not sure how to test the type of an object that i define

object a = "test" ;
object b = 1 ;
object c = true ;

how do i test each of the three objects to find out what types they are.. e.g string, int, bool???

Regards
 
Darryn Ross said:
I am not sure how to test the type of an object that i define
object a = "test" ;
object b = 1 ;
object c = true ;
how do i test each of the three objects to find out what types they are..
e.g string, int, bool???

Hi Darryn,

You can use the 'is' operator:

if (a is string) {...}

If you wanted to find out what type it was you could do this:

string myTypeString = a.GetType().ToString();

Joe
 
I don't think so.. since at the creation level all three object are created
as object you wont be able to get the result you want with the given
solution

if so do it this way

string my = "test";
int i = 1;
bool bol = true;

object a = my;
object b = i;
object c = true;

this way you will get the result

Nirosh.
 
object a = "test" ;
object b = 1 ;
object c = true ;

if (a.GetType() == typeof(string))
{
}
else if (b.GetType() == typeof(int32))
{
}
else if (c.GetType() == typeof(bool))
{
}

Vladimir Scherbina,
Ukraine, Kiev.

Hi...

I am not sure how to test the type of an object that i define

object a = "test" ;
object b = 1 ;
object c = true ;

how do i test each of the three objects to find out what types they are.. e.g string, int, bool???

Regards
 
Rearranged

I don't think so.. since at the creation level all three object are created
as object you wont be able to get the result you want with the given
solution

This is incorrect. Try running this:

using System;
namespace CSharpConsole
{
class Clarification
{
[STAThread]
static void Main(string[] args)
{
object anInt32 = 3;
int anotherInt32 = 4;
Console.WriteLine(
@"Every object ever created is of a specific type.
anotherInt32 is of type {0}.
mySecondInt is of type {1}",
anInt32.GetType().Name,
anotherInt32.GetType().Name);
}
}
}


The type of the varible to which you assign a literal such as 1 or
"myString" doesn't affect the type of the literal. It may cause an
implicit conversion, or boxing, but that doesn't change the fact that
the original literal has its own well defined type.

/Magnus Lidbom
 
Champika Nirosh said:
I don't think so.. since at the creation level all three object are created
as object

No they're not. Just because the variables are *declared* as object
doesn't change the type of the object itself at all.

Here's a test program showing that Joe was right:

using System;

class Test
{
static void Main()
{
object a = "test" ;
object b = 1 ;
object c = true ;

ShowType(a);
ShowType(b);
ShowType(c);
}

static void ShowType(object o)
{
if (o is string)
{
Console.WriteLine ("string");
}
else if (o is int)
{
Console.WriteLine ("int");
}
else if (o is bool)
{
Console.WriteLine ("bool");
}
else
{
Console.WriteLine ("Unknown");
}
}
}
 
Vladimir Scherbina said:
object a = "test" ;
object b = 1 ;
object c = true ;

if (a.GetType() == typeof(string))
{
}
else if (b.GetType() == typeof(int32))
{
}
else if (c.GetType() == typeof(bool))
{
}

Although that will indeed work, using "is" performs better, is more
readable, and works with derived types as well (which isn't applicable
here, but would be in other situations).
 
Back
Top