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: -
//create jagged array
-
-
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[19];
-
-
//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[ix][n] = Convert.ToString(myAL[ix]);
-
-
}
-
-
}
10 7537
Are you using .net 2.0? This can be a reversion to a default schema.
I am indeed using .Net 2.0
This can be a reversion to a default schema.
How can I fix the problem?
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.
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: - 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?
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.
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) - using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using Microsoft.Office.Interop.Excel;
-
using System.IO;
-
using System.Collections;
-
using System.ComponentModel;
-
using System.Data;
-
-
namespace ConsApp_ArrayList_eXCEL_10MAY
-
{
-
class Program
-
{
-
-
-
static public string[] ConvertToStringArray(System.Array values)
-
{
-
// create a new string array
-
//string[] theArray = new string[values.Length];
-
ArrayList theArray = new ArrayList();
-
-
//loop through the 2-D System.Array and populate the 1-D String Array
-
for (int i = 1; i <= values.Length; i++)
-
{
-
if (values.GetValue(1, i) == null)
-
theArray.Add("");
-
else
-
theArray.Add((string)values.GetValue(1, i).ToString());
-
}
-
// add 2 additional holders as I will add 2 values per row later
-
theArray.Add("");
-
theArray.Add("");
-
-
return (string[])theArray.ToArray(typeof(string));
-
-
}
-
-
-
-
static void Main(string[] args)
-
{
-
//Write result to a file
-
FileStream fs = new FileStream("RMReportx_UPDATED.txt", FileMode.OpenOrCreate, FileAccess.Write);
-
StreamWriter sWriter = new StreamWriter(fs);
-
Microsoft.Office.Interop.Excel.Application ExcelObj = null;
-
//InitializeComponent();
-
ExcelObj = new Microsoft.Office.Interop.Excel.Application();
-
-
// create Jagged array
-
string[][] jaggedA = new string[238][];
-
-
-
// read excel file where the data is comming from
-
Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open("C:/Documents and Settings/zzeghoud/My Documents/RMReportx.xls", 0, true, 5,
-
"", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
-
// get the collection of sheets in the workbook
-
Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
-
// get the first and only worksheet from the collection of worksheets
-
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
-
-
-
// create ArrayList to hold the sheet's data
-
ArrayList myAL = new ArrayList();
-
-
// read file from line 8 to line 247
-
for (int i = 8; i < 247; i++) // i should increase until it reaches a row with null
-
// 3384, 247 is an arbitrary number of rows (should cover max number of rows in Excel sheet
-
//odeally the number of row should be counted beforehand and then passed
-
{
-
Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A" + i.ToString(), "q" + i.ToString());
-
System.Array myvalues = (System.Array)range.Cells.Value2;
-
string[] strArray = ConvertToStringArray(myvalues);
-
//add data to the myAL arrayList
-
myAL.Add(ConvertToStringArray(myvalues));
-
-
-
-
-
}
-
-
//create jagged array
-
string[][] myAL2;
-
// set dimension 1's length
-
myAL2 = new string[myAL.Count][];
-
//create (temp) string array for the columns
-
string[] myALChild = new string[19];
-
-
//string str;
-
-
// set dimensions 2 oj jagged array
-
for (int m = 0; m < myAL.Count; m++)
-
{
-
myAL2[m] = new string[19];
-
-
}
-
-
-
-
//read through myAL ArrayList and assing arrays to 2D jagged array list
-
for (int ix = 0; ix < myAL.Count; ix++)
-
{
-
for (int n = 0; n < 19; n++)
-
{
-
myAL2[n][ix] = Convert.ToString(myAL[ix]);
-
}
-
}
-
-
-
// write results to file
-
for (int ix = 0; ix < myAL.Count; ix++)
-
{
-
for (int n = 0; n < 19; n++)
-
{
-
sWriter.WriteLine(myAL2[ix][n].ToString());
-
-
}
-
}
-
-
Console.WriteLine(" The Text file named RMReportx_UPDATED.txt is created ");
-
if (sWriter != null) { sWriter.Close(); }
-
Console.ReadLine();
-
-
-
}
-
-
}
-
}
Thanks again,
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]));
}
}
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?... -
-
{ ...
-
System.Array myvalues = (System.Array)range.Cells.Value2;
-
myAL.Add(ConvertToStringArray(myvalues));
-
}
-
-
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[19];
-
-
//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]));
-
}
-
-
}
Again, thank you for your help.
Thanks a Million, I have sorted it out thanks to you! - for (int i = 8; i < 247; i++)
-
// 247 is an arbitrary number of rows (should cover max number of rows in //Excel sheet
-
{
-
string[] strArray = ConvertToStringArray(myvalues);
-
-
for (int v = 0; v < strArray.Length; v++)
-
{
-
myAL.Add(strArray[v]);
-
}
-
-
}
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!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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">
|
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,
|
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
|
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". "
|
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?
| |
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...
|
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...
|
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
|
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'."}
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
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
| |