Take the 2-minute tour ×

Sorry for what is probably a very basic question. I have a vb.net class with properties defined as follows:

    Private m_Property1 As String
    Public Property Property1() As String
            Return m_Property1 
        End Get
        Set(ByVal value As String)
            If IsNothing(value) Then
                m_Property1 = String.Empty
                m_Property1 = value
            End If
        End Set
    End Property

I can then set the values as follows:

    classname.Property1 = "myvalue" 

How do I set the value of a property that is defined dynmically eg

    Dim strPropertyName As String = "Property1"

Hope that makes sense.

Thanks, Josh

share|improve this question
Is it me or is vb.net just too wordy. –  JonH Feb 17 '10 at 19:32
@JonH: It's obsequiously conversational. –  Joel Etherton Feb 17 '10 at 19:39
@JonH - I'll grant you property definitions. The rest of it is for the most part about right. –  Joel Coehoorn Feb 17 '10 at 19:45

2 Answers 2

up vote 1 down vote accepted

You would use reflection

Dim strPropertyName as string = "Property1"

Dim pi As PropertyInfo = myClass.GetType().GetProperty(strPropertyName)
pi.SetValue(myClass.GetType(), "some string", Nothing)
share|improve this answer

You want to use Reflection in order to do this. VB.NET provides a way to do this if you know the value at compile-time, but for run-time operations, you need to use the GetType keyword in order to get the type of your class (or, use the GetType method on an instance of it if you don't know it).

Then, with that Type instance, you would call GetProperty, passing the string with the name of the property. It will return an PropertyInfo instance which you then call GetValue on, passing the instance of the object in, which will return an Object which you have to cast back to a type you wish to use (if you are).

VB.NET makes a lot of this easier with the CallByName function.

Also, if you know at compile-time what the name of the property is, you can always cast to object and use VB.NET's inherent late binding:

Dim o As Object = <your object>

o.Property1 = ...

VB.NET will perform the late-binding for you.

share|improve this answer
I would never recomment late binding, but reflection is the way to roll. –  Joel Etherton Feb 17 '10 at 19:40

Your Answer


