How to invoke the property base on the string?

Steven Young 261 Reputation points
2021-11-02T13:40:19.783+00:00

dim x as string="abc"

//I want to get or set the property using the string x, how to do?

Public Property abc() As Boolean
Get
Return true
End Get
Set(ByVal Value As Boolean)
If value=true Then
.....
End If
End Set
End Property

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 111.8K Reputation points
    2021-11-02T14:39:01.03+00:00

    Try something like this:

    Dim p = obj.GetType.GetProperty(x)
    
    ' Getting value: '
    Dim value As Boolean = CBool(p.GetValue(obj))
    
    ' Setting value: '
    p.SetValue(obj, some_new_value)
    

    where obj is the object that contains this property.

    0 comments No comments