473,806 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Testing the 'type' of an Object???

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
6 1743
>"Darryn Ross" <da****@datawav e.com.au> wrote in message
news:uy******** ******@TK2MSFTN GP12.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().ToS tring();

Joe
--
http://www.csharp-station.com
Nov 15 '05 #2
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***@nospamAt CSharpDashStati on.com> wrote in message
news:e9******** ******@TK2MSFTN GP11.phx.gbl...
"Darryn Ross" <da****@datawav e.com.au> wrote in message

news:uy******** ******@TK2MSFTN GP12.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().ToS tring();

Joe
--
http://www.csharp-station.com

Nov 15 '05 #3
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****@datawav e.com.au> wrote in message news:uy******** ******@TK2MSFTN GP12.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
Rearranged

On Fri, 6 Feb 2004 17:30:31 +0600, "Champika Nirosh"
<cn*****@textce ntric.lk> wrote:
"Joe Mayo [C# MVP]" <jm***@nospamAt CSharpDashStati on.com> wrote in message
news:e9******* *******@TK2MSFT NGP11.phx.gbl.. .
>"Darryn Ross" <da****@datawav e.com.au> wrote in message news:uy******** ******@TK2MSFTN GP12.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().ToS tring();

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.WriteLi ne(
@"Every object ever created is of a specific type.
anotherInt32 is of type {0}.
mySecondInt is of type {1}",
anInt32.GetType ().Name,
anotherInt32.Ge tType().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
Champika Nirosh <cn*****@textce ntric.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.WriteLi ne ("string");
}
else if (o is int)
{
Console.WriteLi ne ("int");
}
else if (o is bool)
{
Console.WriteLi ne ("bool");
}
else
{
Console.WriteLi ne ("Unknown");
}
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Vladimir Scherbina <vl*********@uk r.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1763
by: Steve | last post by:
Hi, I've started to make use of references for objects and arrays while putting together a new site framework and am wondering if there is any sort of debug code that I could put in to my objects to help show if they are actually copies or references e.g. retrieving memory address of object or it's internal object id. For example:
4
1928
by: a | last post by:
I'm having trouble testing a custom object. I've tried many different approaches. One is shown below. The XML below shows the state of the object and I'm trying to test for that state, ie there are NO classes in the Classes collection (Classes) of the custom object. The conditional expression returns the error shown below it... =======================================================XML returned by
4
1167
by: Notre Poubelle | last post by:
Hello, I have some code where I need to store a value as LPVOID. I'd like to be able to do some runtime testing about whether the thing that's pointed to is of a certain type. I've tried using RTTI's typeid and dynamic_cast functions (the latter won't compile), but they've not worked for me. I've included a short sample program that sort of shows what I'm trying to do and possibly reveals whether I'm doing something dumb. The...
3
2445
by: Joe Van Dyk | last post by:
Say I've written a class that wraps around a particular (complex) communication service library. I want to unit test objects that use that communication service. Is the "best practice" to create a Communication_Service abstract base class that my Concrete_Communication_Service and my Mock_Communication_Service both inherit from? (and then use the mock service when unit testing)
44
2867
by: John A. Bailo | last post by:
Dr. Dobbs has a /glowing/ article on Ruby on Rails this month. What do you guys think? Can it replace .net, php and java? And be the Open Source OOP web solution that is not bound to Sun or MS? http://media.rubyonrails.org/presentations/pursuitofbeauty.pdf
1
1822
by: listservs | last post by:
I have some unit testing code in one of my modules that appears to run without an error, but the unit test fails anyhow. Have a look at the output below -- the TestResult seems to have no errors and no failures, yet I get a system exit. ------------------------------------------------------------------------ --- exceptions.SystemExit Traceback (most recent call last)
18
2402
by: Andrew Wan | last post by:
I have been developing web applications with ASP & Javascript for a long time. I have been using Visual Studio 2003.NET. While VS2003 is okay for intellisense of ASP & Javascript, it's still not that great. One of the cons of ASP & Javascript is that they're both interpreted, which means one has twice the amount of work to do interms of syntax checking & semantic/runtime checking. Another bad thing is that ASP & Javascript doesn't have...
11
2386
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have realized that despite suggestions to use DHTML-based modal dialogs are very common? there is not a single fully functional reliable copyright-free cross-browser alternative to say MsgBox (VBScript) or showModalDialog (IE). This way such suggestions up to...
4
1154
by: Emanuele D'Arrigo | last post by:
Hi everybody, I'm just having a go with Unit Testing for the first time and my feeling about it in short is: Neat! I'm a bit worried about the time it's taking me to develop the tests but after only a day or so I'm already much faster than when I started with it and the code is already much improved in terms of robustness. A couple of "philosophical" questions have emerged in the process.
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10618
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.