By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

ArrayList: Getting "System.String[]" Instead of Actual Values

P: 21
Hi,

I'm new to C#, please be patient :-) I am trying to pass an Arraylist (myAL) to a jagged array (myAL2) . The problem I have is that when I write the values to a file i do not get the values instead I get "System.String[]" ? How can I fix the problem? I wanted to use ArralyList because its length is flexible and I can add values to it.

...All help will be greatly appreciated.

Here's my code:


Expand|Select|Wrap|Line Numbers
  1. //create jagged array
  2.  
  3. string[][] myAL2;
  4.  
  5. // set dimension 1's length
  6.  
  7. myAL2 = new string[myAL.Count][];
  8.  
  9. // set dimensions 2 oj jagged array
  10.  
  11. for (int m = 0; m < myAL.Count; m++)
  12.  
  13. {
  14.  
  15. //here i just set D2 to 19 as a temporary solution as I don't know yet how to get the length of the ArrayList Counts....
  16.  
  17. myAL2[m] = new string[19];
  18.  
  19. //read through myAL ArrayList and passing arrays to 2D jagged array list
  20.  
  21. for (int ix = 0; ix < myAL.Count; ++ix)
  22.  
  23. {
  24.  
  25. // This is causing a problem by not giving me the value
  26.  
  27. myAL2[ix][n] = Convert.ToString(myAL[ix]);
  28.  
  29. }
  30.  
  31. }
May 10 '07 #1
Share this Question
Share on Google+
10 Replies


kenobewan
Expert 2.5K+
P: 4,871
Are you using .net 2.0? This can be a reversion to a default schema.
May 10 '07 #2

P: 21
I am indeed using .Net 2.0

This can be a reversion to a default schema.
How can I fix the problem?
May 10 '07 #3

P: 4
myAL2[m][ix] = Convert.ToString(myAL[ix]);

actually you are misplacing the variables in the for loop. I just changed the places of variables. If the arraylist contains only strings then you can directly use myAL[ix].ToString(); function to get that.

Instead of jagged array you can use ArrayList of ArrayList.

Hope this solves ur problem.
May 10 '07 #4

P: 21
myAL2[m][ix] = Convert.ToString(myAL[ix]);

actually you are misplacing the variables in the for loop. I just changed the places of variables. If the arraylist contains only strings then you can directly use myAL[ix].ToString(); function to get that.

Instead of jagged array you can use ArrayList of ArrayList.

Hope this solves ur problem.
Hi,

variable m=19 represents the number of columns
ix represents the number of rows.
So they should be as I put them.


Maybe my problem comes down to this:

Expand|Select|Wrap|Line Numbers
  1.  myAL2[ix][n] = myAL[ix].ToString();
as it stores in myAL2 "System.String[ ]" instead of the actual values..

(NB myAl ArrayList has both integers and strings. )

Any other idea?
May 10 '07 #5

P: 4
can you post entire code since the declaration and the assignments are imp so i need to know. WHen i tried taking simple string it worked with the change i mentioned. so post the entire code then may be all others also can help you.
May 10 '07 #6

P: 21
can you post entire code since the declaration and the assignments are imp so i need to know. WHen i tried taking simple string it worked with the change i mentioned. so post the entire code then may be all others also can help you.


Ok, here is all of the code, it is not pretty though...


