access to a property via string ?

  • Thread starter Thread starter aschenk
  • Start date Start date
A

aschenk

Hello Group,
is it possible to read/write a property value if the property-name is
in a string var ?

I know, this code don't work !!!:

Public Sub Change_Property(ByVal cn As Control, ByVal strProp As
String, ByVal Value As VariantType)

cn.<strProp> = value

End Sub

thanks, a.schenk
 
You can do it via reflection e.g.

cn.GetType().GetProperty("propName").SetValue(cn, newValue, Nothing)

If your property is non public you'll need to use the overload of
GetProperty which accepts a combination of BindingFlags

Peter
 
Try this:

Dim pi As System.Reflection.PropertyInfo
pi = cn.GetType().GetProperty(strProp,
BindingFlags.Public Or BindingFlags.NonPublic Or
BindingFlags.Static Or BindingFlags.Instance)
pi.SetValue(cn, Value, Nothing)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top