473,320 Members | 1,821 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,320 software developers and data experts.

Checking for variable type

Hello,

I am writing a data serialization routine, and need to determine the value
type for each element in given hashtable. Number types should be treated in
one way, string and char types in other way, regex type in other, and arrays
in just another. Now I am quite confused about what could be the best
strategy for this type checking. Of course, I can do
myVar.getType().ToString() to get the full type identifier. But, I'd be
happy for some kind of "isNumeric" method that would straightforward give
feedback whether the given type is nueric one. Arrays could be checked by
comparing with the IList interface.
So, among all the different choices, what would be the best way?

Thanks,

Pavils
Nov 16 '05 #1
4 42196
Hi Pavils,

IMHO you should check the Type objects for the values.

e.g. if(typeof(char) == value.GetType())
{
//char
}
else if(typeof(string) == value.GetType())
{
//string
}

infortuantely AFAIK there is no IsNumber like method so you should chack
against different number type Type objects.

If you want to filter down all objects implementing given interface (IList
for example)
you can use the following

if(typeof(IList).IsAssignableFrom(value.GetType()) )
{
//List
}

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Pavils Jurjans" <pa****@mailbox.riga.lv> wrote in message
news:Oc**************@TK2MSFTNGP12.phx.gbl...
Hello,

I am writing a data serialization routine, and need to determine the value
type for each element in given hashtable. Number types should be treated in one way, string and char types in other way, regex type in other, and arrays in just another. Now I am quite confused about what could be the best
strategy for this type checking. Of course, I can do
myVar.getType().ToString() to get the full type identifier. But, I'd be
happy for some kind of "isNumeric" method that would straightforward give
feedback whether the given type is nueric one. Arrays could be checked by
comparing with the IList interface.
So, among all the different choices, what would be the best way?

Thanks,

Pavils

Nov 16 '05 #2
Stoitcho Goutsev (100) [C# MVP] wrote:

If you want to filter down all objects implementing given interface (IList
for example)
you can use the following

if(typeof(IList).IsAssignableFrom(value.GetType()) )
{
//List
}


Or the much less verbose:

if (value is IList)
{
// List
}

--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk
Nov 16 '05 #3
Hello Stoitcho,
IMHO you should check the Type objects for the values.

e.g. if(typeof(char) == value.GetType())
{
//char
}
else if(typeof(string) == value.GetType())
{
//string
}
if(typeof(IList).IsAssignableFrom(value.GetType()) )
{
//List
}


Cool, thanks, I will go that route.

Isn't this the same as {value is IList } ?

Rgds,

Pavils
Nov 16 '05 #4
Hi Pavils,
Yes, they are almost the same. The only difference is that with *is*
operator the type is always hard coded. Using IsAssignableFrom the type
objects can be stored in some kind of collection, thus more extendable. I
agree that I should've given better example. Anyways if you want to hardcode
the types using *is* is more readable.

There is one more thing. *is* operator does actual type casting where
IsAssignableFrom doesn't have to do that. You shouldn't be concerned about
the performance in this case, but it is possible that IsAssignableFrom is
better performance wise. This is only a guess and I may be wrong. Hoewever
this is not important I believe.
--

Stoitcho Goutsev (100) [C# MVP]
"Pavils Jurjans" <pa****@mailbox.riga.lv> wrote in message
news:Oe**************@TK2MSFTNGP12.phx.gbl...
Hello Stoitcho,
IMHO you should check the Type objects for the values.

e.g. if(typeof(char) == value.GetType())
{
//char
}
else if(typeof(string) == value.GetType())
{
//string
}
if(typeof(IList).IsAssignableFrom(value.GetType()) )
{
//List
}


Cool, thanks, I will go that route.

Isn't this the same as {value is IList } ?

Rgds,

Pavils

Nov 16 '05 #5

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

Similar topics

5
by: bartek d | last post by:
Hello, Regarding my previous question about a class which is used to store a variable type vector. I tried to be more elaborate on the code. I'd be grateful for your suggestions. Am I going in...
4
by: MCollins | last post by:
trying to determine a variable type, specifically that a variable is an integer. i tried using type(var) but that only seemed to produce a response in the command line. is there a built in...
5
by: frankhall36 | last post by:
Yeah, I need some help, I'm not a very good programmer but I've tried a lot of languages, and anyways, I want to start applying programming to physics, and I would like to learn how to make a...
2
by: Mike Moore | last post by:
does anyone have an example of how to get the connection string object converted to a string variable type in order for me to call a function?
1
by: tshad | last post by:
I have some code to go through a session collection for my error page routine and I get an error on my objects that I store in session variables. Dim strName as String Dim iLoop as Integer ...
5
by: John | last post by:
Which variable type (c#) can whole the largest whole number? I know this sounds silly but as double and decimal are made for numbers with decimals I am not sure. Also if anybody knows of any...
1
by: Sharon | last post by:
I need to write an XML document, that other users can work with to change values and to add elements. My problem is that for each element that me or any other user will add, should have some...
4
by: mouseit | last post by:
If I have a variable of some sort, say x, how can I find out what variable type javascript thinks it is? For example, if I've declared x as 5 earlier, how do I know now whether it's a string or a...
2
by: Dan29 | last post by:
Hi guys, I wonder if you can help? I am trying to create Query Definitions in VBA in Access 2002. I'm using the book 'Begining Access 2002 VBA', which shows how to do this. On the example...
1
by: 55555 | last post by:
what is difference between variable type and object type??
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
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...
1
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....
0
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.