Home All Groups Group Topic Archive Search About

indirectly setting a property of a class

Author
16 May 2006 1:32 AM
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

Author
16 May 2006 3:47 AM
Ken Tucker [MVP]
Hi,

        Yes you use reflection to do that.  The propertyinfo class has a
setvalue method.  There are some examples on the vb-tips website.

http://www.vb-tips.com/default.aspx?ID=ec2003ce-b05e-4c3f-8448-7d02cf64d537

http://www.vb-tips.com/default.aspx?ID=2d0256fd-48ea-4a9c-a993-530a91299771

http://msdn2.microsoft.com/en-us/library/system.reflection.propertyinfo.setvalue.aspx

Ken
-----------
Show quoteHide quote
"Richy" <moviekni***@gmail.com> wrote in message
news:1147743175.148891.30610@y43g2000cwc.googlegroups.com...
>
> 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
>
Author
16 May 2006 5:20 AM
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
Author
16 May 2006 11:09 AM
Nick Hall
Inline: -


Show quoteHide quote
"Richy" <moviekni***@gmail.com> wrote in message
news:1147756836.754022.250430@j33g2000cwa.googlegroups.com...
>
> 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
Author
18 May 2006 3:54 AM
Richy
That worked a treat!

Cheers,

Richy
Author
16 May 2006 7:29 AM
Herfried K. Wagner [MVP]
"Richy" <moviekni***@gmail.com> schrieb:
> 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.

Calling a method by its name
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=callbyname&lang=en>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>