Ruttensoft
asked on
Convert string to object?
Hello
I have a boolean:
Dim test as boolean = false
and now I have a string "test"
how can I now directly get from the string "test" to
test = true?
I tried
Private Sub setvalue(ByVal a As Object, ByVal value As String)
a.value = Convert.ToBoolean(value)
End Sub
but it did not work?
Thanks a lot
Sven
I have a boolean:
Dim test as boolean = false
and now I have a string "test"
how can I now directly get from the string "test" to
test = true?
I tried
Private Sub setvalue(ByVal a As Object, ByVal value As String)
a.value = Convert.ToBoolean(value)
End Sub
but it did not work?
Thanks a lot
Sven
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can also use Reflection. I'll see if I can find an example...
Here is an example using Reflection:
Public Class Form1
Inherits System.Windows.Forms.Form
Private _test As Boolean = False
Private Property test() As Boolean
Get
Return _test
End Get
Set(ByVal Value As Boolean)
_test = Value
End Set
End Property
Public Sub SetValue(ByVal obj As Object, ByVal name As String, ByVal value As Object)
Dim p As System.Reflection.Property Info = _
obj.GetType.GetProperty(na me, _
Reflection.BindingFlags.In stance Or _
Reflection.BindingFlags.Pu blic Or _
Reflection.BindingFlags.No nPublic Or _
Reflection.BindingFlags.Ig noreCase)
If Not IsNothing(p) Then
p.SetValue(obj, value, Nothing)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim varName As String = "test"
MsgBox(test, MsgBoxStyle.Information, "Before")
SetValue(Me, varName, True)
MsgBox(test, MsgBoxStyle.Information, "After")
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Private _test As Boolean = False
Private Property test() As Boolean
Get
Return _test
End Get
Set(ByVal Value As Boolean)
_test = Value
End Set
End Property
Public Sub SetValue(ByVal obj As Object, ByVal name As String, ByVal value As Object)
Dim p As System.Reflection.Property
obj.GetType.GetProperty(na
Reflection.BindingFlags.In
Reflection.BindingFlags.Pu
Reflection.BindingFlags.No
Reflection.BindingFlags.Ig
If Not IsNothing(p) Then
p.SetValue(obj, value, Nothing)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim varName As String = "test"
MsgBox(test, MsgBoxStyle.Information, "Before")
SetValue(Me, varName, True)
MsgBox(test, MsgBoxStyle.Information, "After")
End Sub
End Class
ASKER
Hi, your mind is never idle...... very quick and superb answer, thanks a lot (used the first bits of code)
Thanks
Sven
Thanks
Sven