473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Comparing Types

newbie question:

if I have a Type object that is the type of a particular instance, how
can
I compare it to given Types to determine what type it is.

i want to be able to branch depending on the type of an instance.
if Type is String
if Type is String[]
if Type is Collection
etc

Nov 16 '05 #1
4 18923
You should be able to use typeof, like

if (myType is typeof(string))
{
}
else
....

Bennie Haelen

derek wrote:
newbie question:

if I have a Type object that is the type of a particular instance, how
can
I compare it to given Types to determine what type it is.

i want to be able to branch depending on the type of an instance.
if Type is String
if Type is String[]
if Type is Collection
etc

Nov 16 '05 #2
"derek" <de************@yahoo.com> wrote:
if I have a Type object that is the type of a particular
instance, how can I compare it to given Types to determine
what type it is.


if (myType == typeof(string)) // or whichever type you want

P.
Nov 16 '05 #3
if (typeof(IDrawable).IsAssignableFrom(t))

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
Not "is", you want to use "==" there.
"is" will get the type of an instance in the LHS and compare it to a type in
the RHS.

"Bennie Haelen" <Be***********@jda.com> wrote in message
news:O%***************@TK2MSFTNGP12.phx.gbl...
You should be able to use typeof, like

if (myType is typeof(string))
{
}
else
...

Bennie Haelen

derek wrote:
newbie question:

if I have a Type object that is the type of a particular instance, how
can
I compare it to given Types to determine what type it is.

i want to be able to branch depending on the type of an instance.
if Type is String
if Type is String[]
if Type is Collection
etc

Nov 16 '05 #5

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

Similar topics

5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
5
by: George | last post by:
How do I compare the values of two objects when Option Strict is On? One of the objects is the Tag property from some control (declared as object but holding a value, e.g. an Integer or a String...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
5
by: ma740988 | last post by:
There's a need for me to move around at specified offsets within memory. As as a result - long story short - unsigned char* is the type of choice. At issue: Consider the case ( test code ) where...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
12
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b)...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
3
by: Mike J | last post by:
Hi..need help comparing types example method istypeof(object someobj) { if (someobj=int32) } ya kinda get my idea here.... MJ
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.