By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

Testing the 'type' of an Object???

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
>"Darryn Ross" <da****@datawave.com.au> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...
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???

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
--
http://www.csharp-station.com
Nov 15 '05 #2

P: n/a
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.

"Joe Mayo [C# MVP]" <jm***@nospamAtCSharpDashStation.com> wrote in message
news:e9**************@TK2MSFTNGP11.phx.gbl...
"Darryn Ross" <da****@datawave.com.au> wrote in message

news:uy**************@TK2MSFTNGP12.phx.gbl...
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???

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
--
http://www.csharp-station.com

Nov 15 '05 #3

P: n/a
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.

"Darryn Ross" <da****@datawave.com.au> wrote in message news:uy**************@TK2MSFTNGP12.phx.gbl...
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
Nov 15 '05 #4

P: n/a
Rearranged

On Fri, 6 Feb 2004 17:30:31 +0600, "Champika Nirosh"
<cn*****@textcentric.lk> wrote:
"Joe Mayo [C# MVP]" <jm***@nospamAtCSharpDashStation.com> wrote in message
news:e9**************@TK2MSFTNGP11.phx.gbl...
>"Darryn Ross" <da****@datawave.com.au> wrote in message news:uy**************@TK2MSFTNGP12.phx.gbl...
>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???

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
--
http://www.csharp-station.com


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
Nov 15 '05 #5

P: n/a
Champika Nirosh <cn*****@textcentric.lk> wrote:
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");
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

P: n/a
Vladimir Scherbina <vl*********@ukr.net> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.