我有以下代码。一个将持有基金的班级
Public Class FundElement
Public Name As New FundDetail
Public Price As New FundDetail
Public Code As New CodeDetail
End Class
每个单独数据字段的基类
Public Class FundDetail
Protected _value As String
Public Property value As String
Get
Return _value
End Get
Set(valueToAssign As String)
If isValid(valueToAssign) Then
_value = valueToAssign
Else
Throw New ArgumentException
End If
End Set
End Property
Shared Function isValid(detail As String) As Boolean
Return True
End Function
End Class
一些继承的类将保存特定于某些字段的逻辑。这就是我想要一个类而不仅仅是每个字段的字符串的原因。
Public Class CodeDetail
Inherits FundDetail
Public Shared Shadows Function isValid(value As String) As Boolean
Dim intPattern As Regex = New Regex("[a-zA-Z]{3}[0-9]{12}")
Return intPattern.IsMatch(value)
End Function
End Class
现在,我的问题是我班级的API如下
Dim fund as new FundElement
FundElement.Name.Value = "Google Inc"
FundElement.Code.Value = "USD451234151234"
这意味着编码人员可以访问NAV元素,我想完全隐藏它。 API比它需要的更冗长。我希望界面如下
FundElement.Name = "Google Inc"
FundElement.Price = "USD451234151234"
我可以通过更改类来实现这一点,以便每个fundDetail实例都是私有的,并通过添加如下新属性
Public Class FundElement
Private _Name As New FundDetail
Public Property Name As String
Set(valueToAssign As String)
_Name.value = valueToAssign
End Set
Get
Return _Name.value
End Get
End Property
Private _Code As New FundDetails
Public Property _Code As String
Set(valueToAssign As String)
_Code.value = valueToAssign
End Set
Get
Return _Code.value
End Get
End Property
Private _Price As New PriceDetail
Public Property _Price As String
Set(valueToAssign As String)
_Price.value = valueToAssign
End Set
Get
Return _Price.value
End Get
End Property
End Class
现在这对于仅有3名成员而言非常冗长。此外,每个属性的代码几乎完全相同。打字和杂乱阅读是很乏味的。
有没有什么办法可以实现这一点,而无需在每次添加新成员时都输入属性?
答案 0 :(得分:0)
为什么值总是字符串?
我更擅长C#
Public Class FundElement
{
Public String Name { get; set; }
Public Decimal Price { get; set; }
Public Int32 Shares { get; set; }
}