我正在尝试为具有Property
类型的全局变量分配一个值Double
。这Property
是传递的Object
,分配失败。
在下面的示例代码中,该值从未分配给实际对象,而只是在本地:
Public Class Form1
Friend Home As New Building
Private Sub AssignValues() Handles Me.Load
'Objects of different types are added to a list
Dim listObjects As New List(Of Object)
listObjects.Add(Home.Surface)
'All the Objects in listObjects are assigned a value that
'is stored as String
For Each o As Object In listObjects
SetProperty(o, "45.6")
Debug.Print("Surface = " & Home.Surface.ToString)
Next
End Sub
Private Sub SetProperty(ByRef Variable As Object, ByVal Value As String)
Select Case Variable.GetType
Case GetType(Double)
Variable = CDbl(Value)
Case Else
'...
End Select
End Sub
End Class
Public Class Building
Dim _surface As Double = 0
Public Property Surface As Double
Get
Return _surface
End Get
Set(ByVal value As Double)
_surface = value
End Set
End Property
End Class
程序总是输出Surface = 0
而不是45.6
. 我究竟做错了什么?
我尝试按照此处Variable
的建议传递作为参考,但没有成功。我还阅读了有关使用 Reflection的信息,但应该有比这更简单的东西......