473,498 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get around Array reference values

I want to convert each record read (with a datareader) to a string
array and then place each of those arrays into another array (sResult)

The problem here is that when the inner loop begins to process the 2nd
record read (from the outer loop) & for example: iRowCnt ==1), the
values in sResult[0] are updated to reflect the new values being
placed into sRow.

OK, so I take it that when storing sRow into sResult, I am storing a
reference to sRow. The question is, how do I store the actual values
from each individual row into sResult?

string[][] sResult = new String[30][];
string[] sRow = new String[20];

//loop thru data reader
while(LocalReader.Read())
{
cnt=0;
iReaderLength = LocalReader.FieldCount;
while (cnt < iReaderLength)
{
sRow[cnt] = LocalReader.GetValue(cnt).ToString();
cnt++;
}
sResult[iRowCnt]=sRow;
iRowCnt++;
}

Hope this is clear
Jeff
Nov 17 '05 #1
2 1225
Just move the statement where you create the new sRow, to create a new
one each time around the loop:

string[][] sResult = new String[30][];

//loop thru data reader
while(LocalReader.Read())
{
cnt=0;
iReaderLength = LocalReader.FieldCount;
string[] sRow = new String[iReaderLength];
while (cnt < iReaderLength)
{
sRow[cnt] = LocalReader.GetValue(cnt).ToString();
cnt++;
}
sResult[iRowCnt]=sRow;
iRowCnt++;
}
As well, you probably want to make sResult an ArrayList, then convert
it to an array at the end, so you never run over your 30 allocated
rows, and you never use less than 30 and leave nulls:

ArrayList sResult = new ArrayList();

//loop thru data reader
while(LocalReader.Read())
{
cnt=0;
iReaderLength = LocalReader.FieldCount;
string[] sRow = new String[iReaderLength];
while (cnt < iReaderLength)
{
sRow[cnt] = LocalReader.GetValue(cnt).ToString();
cnt++;
}
sResult.Add(sRow);
}

string[][] sResultArray =
(string[][])sResult.ToArray(typeof(string[]));

Now sResultArray will contain exactly the number of sRows that you read
in.

Nov 17 '05 #2
Excellent !!!

Thanks
Jeff

On 15 Nov 2005 22:33:20 -0800, "Bruce Wood" <br*******@canada.com>
wrote:
Just move the statement where you create the new sRow, to create a new
one each time around the loop:

string[][] sResult = new String[30][];

//loop thru data reader
while(LocalReader.Read())
{
cnt=0;
iReaderLength = LocalReader.FieldCount;
string[] sRow = new String[iReaderLength];
while (cnt < iReaderLength)
{
sRow[cnt] = LocalReader.GetValue(cnt).ToString();
cnt++;
}
sResult[iRowCnt]=sRow;
iRowCnt++;
}
As well, you probably want to make sResult an ArrayList, then convert
it to an array at the end, so you never run over your 30 allocated
rows, and you never use less than 30 and leave nulls:

ArrayList sResult = new ArrayList();

//loop thru data reader
while(LocalReader.Read())
{
cnt=0;
iReaderLength = LocalReader.FieldCount;
string[] sRow = new String[iReaderLength];
while (cnt < iReaderLength)
{
sRow[cnt] = LocalReader.GetValue(cnt).ToString();
cnt++;
}
sResult.Add(sRow);
}

string[][] sResultArray =
(string[][])sResult.ToArray(typeof(string[]));

Now sResultArray will contain exactly the number of sRows that you read
in.


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
13998
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
12
55523
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
10195
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
8
4478
by: Kenneth Baltrinic | last post by:
I am trying to compare values coming out of a database record with known default values. The defaults are in an array of type object (because they can be of any basic data type, I am not working...
5
2250
by: Mircea Dragan | last post by:
Hi, I have a problem. Here is the description: I have a dll which I call from a C# program. The C# program passes an array of doubles to the dll function. Everything works fine as long as I...
4
9668
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
4
1649
by: mathieu | last post by:
Hello, I would like implement a nice way to work around the array of references issue in C++. What do usually people do ? Do you maintain a separate factory/pool of elements as part of the API ?...
14
20365
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
5
2039
by: barcrofter | last post by:
I have two independent arrays and mysteriously they occupy the same space. Is this an error or a feature? Dim tokens$() '(input list of msn's and constants) Dim values() As...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7168
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,...
0
7210
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...
1
6891
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...
1
4916
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...
0
4595
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...
0
3096
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...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.