473,626 Members | 3,231 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

21 New Member
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.Str ing[]" ? 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 7537
kenobewan
4,871 Recognized Expert Specialist
Are you using .net 2.0? This can be a reversion to a default schema.
May 10 '07 #2
R2d2Rabeau
21 New Member
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
rock7799
4 New Member
myAL2[m][ix] = Convert.ToStrin g(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
R2d2Rabeau
21 New Member
myAL2[m][ix] = Convert.ToStrin g(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.Str ing[ ]" instead of the actual values..

(NB myAl ArrayList has both integers and strings. )

Any other idea?
May 10 '07 #5
rock7799
4 New Member
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
R2d2Rabeau
21 New Member
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
rock7799
4 New Member
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.ToStrin g(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.ToStrin g(myAL[ix]);
Console.WriteLi ne(Convert.ToSt ring(myAL[ix]));
}
}
May 10 '07 #8
R2d2Rabeau
21 New Member
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.Str ing[ ]" 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
R2d2Rabeau
21 New Member
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

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

Similar topics

0
1474
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 XsdObjectGen? or a problem with the input schema? I'm trying to avoid hacking the generated code... In: <?xml version = "1.0" encoding = "UTF-8"?> <xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3
1829
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 inserted into the variable is "System. String" and I don't understand what happened to my original value in the key/value pair. Thanks for your help,
7
6331
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 that 103 displays before 99. Is there a way to write the SQL query to order them numerically? This would be much easier for me than changing the data type and hunting down every page that INSERTS or UPDATES the db. Thanks, Giles
2
1273
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
1116
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 the net, and found no clue. Has anyone seen similar problem, or any clue?
0
2669
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 is named "Try". The VB script function accessing the record value is running on a web hosting server with ASP.Net 2.0 on a web page. Result always is it "can't convert from a Field to type 'String'". Printing out the SQL variable results in...
1
1791
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 samples will be sent to a lab for analysis and the lab will send back a report (usually in excel or .txt format), that data will be imported into Access. If the concentration of a heavy metal is lower than the detectable limit of the analysis, the...
1
1551
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 System.Net Imports System.Net.IPAddress
4
11278
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 object of type 'System.String' to type 'System.String'."}
0
8269
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8368
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8512
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7203
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5576
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.