(reference Microsoft Excel 11.0 Object Library in COM has been added)

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.Office.Interop.Excel;
  5. using System.IO;
  6. using System.Collections;
  7. using System.ComponentModel;
  8. using System.Data;
  9.  
  10. namespace ConsApp_ArrayList_eXCEL_10MAY
  11. {
  12.     class Program
  13.     {
  14.  
  15.  
  16.         static public string[] ConvertToStringArray(System.Array values)
  17.         {
  18.             // create a new string array
  19.             //string[] theArray = new string[values.Length];
  20.             ArrayList theArray = new ArrayList();
  21.  
  22.             //loop through the 2-D System.Array and populate the 1-D String Array
  23.             for (int i = 1; i <= values.Length; i++)
  24.             {
  25.                 if (values.GetValue(1, i) == null)
  26.                     theArray.Add("");
  27.                 else
  28.                     theArray.Add((string)values.GetValue(1, i).ToString());
  29.             }
  30.             // add 2 additional holders as I will add 2 values per row later
  31.             theArray.Add("");
  32.             theArray.Add("");
  33.  
  34.             return (string[])theArray.ToArray(typeof(string));
  35.  
  36.         }
  37.  
  38.  
  39.  
  40.         static void Main(string[] args)
  41.         {
  42.             //Write result to a file
  43.             FileStream fs = new FileStream("RMReportx_UPDATED.txt", FileMode.OpenOrCreate, FileAccess.Write);
  44.             StreamWriter sWriter = new StreamWriter(fs);
  45.             Microsoft.Office.Interop.Excel.Application ExcelObj = null;
  46.             //InitializeComponent();
  47.             ExcelObj = new Microsoft.Office.Interop.Excel.Application();
  48.  
  49.             // create Jagged array
  50.             string[][] jaggedA = new string[238][];
  51.  
  52.  
  53.             // read excel file where the data is comming from
  54.             Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open("C:/Documents and Settings/zzeghoud/My Documents/RMReportx.xls", 0, true, 5,
  55.                   "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
  56.             // get the collection of sheets in the workbook
  57.             Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
  58.             // get the first and only worksheet from the collection of worksheets
  59.             Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
  60.  
  61.  
  62.             // create ArrayList to hold the sheet's data
  63.             ArrayList myAL = new ArrayList();
  64.  
  65.             // read file from line 8 to line 247
  66.             for (int i = 8; i < 247; i++) // i should increase until it reaches a row with null
  67.             // 3384, 247 is an arbitrary number of rows (should cover max number of rows in Excel sheet
  68.             //odeally the number of row should be counted beforehand and then passed
  69.             {
  70.                 Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A" + i.ToString(), "q" + i.ToString());
  71.                 System.Array myvalues = (System.Array)range.Cells.Value2;
  72.                 string[] strArray = ConvertToStringArray(myvalues);
  73.                 //add data to the myAL arrayList
  74.                 myAL.Add(ConvertToStringArray(myvalues));
  75.  
  76.  
  77.  
  78.  
  79.             }
  80.  
  81.             //create jagged array
  82.             string[][] myAL2;
  83.             // set dimension 1's length
  84.             myAL2 = new string[myAL.Count][];
  85.             //create (temp) string array for the columns
  86.             string[] myALChild = new string[19];
  87.  
  88.             //string str;
  89.  
  90.             // set dimensions 2 oj jagged array
  91.             for (int m = 0; m < myAL.Count; m++)
  92.             {
  93.                 myAL2[m] = new string[19];
  94.  
  95.             }
  96.  
  97.  
  98.  
  99.             //read through myAL ArrayList and assing arrays to 2D jagged array list
  100.             for (int ix = 0; ix < myAL.Count; ix++)
  101.             {
  102.                 for (int n = 0; n < 19; n++)
  103.                 {
  104.                    myAL2[n][ix] = Convert.ToString(myAL[ix]);
  105.                  }
  106.             }
  107.  
  108.  
  109.             // write results to file
  110.             for (int ix = 0; ix < myAL.Count; ix++)
  111.             {
  112.                 for (int n = 0; n < 19; n++)
  113.                 {
  114.                     sWriter.WriteLine(myAL2[ix][n].ToString());
  115.  
  116.                 }
  117.             }
  118.  
  119.             Console.WriteLine(" The Text file named RMReportx_UPDATED.txt is created ");
  120.             if (sWriter != null) { sWriter.Close(); }
  121.             Console.ReadLine();
  122.  
  123.  
  124.         }
  125.  
  126.     }
  127. }

Thanks again,
May 10 '07 #7

P: 4
Hi,

The problem here i see is while assigning the values to jagged array you are using

for (int ix = 0; ix < myAL.Count; ix++)
{
for (int n = 0; n < 19; n++)
{
myAL2[n][ix] = Convert.ToString(myAL[ix]);
}
}

while getting back values your have made change in variables.. there is the problem since jagged array do not have same number of rows and columns when you write into file it gives u object.

Try to make the same adjusment in other for loop and then see the difference.

The change of variables i told u will solve the problem.
Here is simple code i wrote just to see whats happening....
may this will help you..


//create jagged array
string sMSG = "If you smell what the Rock is Cooking !";
ArrayList myAL = new ArrayList();
string[] arr = sMSG.Split(' ');

for (int i = 0; i < arr.Length; i++)
{
myAL.Add(arr[i]);
}
string[][] myAL2;

// set dimension 1's length

myAL2 = new string[myAL.Count][];

// set dimensions 2 oj jagged array

for (int m = 0; m < myAL.Count; m++)
{
//here i just set D2 to 19 as a temporary solution as I don't know yet how to get the length of the ArrayList Counts....

myAL2[m] = new string[9];

//read through myAL ArrayList and passing arrays to 2D jagged array list

for (int ix = 0; ix < myAL.Count; ++ix)
{
// This is causing a problem by not giving me the value
myAL2[m][ix] = Convert.ToString(myAL[ix]);
Console.WriteLine(Convert.ToString(myAL[ix]));
}
}
May 10 '07 #8

P: 21
The change of variables i told u will solve the problem.
Here is simple code i wrote just to see whats happening....
may this will help you..

Hi,

Thank you for the code you wrote, I tried to adapt it to my problem (see below) however I still get "System.String[ ]" and not the actual value???
What am I missing?...



Expand|Select|Wrap|Line Numbers
  1.  
  2.       {     ...
  3.            System.Array myvalues = (System.Array)range.Cells.Value2;
  4.            myAL.Add(ConvertToStringArray(myvalues));
  5.        }
  6.  
  7.            string[][] myAL2;
  8.  
  9.             // set dimension 1's length
  10.  
  11.             myAL2 = new string[myAL.Count][];
  12.  
  13.             // set dimensions 2 oj jagged array
  14.  
  15.             for (int m = 0; m < myAL.Count; m++)
  16.             {
  17.                 //here i just set D2 to 19 as a temporary solution as I don't know yet how to get the length of the ArrayList Counts....
  18.  
  19.                 myAL2[m] = new string[19];
  20.  
  21.                 //read through myAL ArrayList and passing arrays to 2D jagged array list
  22.  
  23.                 for (int ix = 0; ix < myAL.Count; ++ix)
  24.                 {
  25.                     // This is causing a problem by not giving me the value
  26.                     myAL2[m][ix] = Convert.ToString(myAL[ix]);
  27.                     Console.WriteLine(Convert.ToString(myAL[ix]));
  28.                 }
  29.  
  30.             }
Again, thank you for your help.
May 10 '07 #9

P: 21
Thanks a Million, I have sorted it out thanks to you!



Expand|Select|Wrap|Line Numbers
  1. for (int i = 8; i < 247; i++)            
  2. // 247 is an arbitrary number of rows (should cover max number of rows in //Excel sheet
  3.     {
  4.       string[] strArray = ConvertToStringArray(myvalues);
  5.  
  6.       for (int v = 0; v < strArray.Length; v++)
  7.                 {
  8.                     myAL.Add(strArray[v]);
  9.                 }
  10.  
  11.     }
I only have to work on this code now, as I need to get the values per row.


Thanks again you have been extremely helpful and patient!
May 10 '07 #10

P: 21
Hi, I'm new to C#. I have an Arralist (myList) and an array of ArrayList (myAL) I need to write to a file but for some reason I am getting an error message:

Unable to cast object of type 'System.String[]' to type 'System.String'.

I would greatly appreciate if someone could guide me on the proper syntax to write the data of those 2 arraylist to the console???

Expand|Select|Wrap|Line Numbers
  1. ArrayList myList = new ArrayList();
  2. for (int v = 0; v < anotherArray.Length; v++)   {  
  3. myList.Add(anotherArray[v]);   }
  4.  
  5.  
  6. ArrayList[] myAL = new ArrayList[2] { new ArrayList(), new ArrayList() };
  7. //myList is added several times to myAL, each time with new values
  8. myAL[0].Add(myList);
Thank you for your help,
May 11 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.