473,586 Members | 2,855 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 3025
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();...
7
9948
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
1632
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
5608
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
2
2335
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
7101
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...
20
9272
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
21727
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
7839
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...
0
8338
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...
0
8215
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...
0
6610
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...
0
5390
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...
0
3836
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...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
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...

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.