1

How can i loop through all objects of a class in vb.net, instead of doing as below. I want to init my class objects in the code (it's an example), and then make a for each loop through them all instead of the code in the bottom.

    'Angiv 1. saldo, 2. Navn, 3. CPR nummer, 4. pinkode
Dim Kunde1 As New Kunde(1010, "Mads Peter Petersen", "010190-0000", 1111)
Dim Kunde2 As New Kunde(2020, "John Pedersen", "010190-0001", 2222)
Dim Kunde3 As New Kunde(3030, "Flemming Farsø", "010190-0002", 3333)
Dim Kunde4 As New Kunde(4040, "Didrich Rich", "010190-0003", 4444)
Dim Kunde5 As New Kunde(5050, "Tobias Larsen", "010190-0004", 5555)
Dim aktivKunde As Kunde


Private Sub Main()
    btn100.Enabled = False
    btn200.Enabled = False
    btn500.Enabled = False
    btn1000.Enabled = False
    btnGodkend.Enabled = False
    btnVisbevaegelser.Enabled = False
End Sub


Private Sub btnTjek_Click(sender As Object, e As EventArgs) Handles btnTjek.Click


    If Kunde1.Tjek(CInt(txtPinkode.Text), CInt(txtCpr.Text.Substring(txtCpr.Text.Length - 4))) Then
        txtSaldo.Text = Kunde1.Saldo
        txtNavn.Text = Kunde1.Navn
        aktivKunde = Kunde1
        AktiverKnapper()


    ElseIf Kunde2.Tjek(CInt(txtPinkode.Text), CInt(txtCpr.Text.Substring(txtCpr.Text.Length - 4))) Then
        txtSaldo.Text = Kunde2.Saldo
        txtNavn.Text = Kunde2.Navn
        aktivKunde = Kunde2
        AktiverKnapper()


    ElseIf Kunde3.Tjek(CInt(txtPinkode.Text), CInt(txtCpr.Text.Substring(txtCpr.Text.Length - 4))) Then
        txtSaldo.Text = Kunde3.Saldo
        txtNavn.Text = Kunde3.Navn
        aktivKunde = Kunde3
        AktiverKnapper()


    ElseIf Kunde4.Tjek(CInt(txtPinkode.Text), CInt(txtCpr.Text.Substring(txtCpr.Text.Length - 4))) Then
        txtSaldo.Text = Kunde4.Saldo
        txtNavn.Text = Kunde4.Navn
        aktivKunde = Kunde4
        AktiverKnapper()


    ElseIf Kunde5.Tjek(CInt(txtPinkode.Text), CInt(txtCpr.Text.Substring(txtCpr.Text.Length - 4))) Then
        txtSaldo.Text = Kunde5.Saldo
        txtNavn.Text = Kunde5.Navn
        aktivKunde = Kunde5
        AktiverKnapper()

    Else : MessageBox.Show("Der var desværre ingen kunder med dette CPR-nummer & pinkode", "Forkert pinkode eller CPR-nummer")
    End If
0

1 Answer 1

1

The easiest thing is to use a collection/List:

' declare the list var AND create an instance of it (these are objects)
Private myKunde As New List(of Kunde)

 ...
myKunde.Add(New Kunde(1010, "Mads Peter Petersen", "010190-0000", 1111))
myKunde.Add(New Kunde(2020, "John Pedersen", "010190-0001", 2222))
... etc

To loop:

For Each k As Kunde In myKunde 
   Console.WriteLine(k.Name)           ' ??
Next

To reference a specific one:

Console.WriteLine(myKunde(1).Name)   
' remove Kunde John:
myKunde.RemoveAt(1)

' start over:
myKunde.Clear 

Display entire contents of the list in a ListBox:

lbKunde.DataSource = myKunde

If Kunde overrides ToString(), that is what will display for each item.

They are like an array except you do not have to know how big to make it ahead of time, just add items to it. The List(of T) has oodles of other methods such as Sort and Reverse and you can also use extension methods on it and query the collection.

The button click (cant translate what it says) can be something like:

Sub btnClick(....
  Dim Kindex As Integer

  ' this might be able to be reduced more, I dont know what they are
  ' a LISTBOX would certainly get rid of it
  If Kunde1.Tjek Then
      Kindex = 1
  ElseIf Kunde2.Tjek
      Kindex = 2
  ...

  DisplayKunde(KIndex)
  ...

Private Sub DisplayKinde(ndx As integer)
    ' display desired Kunde to txt controls
    txtBox1.Text = myKunde(ndx).Name      ' ? no idea whats in Kinde
    txtBox2.Text = myKunde(ndx).Foo
    txtBox3.Text = myKunde(ndx).Bar
    ...
End Sub
Sign up to request clarification or add additional context in comments.

15 Comments

In my for each loop, i cant write ".. In myKunde" ? It says that it is not declared?
the very first line of code declares and creates an instance; if this is code in a form, make sure it is outside any SUB
Can't i just do like this? But it dont work as before? For Each kundeNavn As Kunde In kundeListe If kundeNavn.Tjek(CInt(txtPinkode.Text), CInt(txtCpr.Text.Substring(txtCpr.Text.Length - 4))) Then txtSaldo.Text = kundeNavn.Saldo txtNavn.Text = kundeNavn.Navn aktivKunde = kundeNavn AktiverKnapper() Else : MessageBox.Show("Der var desværre ingen kunder med dette CPR-nummer & pinkode", "Forkert pinkode eller CPR-nummer") End If
Sorry, I cant read/understand any of that, so I cant tell what you are trying to do. it doesnt look like you are using the indexer though. Navn seems to be a property tjhough, so For each k As Kunde in myKundeList // If k.Navn ... is probably more what you want
Update: it works if i put the Kundeliste.Add.... in the same sub as the for each loop - how come? :) P.s Thanks for your help!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.