循环通过类对象 - Visual Basic

时间:2015-01-09 20:59:48

标签: vb.net class loops object

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

    '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

1 个答案:

答案 0 :(得分:1)

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

在ListBox中显示列表的整个内容:

lbKunde.DataSource = myKunde

如果Kunde覆盖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