casting a value to a Data-Type that is known only at Runtime

R

Reiner Ziegler

Hi there,

I'm writing my first bigger program with VS2005 (C#) and
CF 2.0 (means I'm a newbee to .NET!).

I have a problem which I can describe
in simple words like so:

I wish to convert the Returnvalue of the
System.Data.DataRow Indexer (e.g. object outValue = row[index])
(which is generic System.Object) to the specific Data-Type of
the DataColumn at RUNTIME!

Is this possible and how can I acomplish that?

Any help is VERY appreciated!

Thanks in advance and greetings

Reiner
 
M

Mihai Tataran

Try this:

YourType rowValue = (YourType)row[index];

I guess the type of the DataColumn is something like int or string.

Mihai Tataran
 
R

Reiner Ziegler

Chris,

NOT so simple!

The Expression "dataRow[columnNumber].GetType()" returns
an object System.Type! Provided the return-value is "3", afterwards
it was "System.String"! Thats not intended...

regards

Reiner


Simple:

dataRow[columnNumber].GetType()

-Chris


Reiner Ziegler said:
Hi there,

I'm writing my first bigger program with VS2005 (C#) and
CF 2.0 (means I'm a newbee to .NET!).

I have a problem which I can describe
in simple words like so:

I wish to convert the Returnvalue of the
System.Data.DataRow Indexer (e.g. object outValue = row[index])
(which is generic System.Object) to the specific Data-Type of
the DataColumn at RUNTIME!

Is this possible and how can I acomplish that?

Any help is VERY appreciated!

Thanks in advance and greetings

Reiner
 
G

Guest

I don't quite understand what you're saying. What actual data type is
dataRow[columnNumber]? What does GetType() return? I just used this
yesterday (albeit in a slightly more complex way) and it worked just fine.

If you have a break point set on that line and go to the immediate window
and type "?dataRow[comunNumber]" what does it return? What does it return
if you type "?dataRow[comunNumber].GetType()"?

-Chris




Reiner Ziegler said:
Chris,

NOT so simple!

The Expression "dataRow[columnNumber].GetType()" returns
an object System.Type! Provided the return-value is "3", afterwards
it was "System.String"! Thats not intended...

regards

Reiner


Simple:

dataRow[columnNumber].GetType()

-Chris


Reiner Ziegler said:
Hi there,

I'm writing my first bigger program with VS2005 (C#) and
CF 2.0 (means I'm a newbee to .NET!).

I have a problem which I can describe
in simple words like so:

I wish to convert the Returnvalue of the
System.Data.DataRow Indexer (e.g. object outValue = row[index])
(which is generic System.Object) to the specific Data-Type of
the DataColumn at RUNTIME!

Is this possible and how can I acomplish that?

Any help is VERY appreciated!

Thanks in advance and greetings

Reiner
 
R

Reiner Ziegler

1.) "?dataRow[comunNumber]" --> returns
"3"
2.) "?dataRow[columnNumber].GetType()" --> returns
"System.String"

regards

Reiner

I don't quite understand what you're saying. What actual data type is
dataRow[columnNumber]? What does GetType() return? I just used this
yesterday (albeit in a slightly more complex way) and it worked just fine.

If you have a break point set on that line and go to the immediate window
and type "?dataRow[comunNumber]" what does it return? What does it return
if you type "?dataRow[comunNumber].GetType()"?

-Chris




Reiner Ziegler said:
Chris,

NOT so simple!

The Expression "dataRow[columnNumber].GetType()" returns
an object System.Type! Provided the return-value is "3", afterwards
it was "System.String"! Thats not intended...

regards

Reiner


Simple:

dataRow[columnNumber].GetType()

-Chris


Hi there,

I'm writing my first bigger program with VS2005 (C#) and
CF 2.0 (means I'm a newbee to .NET!).

I have a problem which I can describe
in simple words like so:

I wish to convert the Returnvalue of the
System.Data.DataRow Indexer (e.g. object outValue = row[index])
(which is generic System.Object) to the specific Data-Type of
the DataColumn at RUNTIME!

Is this possible and how can I acomplish that?

Any help is VERY appreciated!

Thanks in advance and greetings

Reiner
 
S

Scott Ayton

if dataRow[columnNumber].GetType() is Gettype(system.string) THEN
dim myString AS String = ctype(dataRow[columnNumber], String)
--convert code
elseif dataRow[columnNumber].GetType() is Gettype(system.integer) THEN
dim myInteger AS Integer = ctype(dataRow[columnNumber], Integer)
--convert code
elseif .... check for other types in your database....
end if

This is how you can determine the type of what is returned and then do
whatever you wish with it. I would suggest checking for type
System.DBNull aswell.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top