Module MyModule
Public Property alpha As String
Public Property beta As String
Public Property charlie As String
End MyModule
Public Class MyClass
Sub New()
Dim variableName as String = "beta"
MyModule.GetField(variableName) = "new value"
End Sub
End Class
I'm looking for a way to set the value of a variable in the module. But only the variable with the given variable name. The example above obviously does not work, but it provides an idea of my goal.
Imports System.Reflection
Public Class Form1
Sub New()
SetVariableByVariableName("beta", "new value")
End Sub
Public Sub SetVariableByVariableName(ByVal variableName As String, ByVal value As String)
Dim myFieldInfo As FieldInfo = GetType(MyModule).GetField(variableName, BindingFlags.Public Or BindingFlags.Static)
If myFieldInfo IsNot Nothing Then
myFieldInfo.SetValue(Nothing, value)
Console.WriteLine($"The value of {variableName} is now: {GetType(MyModule).GetField(variableName).GetValue(Nothing)}")
Else
Console.WriteLine($"Error: {variableName} does not exist in MyModule.")
End If
End Sub
End Class
Module MyModule
Public Property alpha As String
Public Property beta As String
Public Property charlie As String
End Module