Data & read.

shamu

Member
Joined
Jan 4, 2010
Messages
16
Programming Experience
1-3
Hi everyone.

I cut my programming teeth on QBASIC so apologise if I'm a bit behind the times.
I wrote a program in QB and I'm trying to convert it to VB.NET. One stumbling block I've hit is the DATA & READ facility, there does'nt appear to be an equivilant in VB.
I want to read a list of data into an array using a FOR NEXT loop.
Can anyone suggest a VB equivalent?

Thanks in anticipation.
 
There are many different ways of reading data into an array but I would suggest starting by taking a look at the TextFieldParser in the help file to see if that fits your needs. Also what are you doing with the data after it is imported? It may be more efficient to read the data into a DataSet/DataTable instead depending on what you are doing with the data. A datatable will provide more functionality then an array but again that depends on what you are doing.
 
If your data is not too extensive, you can initialize an array as follows:

Dim intarray() As Integer = {3,6,12,89,23,56,123,45,7,9,12,14,59,254,11}

The first value after the curly brace will be saved to index 0 of the array, and so on. The number of values equals the length of the array, and the upperbound value is one less than the length.

You can also do this with a string array:

Dim strarray() As String = {"one", "two", "three", "four"}

If you don't want to save anything to the 0th index, then use 0 for the first value in a number array, or "" for the first value in a string array.

For a large number of values, you will need to place them inside a text file and enter the data to an array inside a For loop.
 
Thanks for the replies.
Tom, I have read up on textfieldparser, I think it is a few levels above me!!
I probably should have mentioned in the first place but I am working with a two dimensional array. Solitaire, how would I apply your method to a 2d array?

Thanks & regards.
 
Its actually pretty easy if you want an example just let me know. Again may I ask what you are doing with the array after its filled? An array uses less resources but its easier and provides more functionality to use a datatable.
 
Thanks for the reply Tom.

The array is used to store the electronic equivelent version of an A4 sheet of numbers, 16 columns by 8 rows. I want to search along the top row using a 'FOR n=1 to 16 NEXT' loop until a value is matched, I use 'IF var1=array-location THEN' to check for the match. When a match is made I pop the loop and retain the value of n. I then want to search down the first column for another match with another variable, when this match is made I want to save that location as done with the row search above so I effectivly finish up with two values one for a column number, one for a row number. I then want to use these as coordinates to access the required number in the middle of the table!!!!!

Hope you can make sense of that.

Regards.
 
Yes I would suggest using a data table then; it will hold all 16 columns of info if you want and makes searching any of the column rows easier then looping thru all records.
 
Initializing a 2-D array with data: Place a comma inside the (,) after the Dim statement to denote two dimensions. The set of values for each index is enclosed within curly braces, nested inside an outer set of curly braces. The number of sets is the X dimension, and the number of values in each set is the Y dimension.

Place a label and a button on a new form to see how this works. The label will display the declaration statement so make the form wide enough to show all of it. A nested For-Next loop is used to display each of the values by popping up a Messagebox for each data item as it goes through the nested loop.


VB.NET:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Label1.Text = "Dim arr2D(,) As Integer = {{1,2,3,4}, {10, 20, 30, 40}, {11, 22, 33, 44}}"
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim arr2D(,) As Integer = {{1, 2, 3, 4}, {10, 20, 30, 40}, {11, 22, 33, 44}}
		For x As Integer = 0 To 2
			For y As Integer = 0 To 3
				MessageBox.Show("arr2D(" & x & ", " & y & ") = " & arr2D(x, y), "x = " & x & ",  y = " & y)
			Next y
		Next x
	End Sub
 
Last edited:
Shamu what is the delimiter in the file you want read (tab, comma, pipe, fixed length etc)? I'll make you up an example of each; reading into an array and reading into a datatable. Also if you can attach some sample text that matches your file that would be helpful.
 
Tom.

I wasn't reading the data from a file, the data items are written into the program in qbasic like:
DATA x,S1,S2,S3,S4,I,II,III,8,A,A,A,A,A,A,B
DATA 15,A,A,A,A,A,B,C,25,A,A,B,B,B,C,D
DATA 50,A,B,B,C,C,D,E,90,B,B,C,C,C,E,F
DATA 150,B,B,C,D,D,F,G,280,B,C,D,E,E,G,H
DATA 500,B,C,D,E,F,H,J,1200,C,C,E,F,G,J,K
DATA 3200,C,D,E,G,H,K,L,10000,C,D,F,G,J,L,M
DATA 35000,C,D,F,H,K,M,N,150000,D,E,G,J,L,N,P
DATA 500000,D,E,G,J,M,P,Q,999999,D,E,H,K,N,Q,R


then read into the array like:

FOR row = 1 TO 16

FOR column = 1 TO 8
READ codeletter$(row, column)
NEXT column

NEXT row


Hope this helps.
 
Initializing a 2-D array with data: Place a comma inside the (,) after the Dim statement to denote two dimensions. The set of values for each index is enclosed within curly braces, nested inside an outer set of curly braces. The number of sets is the X dimension, and the number of values in each set is the Y dimension.

Place a label and a button on a new form to see how this works. The label will display the declaration statement so make the form wide enough to show all of it. A nested For-Next loop is used to display each of the values by popping up a Messagebox for each data item as it goes through the nested loop.


VB.NET:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Label1.Text = "Dim arr2D(,) As Integer = {{1,2,3,4}, {10, 20, 30, 40}, {11, 22, 33, 44}}"
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim arr2D(,) As Integer = {{1, 2, 3, 4}, {10, 20, 30, 40}, {11, 22, 33, 44}}
		For x As Integer = 0 To 2
			For y As Integer = 0 To 3
				MessageBox.Show("arr2D(" & x & ", " & y & ") = " & arr2D(x, y), "x = " & x & ",  y = " & y)
			Next y
		Next x
	End Sub

Thanks Solitaire, I'll give that a try.
 
Oh sorry for the misunderstanding, I thought you were looking to read the data from a file. Reading data into an array tends to be more dynamic when your program doesnt know what the values are before hand. Since the values are permenant; you can just initalize an array variable (or datatable) as Solitare has suggested.
 
Oh sorry for the misunderstanding, I thought you were looking to read the data from a file. Reading data into an array tends to be more dynamic when your program doesnt know what the values are before hand. Since the values are permenant; you can just initalize an array variable (or datatable) as Solitare has suggested.

If you have one to hand I would appreciate an example of a datatable as it may come in useful for something else.
Thanks.
 
Back
Top