Private Structure Staff
Public Name As String
Public Number As Integer
Public Total As Integer
Public Status As String
End Structure
Dim Staffdata As Staff
Private Sub btnAddArray_Click(sender As Object, e As EventArgs) Handles btnAddArray.Click
Dim strinfo As String
Staffdata.Name = txtStaffmem.Text
Staffdata.Number = txtStaffNum.Text
Staffdata.Total = lblTotal.Text
If radFullTime.Checked = True Then
Staffdata.Status = "Full time"
ElseIf radPartTime.Checked = True Then
Staffdata.Status = "Part time"
End If
strinfo = ("Name: " & Staffdata.Name & ", Number: " & Staffdata.Number & ",Status: " & Staffdata.Status & ",Total sales: " & Staffdata.Total)
我想通过此按钮将这些信息添加到数组中,我该怎么做?此外,我需要使用此结构添加至少5个工作人员的结果,并将它们添加到数组中。
答案 0 :(得分:2)
常见的方法是覆盖结构中的ToString
方法。
Private Structure Staff
Public Name As String
Public Number As Integer
Public Total As Integer
Public Status As String
Public Overrides Function ToString() As String
Return String.Concat(
"Name: ", Me.Name,
", ",
"Number: ", Me.Number.ToString(),
", ",
"Status: ", Me.Status,
", ",
"Total sales: ", Me.Total.ToString()
)
End Function
End Structure
使用List(Of T)存储数据。
Private ReadOnly StaffData As New List(Of Staff)
在点击手柄中,将新项目添加到列表中。
Private Sub btnAddArray_Click(sender As Object, e As EventArgs) Handles btnAddArray.Click
Me.StaffData.Add(New Staff() With {
.Name = Me.txtStaffmem.Text,
.Number = Integer.Parse(Me.txtStaffNum.Text),
.Total = Integer.Parse(Me.lblTotal.Text),
.Status = If(Me.radFullTime.Checked, "Full time", If(Me.radPartTime.Checked, "Part time", ""))
})
End Sub
现在,如果您想创建一个包含所有项目的字符串,您可以执行以下操作:
Dim all As String = String.Join("; ", (From item As Staff In Me.StaffData Let s = item.ToString() Select s))
答案 1 :(得分:0)
您可以将strinfo
声明为字符串数组以存储人员信息。因此你的代码就像:
Private Structure Staff
Public Name As String
Public Number As Integer
Public Total As Integer
Public Status As String
End Structure
Dim Staffdata As Staff
Dim stafCount As Integer=0 ' To calculate array index
Dim strinfo() As String ' Declare staff info as a string
Private Sub btnAddArray_Click(sender As Object, e As EventArgs) Handles btnAddArray.Click
Staffdata.Name = txtStaffmem.Text
Staffdata.Number = txtStaffNum.Text
Staffdata.Total = lblTotal.Text
If radFullTime.Checked = True Then
Staffdata.Status = "Full time"
ElseIf radPartTime.Checked = True Then
Staffdata.Status = "Part time"
End If
strinfo(stafCount) = "Name: " & Staffdata.Name & ", Number: " & Staffdata.Number & ",Status: " & Staffdata.Status & ",Total sales: " & Staffdata.Total
stafCount +=1 ' Increment array index
End Sub