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 4 2994
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.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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**************@TK2MSFTNGP11.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
Thanks
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... 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.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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
|
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
'
'...
|
by: Jason Kendall |
last post by:
Why doesn't the new "IsNot" operator work in conjunction with
'Typeof'?
|
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...
|
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...
|
by: effendi |
last post by:
I am testting the following code in firefox
function fDHTMLPopulateFields(displayValuesArray, displayOrderArray)
{
var i,
currentElement,
displayFieldID,
currentChild,
nDisplayValues =...
|
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()...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |