Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Converting a string to an object property
Message
From
14/06/2008 12:05:00
 
 
To
14/06/2008 02:10:58
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 8.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01323589
Message ID:
01324129
Views:
15
>Nah, Bonnie. The problem is that coming into the parsing routine it comes down to knowing what the control is to match the parsed name. If you don't, you are stuck with a generic Control class init and that doesn't work when looking for a non-generic property.

No, not really ... it should still work just fine. Part of the problem you might have run into could be a case-sensitivity issue with the .GetField() and .GetProperty() methods ... being a VB guy, you're not used to case-sensitivity. <g> You can specify to ignore the CaseSensitivity with the BindingFlags. I know you said you already found a workaround, so maybe you don't want to bother with this, but this looks like it works fairly well and it's not that complicated:
string myProps = "MyComboBox.SelectedValue";
string[] info = myProps.Split('.');

object control = this.GetType().GetField(info[0], 
    BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(this);
object property = control.GetType().GetProperty(info[1], 
    BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(control, null);
(Should be a fairly straightforward conversion from my C# code to VB).

To make a complete routine, you might also want to add BindingFlags.Static to the list.

~~Bonnie



>
>Like I said, I found a workaround but....doggone it, this should have been easy.
>
>>Paul's suggestion should work, or at least point you in the right direction ... perhaps something got lost in the translation to VB?
>>
>>~~Bonnie
>>
>>
>>
>>>No, I suspected before I posted it. I ain't that dumb.
>>>
>>>Postscript: Tried Paul and Mike's solutions and neither worked...although kudos for trying. I kinda gave up on that approach and moved on. But it occured to me that .Net really needs an equivalent to the EVALUATE() function. I don't want to use the word "literal" since .Net has already hijacked it for other uses but maybe we need a CTxt() function to convert a string to what we call a literal. I was talking to some .Net gurus today and they agreed. Man, that would be useful.
>>>
>>>
>>>>>Friggin's .Net weirdness.
>>>>>
>>>>>Thanks, Paul. I suspected the solution had something to do with reflection .... I'll try this tomorrow. Appreciate it.
>>>>>
>>>>
>>>>Only suspected after 3 people suggested it? :)
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform