473,847 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

typeof()

Hello:

I want to know if somo variable is integer, real or string but typeof
returns Int16, Int32, etc, because of that my if() statement has to compare
if X is Int32 or Int64 or ...

Any idea how to reduce the if statement?

thanks
Nov 15 '05 #1
4 3043
rua17 <ru***@hotmail. com> wrote:
I want to know if somo variable is integer, real or string but typeof
returns Int16, Int32, etc, because of that my if() statement has to compare
if X is Int32 or Int64 or ...

Any idea how to reduce the if statement?


Well, you could have two collections (eg Hashtables), one containing
the types of Int32, Int64, UInt32 etc, another containing the types of
Single and Double, and then just a test for string...

--
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 #2
Use the "is" keyword.

You can do something like:

if (23 is int)
{
// Do your stuff
}

-vJ

"rua17" <ru***@hotmail. com> wrote in message
news:OQ******** ******@TK2MSFTN GP11.phx.gbl...
Hello:

I want to know if somo variable is integer, real or string but typeof
returns Int16, Int32, etc, because of that my if() statement has to compare if X is Int32 or Int64 or ...

Any idea how to reduce the if statement?

thanks

Nov 15 '05 #3
Thanks
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
rua17 <ru***@hotmail. com> wrote:
I want to know if somo variable is integer, real or string but typeof
returns Int16, Int32, etc, because of that my if() statement has to compare if X is Int32 or Int64 or ...

Any idea how to reduce the if statement?


Well, you could have two collections (eg Hashtables), one containing
the types of Int32, Int64, UInt32 etc, another containing the types of
Single and Double, and then just a test for string...

--
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 #4
The easiest way to do this is

if (obj is string)
{

}
else if (obj is float || obj is double)
{

}
else if (obj is int || obj is uint || obj is short || obj is ushort ||
obj is long || obj is ulong || obj is byte || obj is sbyte)
{

}
else
{
// Unexpected Type
}

The previous code is preferable, but you can still use if statement with
typeof, by comparing Type objects:

if (obj == null)
{
// Unexpected value

}
if (obj.GetType() ==
typeof(string))
{

}
else if (obj.GetType() == typeof(float) || obj.GetType() ==
typeof(double))
{

}
else if (/*check for integral types in the same way*/)
{

}
else
{
// Unexpected Type
}

Please let me know if you need more clarification

Karim Elias
C# Compiler QA
Microsoft

Nov 15 '05 #5

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

Similar topics

4
299
by: Eric | last post by:
I need to do the following but it doesn't compile if(typeof(listBox1.Items) == typeof(string)){ return; } typeof(listBox1.Items) doesn't work at all. My listBox has 2 types of items in them and I need to know what kind of item a user clicked on so I can use it for drag and drop
4
536
by: ichor | last post by:
hi what is the use of the typeof keyword , and how does it differ from the GetType method. i found the GetType method useful but i fail to understand the use of the typeof method. i have tried out a very simple example which i made up myself. Employee e = new Employee(); ContractEmp ec =new ContractEmp(); MessageBox.Show(typeof(Employee).ToString());
7
9984
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the method. The method could look like this: public void myDynamicMethod(Type type){ //.....do something with the Type passed in....... }
3
950
by: Alberto | last post by:
Can somebody tell me why this typeof doesn't work? foreach (Control myControl in Controls) if (typeof(myControl) == "TextBox") ((TextBox)myControl).Text = string.Empty; Thank you very much
1
1642
by: Brien King | last post by:
Ok, I have three classes (The example here is extremely simplified to illustrate the problem) like this: Public Class A Public Sub DoSomething(ByVal myClass) If TypeOf myClass IS A Then ' ' Never Enters here.... ' End If
11
5628
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
2
2344
by: Andrew Robinson | last post by:
I am guessing there is a simple solution but given a type T, how can I check for nullability? how can I accomplish the following? bool nullable = typeof(int).IsNullable; // false bool nullable = typeof(int?).IsNullable; // true bool nullable = typeof(string).IsNullable; // true Thanks for any help.
4
7116
by: EManning | last post by:
Using A2003. I've got an option group that has a number of check boxes. I have coding to clear the option group if the user wishes to cancel their choice. This coding also clears the rest of the controls on the form. Snippet of the coding: <..snipped..> ElseIf TypeOf oCtrl Is CheckBox Then oCtrl.Value = False <..snipped..> ElseIf TypeOf oCtrl Is OptionGroup Then
20
9326
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues = displayValuesArray.length; for (i=0; i<nDisplayValues; i++) {
20
21798
by: rkk | last post by:
Hi, Is there an equivalent typeof macro/method to determine the type of a variable in runtime & most importantly that works well with most known C compilers? gcc compiler supports typeof() macro, but the same code is not getting compiled in solaris forte compiler and in microsoft VS 2003 compiler. I tried something like below:
0
9879
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
10981
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
10643
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...
1
10706
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10330
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5716
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...
0
5907
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4521
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
3
3158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.