我有几个属性,例如
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
显然,上面的代码不起作用,但是我想知道是否存在等效的代码吗?
最佳答案
当没有提供访问修饰符时,对属性(例如,Public,Private等)的默认访问修饰符的限制最大。在SQLString类中,由于在类的属性前面没有Public
访问修饰符,因此它们本质上是Private
,并且不能从类外部访问。
将access修饰符添加到属性应解决您看到的问题:
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