473,404 Members | 2,179 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,404 software developers and data experts.

Trouble referencing a series of pre-defined arrays within a loop.

I have a Visual Basic (2005) project that contains around twenty
embedded text files as resources. The text files contain two columns
of real numbers that are separated by tab deliminator, and are of
different lengths (e.g. usually between 25 and 45 rows. The columns in
each file have the same length). The text files have been numbered
sequentially e.g. cb0, cb1, cb2 and so on. I would like to read the
data from each text file into separate arrays. I am declaring the
arrays so that they are private to the module I am currently working
in. It is important that I do this rather than pass the arrays to
other procedures from my current procedure. This is because many of
the procedures and functions requiring the information in these arrays
are called over 1000000 times in a Monte Carlo simulation. My arrays
have also been named sequentially so that they complement the
filenames e.g. cbArr0, cbArr1, cbArr2 etc.

I can successfully loop through each file and read each of their rows
one by one into a string which is subsequently split into a 1D array.
If I only worry about one file (cb0) I can copy each of the split
values in the 1D array into a dynamic array (cbarr0) that is increased
in size using a loop counter for every line of the file. My problem
occurs when I want to use all my text files and reference the arrays
with a loop counter. I need to create some sort of reference to the
arrays. I have tried various methods that I extracted from a Wrox book
e.g. creating an array list, creating a stack etc. but I can't seem
toget any of them to work. I'm sure there is a very simple solution to
the problem. I am new to vb and inexperienced in a lot of programming
concepts. I can make the code work if I use a case statement but that
is terribly inefficient as it is required for every line of data in
each of the text files. There must be a simpler way that works.

An example of my code that doesn't work is shown below. The code runs
but doesn't work properly. Whilst debugging I can see that the values
from the split string are being stored in the array list and not the
original arrays. They then get over-written by the next line. The re-
dim doesn't seem to change anything.

For clarity 3 files and 3 arrays have been used in the code example.
Option Explicit On
'Option Strict On
Imports System.IO
'Imports System.Collections.Generic

Public Class Form1

Private cbArr0(1, 0), cbArr1(1, 0), cbArr2(1, 0) As Single

Sub generateCB data

'Variable declarations
Dim isoData As String
Dim isoFile As Integer
Dim lineOfData As String
Dim splitLineOfData() As String
Dim lineCount As Integer

'Creates a list allowing code to loop through embedded files
Dim fileList(0 To 1) As String
fileList(0) = My.Resources.cb0
fileList(1) = My.Resources.cb1
fileList(1) = My.Resources.cb2

Dim arrList As New ArrayList
arrList.Add(cbArr0)
arrList.Add(cbArr1)
arrList.Add(cbArr2)

For isoFile = 0 To 2 'Loops through embedded files

isoData = fileList(isoFile) 'reads isochrone file into a
string
Dim string_reader As New StringReader(isoData.ToString)
lineCount = 0

While True
lineOfData = string_reader.ReadLine 'Reads one line of
string at a time
If lineOfData Is Nothing Then
Exit While
Else
splitLineOfData = Split(lineOfData, Chr(9), )
'Splits two columns into 1D array
ReDim Preserve arrList(isoFile)(1, lineCount)
arrList(isoFile)(0, lineCount) =
CSng(splitLineOfData(0))
arrList(isoFile)(1, lineCount) =
CSng(splitLineOfData(1))
lineCount += 1
End If
End While

Next isoFile

End Sub
End Class
Thank you for you time.
DJW

Apr 19 '07 #1
1 2423
Doug wrote:
<backposted/>

There are a few problems with your approach (not to mention the
apparent mistypings on lines 20 and 23: Dim fileList(0 To 1) As
String: ... : fileList(1) = My.Resources.cb2 -- those "1"s should be
"2"s, I guess).

The major problem is that you're resorting to ArrayList. An ArrayList
isn't, as its name seems to suggest, a list of arrays. It's a list
that behaves like an array. Don't use it. Ever. It's the reason you
have to disable Option Strict -- always a bad thing to do, when you're
not explicitly using late bind.

In the following lines, copies of your arrays are added to the
ArrayList, not your actual arrays:
arrList.Add(cbArr0)
arrList.Add(cbArr1)
arrList.Add(cbArr2)
Therefore, later, when you do
ReDim Preserve arrList(isoFile)(1, lineCount)
two things happen: First, the code is compiled as late bound code --
VB doesn't know the type of the element in arrList(isoFile), so it
builds less efficient code than what would get compiled if an explicit
array was used. Second, since the array inside arrList is not your
original array, but a copy of it (actually an Object array, not a
Double array), *that* is what get's resized, not your original array.

So, ArrayList is your enemy, don't use it. If you need a dynamic list,
use the generic list instead:

''declares a strongly typed, dynamic list of Double arrays
Dim arrList As New List(Of Double( ) )
Also, if you're concerned with efficiency, don't use a Redim Preserve
inside a loop. Instead, redim the array at one fell swoop *before* the
loop.

Finally, notice that when you Redim an array (preserve or not) VB
creates a new object and assigns it to the array variable. Other
variables will still reference the previous object -- and be out of
synch with the Redim'ed variable.

Without changing your approach too much, this is how your code might
look like:

<aircode>
For isoFile = 0 To 2 'Loops through embedded files
Dim Lines() As String = fileList(isoFile).Split(ControlChars.CrLf)
Dim List(0 To 1, 0 To Lines.Length - 1) As Single

