Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How do I check the type of the value represented in a system.type?

Reply
Thread Tools

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

 
 
Don
Guest
Posts: n/a
 
      09-12-2005
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

 
Reply With Quote
 
 
 
 
S. Justin Gengo
Guest
Posts: n/a
 
      09-12-2005
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
"Don" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>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
>



 
Reply With Quote
 
 
 
 
Don
Guest
Posts: n/a
 
      09-12-2005
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.

 
Reply With Quote
 
S. Justin Gengo
Guest
Posts: n/a
 
      09-12-2005
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
"Don" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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.
>



 
Reply With Quote
 
Don
Guest
Posts: n/a
 
      09-13-2005
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.

 
Reply With Quote
 
S. Justin Gengo
Guest
Posts: n/a
 
      09-13-2005
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
"Don" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can this Java method header be represented in Ruby Abder-Rahman Ali Ruby 7 09-20-2010 02:00 PM
How to get the minimum number that can be represented? Peng Yu Python 8 09-21-2009 05:27 AM
how is a struct represented internally. xpika C Programming 5 01-14-2008 05:42 AM
how to find the type of an object represented by an interface Random ASP .Net 5 02-26-2007 05:50 PM
How are JS numbers represented internally?? bobalong@gmx.net Javascript 41 03-22-2006 07:27 AM



Advertisments