indirectly setting a property of a class

R

Richy

Hi,

How do I indirectly set the property of a class? Example:

Dim MyClass as aClass
Dim MyProperty As String = "Name"
Dim MyValue As String = "Test"

I want to be able to set the property whose name is stored in
MyProperty, of the class MyClass, to the value in MyValue. So in this
example, this equates to:

MyClass.Name = "Test"

Do I have to use reflection?

Thanks,

Richy
 
R

Richy

Thanks!

One more question...I am trying to retrieve the propertyinfo from a
variable that contains the name of the property (in this case,
"specularSharpness") but my property in the class is SpecularSharpness.
The case doesn't match. I have tried using the BindingFlags.IgnoreCase
but that just returns Nothing e.g.

Dim pi As PropertyInfo = t.GetProperty(name, BindingFlags.IgnoreCase)

Even if the varialbe name precisely matches the case of the property
name, it returns nothing and if I remove the IgnoreCase option it
works.

However, I need it to disregard the case and match "specularSharpness"
with "SpecularSharpness".

What am I doing wrong, if anything?

Thanks,

Richy
 
N

Nick Hall

Inline: -


Richy said:
Thanks!

One more question...I am trying to retrieve the propertyinfo from a
variable that contains the name of the property (in this case,
"specularSharpness") but my property in the class is SpecularSharpness.
The case doesn't match. I have tried using the BindingFlags.IgnoreCase
but that just returns Nothing e.g.

Dim pi As PropertyInfo = t.GetProperty(name, BindingFlags.IgnoreCase)

Even if the varialbe name precisely matches the case of the property
name, it returns nothing and if I remove the IgnoreCase option it
works.

That's because the overload that takes only a string is specifying
additional BindingFlags internally- specifically Instance, Static and
Public. Your code needs to become the following to be equivalent: -

Dim pi As PropertyInfo = t.GetProperty(name, BindingFlags.IgnoreCase Or
BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.Static)
However, I need it to disregard the case and match "specularSharpness"
with "SpecularSharpness".

What am I doing wrong, if anything?

Thanks,

Richy

Hope this helps,

Nick Hall
 

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

Top