VB.net 从 class 创建的数组中检索特定值?
VB.net retrieve specific value from an array create by a class?
我有一个叫 'account' 的 Class。这是代码:
Public Class Account
Private accounttype As String
Private accountnumber As Integer
Private branchcode As String
Private currency As String
Private balance As Integer
Public Sub New()
accounttype = " "
accountnumber = 0
branchcode = " "
currency = " "
balance = 0
End Sub
Public Sub New(ByVal cu As String, ByVal na As Integer, ByVal su As String, ByVal a1 As String, ByVal a2 As Integer)
accounttype = cu
accountnumber = na
branchcode = su
currency = a1
balance = a2
End Sub
Public ReadOnly Property Getaccountnumber() As String
Get
Return accountnumber
End Get
End Property
Public ReadOnly Property Getbalance() As Integer
Get
Return balance
End Get
End Property
Public Overrides Function ToString() As String
Return "Account Type: " & accounttype & vbCrLf & "Account Number: " & accountnumber.ToString & vbCrLf & "Branch Code: " & branchcode & vbCrLf & "Currency: " & currency & vbCrLf & "Balance: " & balance.ToString
End Function
End Class
然后我硬编码了几个这样的帐户。
Dim account1 As New Account("Savings", "1", "S1", "Khajalee", 1000)
accodetails.Add(account1)
我想知道如何从数组中获取帐户的 'balance' 值。任何帮助都会很棒!!如您所见,我尝试使用一些不同的代码来获得余额,但没有成功。
谢谢
假设,"the array",你的意思是accodetails
,那么你可以通过索引访问数组中的任何项目,像这样:
Dim myAccount As Account = accodetails(0)
然后您可以通过该变量访问 Account
对象中的任何 属性,如下所示:
Dim myBalance As Integer = myAccount.GetBalance
或者,更简单地说,您可以直接从列表中的项目访问 属性,而无需将其分配给变量,如下所示:
Dim myBalance As Integer = accodetails(0).GetBalance
我有一个叫 'account' 的 Class。这是代码:
Public Class Account
Private accounttype As String
Private accountnumber As Integer
Private branchcode As String
Private currency As String
Private balance As Integer
Public Sub New()
accounttype = " "
accountnumber = 0
branchcode = " "
currency = " "
balance = 0
End Sub
Public Sub New(ByVal cu As String, ByVal na As Integer, ByVal su As String, ByVal a1 As String, ByVal a2 As Integer)
accounttype = cu
accountnumber = na
branchcode = su
currency = a1
balance = a2
End Sub
Public ReadOnly Property Getaccountnumber() As String
Get
Return accountnumber
End Get
End Property
Public ReadOnly Property Getbalance() As Integer
Get
Return balance
End Get
End Property
Public Overrides Function ToString() As String
Return "Account Type: " & accounttype & vbCrLf & "Account Number: " & accountnumber.ToString & vbCrLf & "Branch Code: " & branchcode & vbCrLf & "Currency: " & currency & vbCrLf & "Balance: " & balance.ToString
End Function
End Class
然后我硬编码了几个这样的帐户。
Dim account1 As New Account("Savings", "1", "S1", "Khajalee", 1000)
accodetails.Add(account1)
我想知道如何从数组中获取帐户的 'balance' 值。任何帮助都会很棒!!如您所见,我尝试使用一些不同的代码来获得余额,但没有成功。
谢谢
假设,"the array",你的意思是accodetails
,那么你可以通过索引访问数组中的任何项目,像这样:
Dim myAccount As Account = accodetails(0)
然后您可以通过该变量访问 Account
对象中的任何 属性,如下所示:
Dim myBalance As Integer = myAccount.GetBalance
或者,更简单地说,您可以直接从列表中的项目访问 属性,而无需将其分配给变量,如下所示:
Dim myBalance As Integer = accodetails(0).GetBalance