Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting Value in Object Sub Properties

Status
Not open for further replies.

AndyWatt

Programmer
Oct 24, 2001
1,288
GB
I'm trying to set an object property (in this example Calendar.SelectedDayStyle.BackColor) from a string value containing the property name (e.g. "SelectedDayStyle.BackColor"). From some Google searches I've managed to cobble together some code that I'd hoped would move down the object hierarchy to the target property and then set the value of that property.

Would someone be kind enough to point out what I'm doing wrong with the following:

Code:
Public Sub SetPropertyFromString(ByVal TargetObject As Object, ByVal PropertyString As String, ByVal ValueToSet As String)
        Dim ObjectType As Type = TargetObject.GetType()
        Dim strProperty As String
        Dim colTemp As Drawing.Color

        For Each strProperty In PropertyString.Split(".")
            Dim LocalProperty As System.Reflection.PropertyInfo
            LocalProperty = ObjectType.GetProperty(strProperty)
            TargetObject = LocalProperty.GetValue(TargetObject, Nothing)
            ObjectType = LocalProperty.PropertyType
        Next
        colTemp = Drawing.Color.FromName(ValueToSet)
        TargetObject.SetValue(TargetObject, colTemp, Nothing)
    End Sub
Called like this:
Code:
SetPropertyFromString(Me.Calendar1, "SelectedDayStyle.BackColor", "Red")

The above code generates a MissingMemberException and I don't understand why!

Many thanks in advance.

Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
Thanks for the suggestion! I've reposted in ASP.NET.

Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top