473,543 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.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.cb 0
fileList(1) = My.Resources.cb 1
fileList(1) = My.Resources.cb 2

Dim arrList As New ArrayList
arrList.Add(cbA rr0)
arrList.Add(cbA rr1)
arrList.Add(cbA rr2)

For isoFile = 0 To 2 'Loops through embedded files

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

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

Next isoFile

End Sub
End Class
Thank you for you time.
DJW

Apr 19 '07 #1
1 2434
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.cb 2 -- 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(cbA rr0)
arrList.Add(cbA rr1)
arrList.Add(cbA rr2)
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(isoFil e).Split(Contro lChars.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(lineOfDat a, ControlChars.Ta b)
List(0, Index) = CSng(splitLineO fData(0))
List(1, Index) = CSng(splitLineO fData(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.Collecti ons.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.cb 0
fileList(1) = My.Resources.cb 1
fileList(1) = My.Resources.cb 2

Dim arrList As New ArrayList
arrList.Add(cbA rr0)
arrList.Add(cbA rr1)
arrList.Add(cbA rr2)

For isoFile = 0 To 2 'Loops through embedded files

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

While True
lineOfData = string_reader.R eadLine 'Reads one line of
string at a time
If lineOfData Is Nothing Then
Exit While
Else
splitLineOfData = Split(lineOfDat a, Chr(9), )
'Splits two columns into 1D array
ReDim Preserve arrList(isoFile )(1, lineCount)
arrList(isoFile )(0, lineCount) =
CSng(splitLineO fData(0))
arrList(isoFile )(1, lineCount) =
CSng(splitLineO fData(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
1139
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 XML 2005 Conference, the most respected educational gathering of technologists, novices, experienced implementers and users, consultants, and...
47
4511
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 turbo c++. thank U in advance
3
4975
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 : >************************************************************************ > <WebMethod(), System.Web.Services.Protocols.SoapRpcMethod()> _ > Public Function...
3
1331
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 ( & ) The idea is: "Please show me all assignments from through . Here is my SQL Code for that:
8
1380
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 bin directory. However, I can’t get it to work at all from the GAC. If I remove the dll from the web applications bin file, I get the following...
1
1960
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 putting different parts in different table linked together with relationships). THe bits that I can't figure out are: 1) how to get info from the...
1
1336
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 would only access db1 and user2 would only access db2. However, now I want user1 to access both db1 & db2. I've used cPanel to declare user1 as a...
7
8978
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, but so far, no cigar. Here is what I'd like to do (pseudo-code): CREATE TRIGGER fp_insert NO CASCADE BEFORE INSERT ON DB2INST1.FILE_PROPERTY
10
8132
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 and excel is closed. But there are some commented lines: //xlSeries.XValues = xlWs.get_Range("B2", "B4"); // makes com objects, but which...
0
7584
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. ...
0
7729
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7332
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...
0
7677
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...
1
5258
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...
0
3386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1809
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
1
952
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
626
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.