[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

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
0
Ruttensoft
Asked:
Ruttensoft
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use the old CallByName() method:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public test As Boolean = False

    Private Sub SetValue(ByVal obj As Object, ByVal name As String, ByVal value As Object)
        Try
            CallByName(obj, name, CallType.Let, value)
        Catch ex As Exception
        End Try
    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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can also use Reflection.  I'll see if I can find an example...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
RuttensoftAuthor Commented:
Hi, your mind is never idle...... very quick and superb answer, thanks a lot (used the first bits of code)

Thanks

Sven
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now