Using a variable for a name

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

Given the following code:

Private Sub ButtonShowTheIntValue_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ButtonShowTheIntValue.Click

MsgBox(String.Format("TheClassInstance.TheInt = {0:G}",
TheClassInstance.TheInt))

End Sub

The code above displays a simple message box that shows the value of the field
TheInt in the object TheClassInstance. If I rename the object or the field in
it's declaration I'll get an easily fixable error. However, there's the risk
of correcting the name of the variable, but leaving the string literal
unchanged. Is there somehow a way to replace the string literal
"TheClassInstance.TheInt" with a variable so that I would get a compilation
error if the field's name was to be changed in its declaration?
 
AFAIK, no, there is no direct way. Reflection allows you to discover types
fields at run time, but you need the name to identify an element. The only
workaround would be to attach an attribute to the field and use Reflection
at run time to locate the element whose attribute is that and then retrieve
the name.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 

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

Back
Top