我有几个属性,例如
Public Property FIRSTNAME As New SQLString("FirstName", 50)
Public Property FULLNAME As New SQLString("Name", 50)
SQLString对象定义为:
Public Class SQLString
Property SQL_Column As String
Property Limit As Integer
Property Value As String
Public Sub New(SQLcolumn As String, limit_ As Integer)
SQL_Column = SQLcolumn
Limit = limit_
End Sub
Public ReadOnly Property SQL_value() As String
Get
Return "'" & clean(Value, Limit) & "'"
End Get
End Property
End Class
请注意,通过此方法,我的每个属性(例如FIRSTNAME
)都可以拥有多个子属性,这是必要的。
要访问它们,只需例如FIRSTNAME.SQL_Column
。
这是有效的,但我想要的是能够在FIRSTNAME
属性本身上存储一个值(例如字符串数据类型),这将使其像访问它一样:
Dim MyFirstName As String = FIRSTNAME
而不是:
Dim MyFirstName As String = FIRSTNAME.Value
这是我目前要做的事情。 我能看到的唯一方法是默认将SQLString对象设置为字符串(或其他数据类型),如:
Public Class SQLString As String
显然上面的代码不起作用,但我想知道是否有相同的代码呢?
答案 0 :(得分:2)
当没有提供访问修饰符时,属性的默认访问修饰符(即:Public,Private等)是最严格的。在SQLString类中,由于类中的属性前面没有Public
访问修饰符,因此它们基本上是Private
,无法从类外部访问。
向属性添加访问修饰符可以解决您看到的问题:
Public Property SQL_Column As String
Public Property Limit As Integer
Public Property Value As String
请告诉我投票结果的问题 - 这是上面提出的代码更改(https://dotnetfiddle.net/96o8qm)的.NET小提琴。
Imports System
Dim p as Person = new Person()
p.FIRSTNAME = new SQLString("Test", 1)
p.FIRSTNAME.Value = "Test Value"
Console.WriteLine("Person Value: {0}", p.FIRSTNAME.Value)
Public Class Person
Public Property FIRSTNAME AS SQLString
End Class
Public Class SQLString
Public Property SQL_Column As String
Public Property Limit As Integer
Public Property Value As String
Public Sub New(SQLcolumn As String, limit_ As Integer)
SQL_Column = SQLcolumn
Limit = limit_
End Sub
Public ReadOnly Property SQL_value() As String
Get
Return ""
End Get
End Property
End Class
这会产生输出:
Person Value: Test Value