遍历 class 个对象 - Visual basic

Loop through class objects - Visual basic

我如何循环遍历 vb.net 中 class 的所有对象,而不是像下面那样做。我想在代码中初始化我的 class 对象(这是一个示例),然后为每个循环创建一个循环而不是底部的代码。

    '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

最简单的方法是使用 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

循环:

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

要引用特定的:

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

' start over:
myKunde.Clear 

在列表框中显示整个列表内容:

lbKunde.DataSource = myKunde

如果昆德语覆盖 ToString(),那将是每个项目的显示内容。

它们就像一个数组,只是您不必提前知道它有多大,只需向其中添加项目即可。 List(of T) 有很多其他方法,例如 SortReverse,您还可以在其上使用扩展方法并查询集合。

按钮点击(不能翻译它说的)可以是这样的:

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