So, let's say I have this code (VB.Net):
Sub Main()
dim xxx as string = "HELLO WORLD"
DetectName(xxx)
End Sub
Public Sub (theVariable as string)
dim output as string = "The Value: {0} was stored in the variable named: {1}."
debug.writeline(string.format(output, theVariable, What Goes Here????))
End Sub
Te output I want to see is:
The Value: HELLO WORLD was stored in the variable named: xxx.
Is this somehow possible, through reflection possibly?? (And if you definitively know it isn't possible, thats useful to know also).
In case you are wondering why I want to do this...I am trying to find some decent way to write a custom binding class (for forms, not grids) for asp.net because I can't seem to find anything that works well enough to be worth the trouble of using it. (NOTE: I am working with webforms and cannot use MVC to solve this problem).
I'm thinking my convention for supplying the binding information will be something like:
asp:TextBox id="myClassInstanceName.myPropertyName" ...
So, then I can have a function like so:
Public Sub Bind(webForm as System.Web.UI.Page, bindingObjects as HashTable)
For each ctl in webForm.flattenedControls that contains "." within ID
' Here, which property is set would actually depends on control type
' magicGetValue() would find the property class instance within bindingObjects, which is indexed by variable *NAME* (not type), and then return the proper property value via reflection
ctl.Text = magicGetValue(ctl.ID, bindingObjects)
End Sub
End Sub
Personally, I think this is a much easier syntax and much more powerful than the recommended asp:FormView, Bind("") approach. For example:
- if I want to implement the ability to render as read only, its quite a simple change in my underlying Bind function.
- if I want to have more than one class used to populate the UI, I can
- if I want to traverse a rich domain model, I can, ie:
asp:TextBox id="_Customer.Address.State"
What I don't like is:
- relies upon reflection
- loosely typed (no compile time checking)
- I'd rather store the binding information somewhere other than ID, but don't know where else it could go within the aspx definition. Moving the binding declaration to the code behind would be better in some ways, but worse in others.
If anyone has any insight or advice, I'd be very grateful!