Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,085 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,784 people online right now. Registration is fast and FREE... Join Now!




Array Issue

2 Pages V  1 2 >  
Reply to this topicStart new topic

Array Issue, Using Array before it is assigned a value

dstpierre
18 Apr, 2008 - 09:10 PM
Post #1

New D.I.C Head
*

Joined: 18 Apr, 2008
Posts: 11

APP: MS Visual Basic 2008 Express

Hey everyone, maybe I have just been looking at this to long. The goal is to read a data file with data like this:

AAA 1234 BBBB
DDD 333 22222

and place the data into a 2-D array. Logically it seems like my code should work. However it appears it throws an except because I am not using the 2-D array properly (says I am using it before it is assigned a value). Can anyone help shed some light on this? Thanks for helping! Hope all is well! - Dan

CODE
Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRead.Click
        'Declare Variable Types
        Dim intFileNum As Integer
        Dim strTemp As String = ""
        Dim intCnt As Integer = 0
        Dim strArray() As String
        Dim strDataArr()() As String

        'Find Available File Number
        intFileNum = FreeFile()

        'Open File To Read
        FileOpen(intFileNum, txtFilePath.Text, OpenMode.Input)

        'Start Loop for EOF?
        While Not EOF(intFileNum)

            'Read First Line of File Into String
            strTemp = LineInput(intFileNum)

            'Use Delimiter to Split Up Records in String and Insert into Array
            strArray = Split(strTemp, ControlChars.Tab)

            For i = 0 To UBound(strArray)
                strDataArr(intCnt)(i) = Trim(strArray(i))
            Next

            'Increment Counter
            intCnt = intCnt + 1

        End While

        'Close File
        FileClose(intFileNum)

    End Sub

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Array Issue
18 Apr, 2008 - 09:28 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to VB.NET smile.gif
User is offlineProfile CardPM
+Quote Post

GravityGuy
RE: Array Issue
18 Apr, 2008 - 09:33 PM
Post #3

New D.I.C Head
*

Joined: 21 Jan, 2008
Posts: 45


My Contributions
I'm could be wrong about the syntax for declaring a 2d array in VB but I think it should look like

Dim array2d(,) as string

not () () as you would in C++ but with [] [] square brackets.

Secondly, in VB if you then want to allocate space to the array after knowing how big it's going to be you have to use

Redim array2d(row,col) as string

I thought VB defaulted the starting index to 1 for array, contrary to C++, but maybe this has changed recently.


User is offlineProfile CardPM
+Quote Post

dstpierre
RE: Array Issue
18 Apr, 2008 - 09:56 PM
Post #4

New D.I.C Head
*

Joined: 18 Apr, 2008
Posts: 11

I tried declaring 2D array as strDataArr(,) and still same result. Application says I am using the array before it is assigned a valued. I also tried the redim and even delcaring strDataArr(100)(100) or strDataArr(100,100) and still run into the same message about using an array before a value is assigned. Thanks for the insight! Any other ideas? or am I missing something or doing something wrong? - Dan
User is offlineProfile CardPM
+Quote Post

dstpierre
RE: Array Issue
18 Apr, 2008 - 10:11 PM
Post #5

New D.I.C Head
*

Joined: 18 Apr, 2008
Posts: 11

Found a solution:
CODE
Dim strArray() As String = {}
        Dim strDataArr(,) As String = {}

or
CODE
Dim strArray(8000) As String
        Dim strDataArr(8000,8000) As String


either works. Thanks GravityGuy
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Array Issue
18 Apr, 2008 - 10:12 PM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You have to intialize the Array after it's created, thats the one step you havent completed


vb

'Declare and initialize array (we will use fictious values
'for initializing the array
Dim strDataArr()() As String = New String(2)(2)


Then when you know the exact size you need for your array you can ReDim the array


vb

While Not EOF(intFileNum)

'Read First Line of File Into String
strTemp = LineInput(intFileNum)

'Use Delimiter to Split Up Records in String and Insert into Array
strArray = Split(strTemp, ControlChars.Tab)

For i = 0 To UBound(strArray)
'ReDim your array. Be sure the use
'the Preserve keyword to retain the contents
Redim Preserve strDataArr(intCnt)(i)
'Add values to the array
strDataArr(intCnt)(i) = Trim(strArray(i))
Next

'Increment Counter
intCnt = intCnt + 1

End While


NOTE: When using ReDim make sure to remember the Preserve keyword so you are able to retain the values currently in the array. Forgetting this will cause the array to be overwritten each time you resize it.


