How do I check the type of the value represented in a system.type?

D

Don

I have an array of System.type and I need to go through the array and
perform different logic depending on the type stored in the array.

I want to do:
if (typeof typeCollection(i) is String) then
'some logic
elseif (typeof typeCollection(i) is int32) then
'some logic
.....

The problem is the type of my object is always system.type. If I
toString it I get the text that represents the data type but I don't
really like string comparisions when I'm trying to determine type.

I am storing the type of data from a dataColumn in this array as
follows:
typeCollection.add(dataColumn.dataType())

I then add the array to the viewState so I can access the data after a
postback.

Thanks,
Don
 
S

S. Justin Gengo

Don,

I think you need to store your items as an array of System.Object instead of
System.Type. Then when you check them you won't always get back System.Type.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
D

Don

I have a DataTable filled with data from the database which I bind to
the datagrid. On post back I need to be able to determine the data
type for each cell on the selected row.

To do this I iterate through the columns in the data table and add the
type of each column to the array as shown below:
dim col as datacolumn

for each col in datatable.column
typeCollection.add(col.DataType())
next

I can't add the column to the array because it is not serializable and
I need to store it in the ViewState. I don't have an actual object to
add to the array. I could iterate through the first row and add that
object to the array but I still need the DataTypes when I have no rows
returned.
 
S

S. Justin Gengo

Don,

Ahhhh, didn't know that. I think you could do this:

Going with what you said earlier about ToString working go ahead and store
each column type using ToString

typeCollection.add(col.DataType().ToString)

But then when you do your comparison use the System.Type object's GetType
method.

If (System.Type.GetType(typeCollection(i).toString) Is String) Then

The System.Type.GetType function returns a type based on the string name.



--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
D

Don

This does not work. As you stated GetType returns a type based on the
string name and I already have that in the typeCollection in array.
The problem is that the type of this obejct is System.Type not
System.String so I can't do the typeOf operation.
 
S

S. Justin Gengo

Don,

Did you try it?

Works fine for me.

Here's the code I used, run it and see.

Dim DataTable1 As New DataTable

DataTable1.Columns.Add(New DataColumn("Column1", GetType(String)))

DataTable1.Columns.Add(New DataColumn("Column2", GetType(Int32)))

DataTable1.Columns.Add(New DataColumn("Column3", GetType(Object)))

DataTable1.Columns.Add(New DataColumn("Column4", GetType(DateTime)))

Dim ArrayList As New ArrayList

For Each Column As DataColumn In DataTable1.Columns

ArrayList.Add(Column.DataType.ToString)

Next

Dim ColumnCount, ColumnLoop As Int32

ColumnCount = ArrayList.Count - 1

Dim TypeString As String

For ColumnLoop = 0 To ColumnCount

If System.Type.GetType(ArrayList(ColumnLoop).ToString) Is GetType(Int32)
Then

Response.Write("Int32 Found in Column " & ColumnLoop.ToString)

End If

Next


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top