gpt4 book ai didi

vb.net - 直接给对象赋值

转载 作者:行者123 更新时间:2023-12-02 08:15:12 25 4
gpt4 key购买 nike

例如,我有几个属性

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并且无法从类(class)外部访问。

向属性添加访问修饰符应该可以解决您看到的问题:

Public Property SQL_Column As String
Public Property Limit As Integer
Public Property Value As String

请告诉我投票失败的问题 - 这是上面建议的代码更改的有效 .NET fiddle ( https://dotnetfiddle.net/96o8qm )。

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

关于vb.net - 直接给对象赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27894985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com