下面是一個簡化示例,顯示CallByName()。請注意,目標變量是公共:
Public Class Form1
Public _101 As String
Public _102 As String = "{Default Value}"
Public _103 As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Debug.Print("Before: _102 = " & _102)
Dim ColMap As Integer = 102
Dim ValorLeido As String = "Hello World!"
Dim varName As String = "_" & ColMap
CallByName(Me, varName, CallType.Let, ValorLeido)
Debug.Print("After: _102 = " & _102)
End Sub
End Class
,並通過反思,這使得目標變量是私人這裏是同樣的事情:
Imports System.Reflection
Public Class Form1
Private _101 As String
Private _102 As String = "{Default Value}"
Private _103 As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Debug.Print("Before: _102 = " & _102)
Dim ColMap As Integer = 102
Dim ValorLeido As String = "Hello World!"
Dim varName As String = "_" & ColMap
Dim fi As FieldInfo = Me.GetType.GetField(varName, Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
fi.SetValue(Me, ValorLeido)
Debug.Print("After: _102 = " & _102)
End Sub
End Class
您需要包括更多的代碼,有配發該塊是ambigeous的路人怎麼回事。或者給你的問題添加一些更具體的細節。很難說你的目標是什麼。 – DarrenMB
使用反射在技術上是可行的。如果目標變量是Public,則可以使用傳統的CallByName()函數,使語法稍微簡單一些。它只會在變量處於Class中時才起作用,但CallByName()不適用於Modules。 –