|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
indirectly setting a property of a classHow 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 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 > 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 Inline: -
Show quoteHide quote "Richy" <moviekni***@gmail.com> wrote in message That's because the overload that takes only a string is specifying 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. 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) > Hope this helps,> However, I need it to disregard the case and match "specularSharpness" > with "SpecularSharpness". > > What am I doing wrong, if anything? > > Thanks, > > Richy > Nick Hall "Richy" <moviekni***@gmail.com> schrieb: Calling a method by its name> 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. <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/>
Check if Component is not disposed?
Change Screen Resolution in VB.NET????? Problem adding currency in VB6 can I write a select ..where.. against a table in a dataset? force next iteration in for...next How many jobs are waiting in the printer's queue Help needed to locate Documentation Authoring Tool for VB.Net 2.0 How to get Actual field name from TextBox Control WMI - Win32_LogicalDisk - Status Get PenDrive IDs |
|||||||||||||||||||||||