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

Discussion in 'ASP .Net' started by Don, Sep 12, 2005.

  1. Don

    Don Guest

    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
     
    Don, Sep 12, 2005
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    >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. Justin Gengo, Sep 12, 2005
    #2
    1. Advertising

  3. Don

    Don Guest

    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.
     
    Don, Sep 12, 2005
    #3
  4. 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" <> wrote in message
    news:...
    >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. Justin Gengo, Sep 12, 2005
    #4
  5. Don

    Don Guest

    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.
     
    Don, Sep 13, 2005
    #5
  6. 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" <> wrote in message
    news:...
    > 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. Justin Gengo, Sep 13, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Random
    Replies:
    5
    Views:
    425
    Random
    Feb 26, 2007
  2. xpika

    how is a struct represented internally.

    xpika, Jan 3, 2008, in forum: C Programming
    Replies:
    5
    Views:
    344
    David Thompson
    Jan 14, 2008
  3. Peng Yu
    Replies:
    8
    Views:
    318
    Simon Forman
    Sep 21, 2009
  4. Abder-Rahman Ali
    Replies:
    7
    Views:
    112
    Abder-Rahman Ali
    Sep 20, 2010
  5. Replies:
    41
    Views:
    392
Loading...

Share This Page