Weird class cast issue
-
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 -
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, MichaelI 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++
-
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++
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