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 Object Properties from string value

Status
Not open for further replies.

AndyWatt

Programmer
Oct 24, 2001
1,288
GB
I'm an absolute newb to ASP/.Net, and this is my first task!

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!

I'd be grateful for any guidance/solutions you can provide.

Many thanks in advance.

Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
I have to ask why you are trying to set property values this way?
 
and what is wrong with this?
Code:
MyCalendar.SelectedDayStyle.BackColor = Drawing.Color.FromName(name_of_color)


Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
and what's wrong with this?...

Because the properties I'm trying to set are not necessarily known in advance. Values are stored elsewhere, and do not solely apply to Calendar controls. Nor will they always apply on each page load.

"Simple" things like object.property I've managed to get to work by cut-and-pasting examples found on MSDN and elsewhere, but it's navigating down to nth sub-property that I can't fathom out how to do.

Andy
--
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
 
In this instance you are working harder (much harder) than you need to. a simple abstraction would work. all web control inherit from WebControl. use this high level abstraction instead.
Code:
public class ColoChanger
{
   public void ChangeBackColor(WebControl control)
   {
      control.BackColor = GetColorFromSomewhere();
   }
}
client code would consume it like this
Code:
Page_Load()
{
      new ColoChanger().ChangeBackColor(MyCalendar);
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top