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

Determining type

C# newbie question:

I'm trying to determine the type of a datareader value. Why cant I do it
like this?

if (rdr.GetValue(0).GetType() is Guid || rdr.GetValue(0).GetType() is
Int32)
{
// do something
}
else{
// somethine else
}
Thanks in advance,
DougS


Jul 29 '06 #1
5 1791
I'm new to this too, but I think instead of "is" you mean "==".

As I understand it, an expression like "X is int" would mean X is an object
of type int.

The value returned by GetType is always of type Type; its value is a type.

"Doug Stiers" <nonewrote in message
news:Oy**************@TK2MSFTNGP03.phx.gbl...
C# newbie question:

I'm trying to determine the type of a datareader value. Why cant I do it
like this?

if (rdr.GetValue(0).GetType() is Guid || rdr.GetValue(0).GetType() is
Int32)
{
// do something
}
else{
// somethine else
}
Thanks in advance,
DougS




Jul 29 '06 #2
if (rdr.GetValue(0).GetType() == typeof(Guid))
{
....
}

Sincerely,
simida

Doug Stiers 写道:
C# newbie question:

I'm trying to determine the type of a datareader value. Why cant I do it
like this?

if (rdr.GetValue(0).GetType() is Guid || rdr.GetValue(0).GetType() is
Int32)
{
// do something
}
else{
// somethine else
}
Thanks in advance,
DougS
Jul 29 '06 #3
Doug Stiers wrote:
I'm trying to determine the type of a datareader value. Why cant I do it
like this?

if (rdr.GetValue(0).GetType() is Guid || rdr.GetValue(0).GetType() is
Int32)
The "is" operator works for instances: it will tell you if a
particular object is assignment compatible with the instance of the
type on the right (ie, is an instance of that type, or is an instance
of a descendant type).

GetType() returns a System.Type instance. A Type instance is a runtime
description of a compile-time type; it is not an instance of the type,
nor is it the compile-time type itself.

You should be able to do what you want with either

Type FirstValueType = rdr.GetValue(0).GetType();
if (FirstValueType == typeof(Guid) ||
FirstValueType == typeof(Int32))

// or

object FirstValue = rdr.GetValue(0);
if (FirstValue is Guid || FirstValue is Int32)

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Jul 29 '06 #4
Doug Stiers wrote:
C# newbie question:

I'm trying to determine the type of a datareader value. Why cant I do it
like this?

if (rdr.GetValue(0).GetType() is Guid || rdr.GetValue(0).GetType() is
Int32)
Just remove the .GetType(). You don't need that here as "is" can
directly compare an object to a type:

if (rdr.GetValue(0) is Guid || rdr.GetValue(0) is Int32) {
...
}

hth,
Max

Jul 29 '06 #5
I wrote:
The "is" operator works for instances: it will tell you if a
particular object is assignment compatible with the instance of the
type on the right
That was careless - it's not "assignment compatible with the instance
of the
type on the right" but "assignment compatible with *a variable* of the
type on the right."

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Jul 29 '06 #6

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

Similar topics

1
by: Simon Wigzell | last post by:
I am adapting a javascript pulldown menu system to my dynamic website generator - the arrays that hold the menu items information are read from a database and will be different for different users...
12
by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
18
by: Robert | last post by:
Hi! I was wondering if the was any way to determine the state of the caps lock key, on or off. Of course I can capture the key events and see whether the caps lock is pressed, but that does not...
0
by: CTDev Team | last post by:
Hi, We are using Exchange Server 5.5, and have applications written in VB6 and C# that read and process emails. We are experiencing intermittent errors similar to C# Application ...
2
by: Garrett | last post by:
Need any help in determining which groups have been given security access to a folder. Searched DirectoryServices to no avail... Any Help?
4
by: petermichaux | last post by:
Hi, I'm hoping for a reason I'm wrong or an alternate solution... I'd like to be able to dynamically include some javascript files. This is like scriptaculous.js library but their solution is...
1
by: Mantorok | last post by:
Hi Is there a "concrete" way of determining the Mime type? I'm currently using the registry as a source, however, what if the extension is not listed in the registry? Thanks Kev
9
by: Clay_Culver | last post by:
I need to find out if an object is a class. Using new style classes this is very easy: class Test(object): pass obj = Test or obj = Test() if type(obj) == type:
3
by: BronxJedi | last post by:
I need help determining the correct syntax for achieving something: I have a property and in the set accessor I want the code to determine if the value is of a certain type. If it's not then I...
1
by: Nathan Sokalski | last post by:
I am having trouble determining the MIME type of a file. Does anybody know how to do this for a file programmatically? Thanks. -- Nathan Sokalski njsokalski@hotmail.com...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll 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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
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...
1
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)...
1
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
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.