每次按下按钮时,都应该将客户添加到列表中。但它只是覆盖了我之前的值,并没有更新List。
这正是他们在我的书中所做的,但我不知道为什么下一个变量没有被添加到列表中
{
"contacts": [
{
"id": "200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"url": "http://149.202.196.143:8000/live/djemal/djemal/592.ts"
},
{
"id": "201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"url":"http://149.202.196.143:8000/live/djemal/djemal/592.ts"
},
{
"id": "202",
"name": "Leonardo Dicaprio",
"email": "leonardo_dicaprio@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"url":"http://149.202.196.143:8000/live/djemal/djemal/592.ts"
}
]
}
我的班级" Klant"
Private Sub btnOpslaan_Click(sender As Object, e As EventArgs) Handles btnOpslaan.Click
Dim klantenlijst As New List(Of Klant)
Dim nieuwe_klant As New Klant
Dim path As String = IO.Path.GetTempFileName()
nieuwe_klant.Naam = txtNaam.Text
nieuwe_klant.Straat = txtStraat.Text
nieuwe_klant.Postcode = txtPostcode.Text
nieuwe_klant.Gemeente = txtGemeente.Text
nieuwe_klant.Telefoon = txtTelefoon.Text
nieuwe_klant.Email = txtEmail.Text
If chkHardware.Checked = True Then
nieuwe_klant.Hardware = True
End If
If chkInternet.Checked = True Then
nieuwe_klant.Internet = True
End If
If chkMultimedia.Checked = True Then
nieuwe_klant.Multimedia = True
End If
If chkSoftware.Checked = True Then
nieuwe_klant.Software = True
End If
klantenlijst.Add(nieuwe_klant)
MsgBox(klantenlijst.Count)
End Sub
结束班
答案 0 :(得分:2)
这里的问题是,每次点击,你都会宣布一个新的“klantenlijst”'并使其私密。只需将其声明在点击之外,您就可以获得所需的结果:
Dim klantenlijst As New List(Of Klant)
Private Sub btnOpslaan_Click(sender As Object, e As EventArgs) Handles btnOpslaan.Click
Dim nieuwe_klant As New Klant
Dim path As String = IO.Path.GetTempFileName()
nieuwe_klant.Naam = txtNaam.Text
//continue your code...
klantenlijst.Add(nieuwe_klant)
MsgBox(klantenlijst.Count)
答案 1 :(得分:1)
每次点击该按钮,您创建一个新列表:
Dim klantenlijst As New List(Of Klant)
然后你只在该列表中添加一个项目:
klantenlijst.Add(nieuwe_klant)
因此该列表只包含一个项目。
相反,创建一个类级别列表并添加到该列表中。所以把这一行放在班级:
Dim klantenlijst As New List(Of Klant)
然后在整个类的实例中都可以使用相同的列表。有几点需要注意: