473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
10 7501
kenobewan
4,871 Expert 4TB
Are you using .net 2.0? This can be a reversion to a default schema.
May 10 '07 #2
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
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
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
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
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
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
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
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
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

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

Similar topics

0
by: Andrew Burgher | last post by:
Feeding the following .xsd into the XsdObjectGen (v1.4.2.0) tool produces an invalid attribute: with DataType="System.String". Has anybody seen this behaviour before? Is this a bug in...
3
by: Andy Sutorius via DotNetMonster.com | last post by:
Hi, I have a Sorted List that has 9 key/value pairs. I am trying to take one of the key/value pairs and store the value of that key into a string variable. However, the value that actually gets...
7
by: Giles | last post by:
An ASP page outputs data from the query "Select ThisAndThat from comments WHERE pageURL='" & pageURL & "' ORDER BY threadID, datesent" (Access mdb) threadID is a string (OK, I know!), which means...
2
by: jiabin | last post by:
Hi, I got an exception with the message below, when I'm trying to start my application. "Could not load a type. Failed to partially bind to "System.String, mscorlib". "
0
by: jiabin | last post by:
Hi, I got an exception with the message below, when I'm trying to start my application. "Could not load a type. Failed to partially bind to "System.String, mscorlib". " I did a search on...
0
by: woollymammoth | last post by:
I can't assign a MS SQL Server table record value to a simple VB variable, should be an easy thing. Sample SQL Server table has the data in the record as a char(30) string, the column for that record...
1
by: Ducknut | last post by:
Not so much a problem as a discussion. I am currently in the early stages of designing a database to hold a bunch of water quality data (e.g., concentrations of heavy metals in drinking water). Water...
1
by: mcarten | last post by:
I'm getting the dreaded "nullreferenceexception" error with this code. In an ASP application it works fine. In VB Express 2005 it does not. These are my imports .... Imports System Imports...
4
by: archu007 | last post by:
hi i'm using the following statement in my application string strTArray = new string; strTArray = (string)(Session); when i run the application its giving the below error {"Unable to cast...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.