Dim Index As Integer = 0 'resets the line count

For Each LineOfData In Lines
If LineOfData IsNot Nothing Then
splitLineOfData = Split(lineOfData, ControlChars.Tab)
List(0, Index) = CSng(splitLineOfData(0))
List(1, Index) = CSng(splitLineOfData(1))
End If
Index += 1
Next 'LineOfData

Select Case isoFile
Case 0 : cbArr0 = List
Case 1 : cbArr1 = List
Case 2 : cbArr2 = List
'etc, etc, etc
End Select

Next isoFile
</aircode>

HTH.

Regards,

Branco.

I have a Visual Basic (2005) project that contains around twenty
embedded text files as resources. The text files contain two columns
of real numbers that are separated by tab deliminator, and are of
different lengths (e.g. usually between 25 and 45 rows. The columns in
each file have the same length). The text files have been numbered
sequentially e.g. cb0, cb1, cb2 and so on. I would like to read the
data from each text file into separate arrays. I am declaring the
arrays so that they are private to the module I am currently working
in. It is important that I do this rather than pass the arrays to
other procedures from my current procedure. This is because many of
the procedures and functions requiring the information in these arrays
are called over 1000000 times in a Monte Carlo simulation. My arrays
have also been named sequentially so that they complement the
filenames e.g. cbArr0, cbArr1, cbArr2 etc.

I can successfully loop through each file and read each of their rows
one by one into a string which is subsequently split into a 1D array.
If I only worry about one file (cb0) I can copy each of the split
values in the 1D array into a dynamic array (cbarr0) that is increased
in size using a loop counter for every line of the file. My problem
occurs when I want to use all my text files and reference the arrays
with a loop counter. I need to create some sort of reference to the
arrays. I have tried various methods that I extracted from a Wrox book
e.g. creating an array list, creating a stack etc. but I can't seem
toget any of them to work. I'm sure there is a very simple solution to
the problem. I am new to vb and inexperienced in a lot of programming
concepts. I can make the code work if I use a case statement but that
is terribly inefficient as it is required for every line of data in
each of the text files. There must be a simpler way that works.

An example of my code that doesn't work is shown below. The code runs
but doesn't work properly. Whilst debugging I can see that the values
from the split string are being stored in the array list and not the
original arrays. They then get over-written by the next line. The re-
dim doesn't seem to change anything.

For clarity 3 files and 3 arrays have been used in the code example.

Option Explicit On
'Option Strict On
Imports System.IO
'Imports System.Collections.Generic

Public Class Form1

Private cbArr0(1, 0), cbArr1(1, 0), cbArr2(1, 0) As Single

Sub generateCB data

'Variable declarations
Dim isoData As String
Dim isoFile As Integer
Dim lineOfData As String
Dim splitLineOfData() As String
Dim lineCount As Integer

'Creates a list allowing code to loop through embedded files
Dim fileList(0 To 1) As String
fileList(0) = My.Resources.cb0
fileList(1) = My.Resources.cb1
fileList(1) = My.Resources.cb2

Dim arrList As New ArrayList
arrList.Add(cbArr0)
arrList.Add(cbArr1)
arrList.Add(cbArr2)

For isoFile = 0 To 2 'Loops through embedded files

isoData = fileList(isoFile) 'reads isochrone file into a
string
Dim string_reader As New StringReader(isoData.ToString)
lineCount = 0

While True
lineOfData = string_reader.ReadLine 'Reads one line of
string at a time
If lineOfData Is Nothing Then
Exit While
Else
splitLineOfData = Split(lineOfData, Chr(9), )
'Splits two columns into 1D array
ReDim Preserve arrList(isoFile)(1, lineCount)
arrList(isoFile)(0, lineCount) =
CSng(splitLineOfData(0))
arrList(isoFile)(1, lineCount) =
CSng(splitLineOfData(1))
lineCount += 1
End If
End While

Next isoFile

End Sub
End Class

Thank you for you time.
DJW

Apr 20 '07 #2

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

Similar topics

0
by: melledge | last post by:
The Reliable Source for Everything XML - XML 2005 Update XML 2005 - November 14-18 - Atlanta Hilton Hotel - Atlanta, GA www.xmlconference.org Register today and participate in IDEAlliance's...
47
by: shan | last post by:
Hi to every body, I am a begginer in C.can anybody give the complete code for fibinocci series (0 1 1 2 3 5 8 ...)an input is get from user to calculate how many numbers to be printed.I am using...
3
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
3
by: drum2001 | last post by:
I currently have a form "VIEW ALL ASSIGNMENTS" containing a subform with record source "qryVIEWALLASSIGNMENTS. This query filters the database based on two combo boxes on the form containing dates...
8
by: Dale | last post by:
I have created a class that implements IHttpModule and I want to use the module on several of the web sites that exist on my server. The module works great if I place the dll in a web applications...
1
by: Sockpuppet | last post by:
After having googles I've knocked together ther following code. What I am trying to do is select all items in one table and move through them adding them into other tables (splitting them and...
1
by: David | last post by:
Hi All, I'm having trouble accessing multiple databases using a single user. Basically, I have 2 databases, db1 and db2 and 2 users, user1 and user2. Originally these were setup so that user1...
7
by: apattin | last post by:
Hi experts, I am having a hard time coding a trigger to handle multiple validation conditions in DB2 UDB V8.1.9.. I thought that a single trigger might be better than one-trigger-per-condition,...
10
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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
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,...
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.