CallByName()を示す簡単な例を次に示します。ターゲット変数がPublicであることに注意してください。
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
そして、ターゲット変数をPrivateにすることを許可するReflectionを介した同じことは次のとおりです。
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