Trying to read from a .txt file

DominicB

Active member
Joined
May 2, 2008
Messages
30
Programming Experience
Beginner
Hi all

First post here. I'm new at all this VB.Net stuff, but an old hand at using VBA, but all though the two are related there's such a huge difference that I'm still finding my feet. As practice I'm making a very simple contacts form reading information in from a .txt file. Each different field uses a delimiter of ASCII character 1-7, to help me identify the field. Now last night I had this thing running beautifully and I did something stupid and tinkered with the code, now one line is showing an error, and I can't figure out why. VB very helpfully offers to run the last good build for me, but won't show me the code.

Would somebody pleeeeaaaaase put me out of my misery and back on the right track.
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fileReader As String = My.Computer.FileSystem.ReadAllText("C:\AddressBook.txt")
        Dim Names() As String
        Dim Address1() As String, Address2() As String
        Dim Address3() As String, Address4() As String
        Dim Postcode() As String, Telephone() As String
        Dim Counter1 As Long = 1, Counter2 As Long = 0
        Dim fields() As String
        Dim delimiters() As Char = {Chr(1), Chr(2), Chr(3), Chr(4), Chr(5), Chr(6), Chr(7), Chr(8)}
        fields[COLOR="Red"]()[/COLOR] = fileReader.Split(delimiters)
        ReDim Names(UBound(fields) / 7)
        ReDim Address1(UBound(fields) / 7)
        ReDim Address2(UBound(fields) / 7)
        ReDim Address3(UBound(fields) / 7)
        ReDim Address4(UBound(fields) / 7)
        ReDim Postcode(UBound(fields) / 7)
        ReDim Telephone(UBound(fields) / 7)
        While Counter1 < (UBound(fields) / 7) + 1
            Names(Counter1) = fields(Counter2)
            Counter2 = Counter2 + 1
            Address1(Counter1) = fields(Counter2)
            Counter2 = Counter2 + 1
            Address2(Counter1) = fields(Counter2)
            Counter2 = Counter2 + 1
            Address3(Counter1) = fields(Counter2)
            Counter2 = Counter2 + 1
            Address4(Counter1) = fields(Counter2)
            Counter2 = Counter2 + 1
            Postcode(Counter1) = fields(Counter2)
            Counter2 = Counter2 + 1
            Telephone(Counter1) = fields(Counter2)
            Counter2 = Counter2 + 1
            Counter1 = Counter1 + 1
        End While
    End Sub
It's the bit in red that has the dreaded blue squiggly line under it and I'm taunted by the error message "Number of indices is less than the number of dimensions of the indexed array."

A big fat (virtual) ice cream to the one who can end my torment.

Thanks for reading.

DominicB
 
VB.NET:
fields = fileReader.Split(delimiters)

This should do it. The right part of the assignation returns an array. fields represents an array. fields() does not represent anything.

The only time you use empty parenthesis with arrays is when you declare it, to tell the program you declare an array of String and not just a String. Otherwise you must put the index of an element in the parenthesis or a compilation error occurs.

PS. I rather like syntax like Java "String[] something = null". Using the same operator for calling methods and array indexing/declaration makes thing more complicated than they need to be...
 
Last edited:
Top of the class for you, Stonkie

Hi Stonkie

You know what? It damn well works:D

So relieved that all my hard work isn't down the pan.

Stonkie}I rather like syntax like Java "String[ said:
something = null". Using the same operator for calling methods and array indexing/declaration makes thing more complicated than they need to be...
Yes, I'm sure I'm not doing it in the most efficient way code-wise but as I said it's all for practise and I'm sure I'll pick up better ways of using the language structure as I pick up more of the syntax and get used to VB.Net's little quirks.

Thanks very much for your timely help.

DominicB
 
That was a complain about the syntax of VB vs Java, not your code :p
 
PS. I rather like syntax like Java "String[] something = null". Using the same operator for calling methods and array indexing/declaration makes thing more complicated than they need to be...
Using different things for different things is more complicated than using one thing for everything ;) The {[]} characters is also just about out of reach on keyboard, you gotta hate that!
 
I guess so... Unless you write French all the time and got used to misplaced keys anyway! :p
 
Back
Top