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!
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
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
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
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?
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
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.
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!
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