Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Weird class cast issue

Weird class cast issue

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    serious sam
    wrote on last edited by
    #1

    Hi all, I'm having a problem using class casts. I'm storing an ArrayList inside a DataTable, and when I want to retrieve the list, I get an InvalidCastException. Here is my code: using System; using System.Data; using System.Collections; class CastTest { const string COL = "test"; static void Main() { Console.WriteLine("CastTest"); ArrayList list = new ArrayList(); string str = "Hello world"; list.Add(str); Console.WriteLine("arrayList created"); DataTable table = new DataTable(); DataRow row = table.NewRow(); table.Columns.Add(COL); table.Rows.Add(row); row[COL] = list; Console.WriteLine("table created"); object resultObj = table.Rows[0][COL]; Console.WriteLine("resultObj: " + resultObj); ArrayList resultList = (ArrayList)resultObj; string resultStr = (string)resultList[0]; Console.WriteLine(resultStr); } } The second last WriteLine statement prints: "resultObj: System.Collections.ArrayList". I seem to be retrieving the ArrayList as an object, but I'm unable to cast it. Why? Thanks in advance. Cheers, Michael

    C 1 Reply Last reply
    0
    • S serious sam

      Hi all, I'm having a problem using class casts. I'm storing an ArrayList inside a DataTable, and when I want to retrieve the list, I get an InvalidCastException. Here is my code: using System; using System.Data; using System.Collections; class CastTest { const string COL = "test"; static void Main() { Console.WriteLine("CastTest"); ArrayList list = new ArrayList(); string str = "Hello world"; list.Add(str); Console.WriteLine("arrayList created"); DataTable table = new DataTable(); DataRow row = table.NewRow(); table.Columns.Add(COL); table.Rows.Add(row); row[COL] = list; Console.WriteLine("table created"); object resultObj = table.Rows[0][COL]; Console.WriteLine("resultObj: " + resultObj); ArrayList resultList = (ArrayList)resultObj; string resultStr = (string)resultList[0]; Console.WriteLine(resultStr); } } The second last WriteLine statement prints: "resultObj: System.Collections.ArrayList". I seem to be retrieving the ArrayList as an object, but I'm unable to cast it. Why? Thanks in advance. Cheers, Michael

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I ran the above code, and as I suspected, the arraylist is having it's 'tostring()' called as it goes into the table. You don't have an arraylist, you have a string which says 'System.Collections.ArrayList'. I doubt you can shove arrays into a cell of a table. Christian Graus - Microsoft MVP - C++

      S 1 Reply Last reply
      0
      • C Christian Graus

        I ran the above code, and as I suspected, the arraylist is having it's 'tostring()' called as it goes into the table. You don't have an arraylist, you have a string which says 'System.Collections.ArrayList'. I doubt you can shove arrays into a cell of a table. Christian Graus - Microsoft MVP - C++

        S Offline
        S Offline
        serious sam
        wrote on last edited by
        #3

        With the help of a workmate, I've resolved the issue. The quirk was in the Add method of the DataColumnCollection class (which holds the cells for the rows). The writers of the method, in their infinite wisdom, convert the input value to a string before adding it. This is default behaviour. It can be overridden by creating a custom DataColumn to hold the type of the input data. I'm including the code which now works. using System; using System.Data; using System.Collections; class CastTest { const string COL = "test"; static void Main() { Console.WriteLine("CastTest"); ArrayList list = new ArrayList(); string str = "Hello world"; list.Add(str); Console.WriteLine("arrayList created"); DataTable table = new DataTable(); DataColumn dC = new DataColumn(COL, list.GetType()); table.Columns.Add(dC); DataRow row = table.NewRow(); table.Rows.Add(row); row[COL] = list; Console.WriteLine("table created"); object resultObj = table.Rows[0][COL]; Console.WriteLine("resultObj type: " + resultObj.GetType()); ArrayList resultList = (ArrayList) resultObj; string resultStr = (string)resultList[0]; Console.WriteLine(resultStr); } } I guess I should be reading the documentation more closely so that I would be more aware of "features" like this ;) Thanks for the replies. Regards, Michael

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups