Using Objects as Arrays

  • Thread starter Thread starter Jonas Pohlandt
  • Start date Start date
J

Jonas Pohlandt

You are working with a single Account object inside the loop.

Dim bankAcct As Account
Do While (sr.Peek <> -1)
bankAcct = new Account

instead of

Dim bankAcct As New Account 'create an instance of Account
Do While (sr.Peek <> -1)

should do the trick.
 
I have 3 records in a text file called cvsAcctInfo.txt. I would like to read
these three lines and put them into an array which happens to be a defined
object. When I do this, it creates an array of 3 records; however, all three
arrays have the same information for the LAST account (3rd line in text
file).

Dim accounts(50) As Account 'bankAcct is an object of class Account
Dim count As Integer 'keeps track of the array index

'used to parse out the data in cvsAcctInfo.txt
Dim recordLine As String
Dim acctRecord()
Dim field0, field1, field2, field3, field4 As String

Dim bankAcct As New Account 'create an instance of Account

Do While (sr.Peek <> -1)

recordLine = sr.ReadLine
acctRecord = recordLine.Split(";")
field0 = acctRecord(0) : field1 = acctRecord(1) : field2 = acctRecord(2)
field3 = acctRecord(3) : field4 = CDbl(acctRecord(4))

'reads account Name
bankAcct.name = field0
cboSelectAcct.Items.Add(bankAcct.name) <--- this works!
'reads account type
bankAcct.type = field1
'reads bank name
bankAcct.bankName = field2
'reads account number
bankAcct.number = field3
'reads account balance
bankAcct.balance = field4
*** note, if I list out each record here into a listBox, they list out
correctly ****
accounts(count) = bankAcct <-- somthing is going wrong here
count += 1
Loop

Now that I have the array, I want to get the balance based on what was
selected in a combo box... so I would do something like
txtBalance.text = accounts(cboSelectAcct.SelectedIndex).balance
but of course it doesn't work because all balances are the same for each
record in my array. If anyone can help, it will be appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top