@GravityGuy: In older versions of VB (such as VB6) string arrays started with index 1, but with .Net they sart with index zero
User is offlineProfile CardPM
+Quote Post

dstpierre
RE: Array Issue
18 Apr, 2008 - 10:13 PM
Post #7

New D.I.C Head
*

Joined: 18 Apr, 2008
Posts: 11

Is there any way of doing it without defining the size? I know if i use the first option I run into an error down the line that is resolved by setting a size for the array. I have no idea how large these files will become, would like to not cap the arrays. Any Ideas?
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Array Issue
18 Apr, 2008 - 10:15 PM
Post #8

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
dstpierre, you have to declare the size of the array, theres no way around it. Look at the code I offered in my last post to see how to accomplish this
User is offlineProfile CardPM
+Quote Post

dstpierre
RE: Array Issue
18 Apr, 2008 - 10:19 PM
Post #9

New D.I.C Head
*

Joined: 18 Apr, 2008
Posts: 11

Thanks for the reply PsychoCoder. I will try your method. Every time you redim does it auto allocate to the size needed each time?
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Array Issue
18 Apr, 2008 - 10:23 PM
Post #10

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Thats why you pass your counters when you ReDim the array. That tells it what size to resize the array to.
User is offlineProfile CardPM
+Quote Post

GravityGuy
RE: Array Issue
18 Apr, 2008 - 10:41 PM
Post #11

New D.I.C Head
*

Joined: 21 Jan, 2008
Posts: 45


My Contributions
This is a classic problem of storing the data you read from a file into an array but you don't know how big the file is until you get to the end.

If your application allows, you have the option of reading the entire file just to count the number of lines and hence the number of rows you will need to store the data. The number of columns will be limited, but if they aren't then you could go ahead and parse each line as well, just to get the size stats.

Then reopen the file and read it all in after resizing the array.

Like I said, it depends on how long the file is, how long the user might want to wait since you are doing twice the work, and whether the file is located on a local drive or on another server.

This type of problem is often solved by designing the file format to include the number of lines at the top of the file. The first line you read in thus contains the size of the dataset to follow. This 'header' method is found in all types of files including graphics/image files. How many pixels wide and high the image will be is located right in the header. A program that writes the file will obviously know how many it will be writing so it's easy to add this info to the top of the file. Not always but very often. Then, reading it back in is that much easier.
User is offlineProfile CardPM
+Quote Post

dstpierre
RE: Array Issue
18 Apr, 2008 - 10:43 PM
Post #12

New D.I.C Head
*

Joined: 18 Apr, 2008
Posts: 11

Well here is my current work in progess (working). I will go ahead and add on to support companies data file instead of my test data. Thanks for all the help everyone!

Sample Data File (TAB-DELIMITED):
AAA BBB CCC DDD EEE
111 222 333 444 555

CODE
Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRead.Click
        'Declare Variable Types
        Dim intFileNum As Integer
        Dim strTemp As String = ""
        Dim intCnt As Integer = 0
        Dim intCnt2 As Integer = 0
        Dim intCnt3 As Integer = 0
        Dim strArray(1000) As String
        Dim strDataArr(1000, 1000) As String

        'Find Available File Number
        intFileNum = FreeFile()

        'Open File To Read
        FileOpen(intFileNum, txtFilePath.Text, OpenMode.Input)

        'Loop to Read until End of File
        While Not EOF(intFileNum)

            'Read First Line of File Into String
            strTemp = LineInput(intFileNum)

            'Use Delimiter to Split Up Records in String and Insert into Array
            strArray = Split(strTemp, ControlChars.Tab)

            'Reset Counter2
            intCnt2 = 0

            'Fill 2D Array
            For i = 0 To UBound(strArray)
                strDataArr(intCnt, i) = Trim(strArray(i))
                intCnt2 = intCnt2 + 1
            Next

            'Increment Counter
            intCnt = intCnt + 1

        End While

        'Resync Counter 1, 2
        intCnt = intCnt - 1
        intCnt2 = intCnt2 - 1

        'Fill Listbox Loop
        While intCnt >= 0
            intCnt3 = intCnt2
            While intCnt3 >= 0
                lstData.Items.Add(intCnt & "," & intCnt3 & ": " & strDataArr(intCnt, intCnt3))
                intCnt3 = intCnt3 - 1
            End While
            intCnt = intCnt - 1
        End While

        'Close File
        FileClose(intFileNum)

    End Sub

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:46PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month