#### Archived

This topic is now archived and is closed to further replies.

# Reflection.PropertyInfo.SetValue Problem (.NET)

This topic is 5460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I'm trying to set the value of a property for an object I created dynamically at runtime. I know my way around the Reflection namespace, I've followed the examples in the help file, and checked other examples on sites like dotnet247.com. As far as I can tell, I'm doing everything correctly (but if that were the case, then I wouldn't have this problem). The problem is that the line in question doesn't appear to be doing anything, ie, my call to SetValue doesn't have any effect. I need another set of eyes to look over this and tell me if I'm missing something (VB):
Private Sub SetObjectProperty(ByRef theObject As Object, ByVal PropertyName As String, ByVal PropertyValue As String)

Dim WorkingType As System.Type = theObject.GetType

Dim theProperty As PropertyInfo = WorkingType.GetProperty(PropertyName)

' // This is the line that's not working as it should.
theProperty.SetValue(theObject, CSng(PropertyValue), Nothing)

End Sub         
This is a stripped down version of the full method, but it shows what I'm trying to do. Every line works as it should, except the one I pointed out. Here's a few things about it: - The object whose property I am trying to set is one I created. In fact, here's the property code for my test property (C#):
public float FormWidth
{
get { return this.GetWidth(); }
set { this.Width.Init(FormWidth); }
}      
- GetValue works without any problems. - If I GetValue before the call, I get what I passed to the property in the constructor (object was declared a long time ago). - If I GetValue after the call, I get the same value as I did before the call. Like the SetValue function didn't do anything. - Yes, PropertValue is a string when it is passed to the function, that's the way I need it to be. This string contains a number, which I convert to a Single (float) for the SetValue method. I know they have to be exactly the same type, and Single in VB is float in C#. - The line in question DOES NOT generate any exceptions. Bummer. - I have MsgBox's in place to test the value before and after the call. I hope that covers everything. Can someone take a look and tell me where I might've messed up here? I can't seem to find it on my own :/ Thanks [edited by - Tri on March 7, 2003 1:12:48 PM] [edited by - Tri on March 7, 2003 1:53:55 PM]

##### Share on other sites
I think the problem is with the FormWidth property, not your SetObjectProperty() code. When trying it out with a simple driver property, your SetObjectProperty() method works fine. And there isn''t enough given code to tell why it doesn''t work on your FormWidth property.

##### Share on other sites
Thank you for your response. It was helpful and led me to a solution that worked.

set { this.Width.Init(value ); }

I had to use value instead of FormWidth. I don't know why it had to be that, but it works and I'm happy. I think in VB I could get away with using FormWidth.

I couldn't give the code for the set routine because that stuff is secret (suffice to say it's tested and works as it should).

So thanks again

[edited by - Tri on March 8, 2003 2:40:38 PM]