Link to home
Start Free TrialLog in
Avatar of Ruttensoft
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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.PropertyInfo = _
            obj.GetType.GetProperty(name, _
                Reflection.BindingFlags.Instance Or _
                Reflection.BindingFlags.Public Or _
                Reflection.BindingFlags.NonPublic Or _
                Reflection.BindingFlags.IgnoreCase)
        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
Avatar of Ruttensoft
Ruttensoft

ASKER

Hi, your mind is never idle...... very quick and superb answer, thanks a lot (used the first bits of code)

Thanks

Sven