Using a variable for a name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

    #1

    Using a variable for a name

    Given the following code:

    Private Sub ButtonShowTheIn tValue_Click(By Val sender As System.Object, ByVal e
    As System.EventArg s) Handles ButtonShowTheIn tValue.Click

    MsgBox(String.F ormat("TheClass Instance.TheInt = {0:G}",
    TheClassInstanc e.TheInt))

    End Sub

    The code above displays a simple message box that shows the value of the field
    TheInt in the object TheClassInstanc e. 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
    "TheClassInstan ce.TheInt" with a variable so that I would get a compilation
    error if the field's name was to be changed in its declaration?














  • Carlos J. Quintero [.NET MVP]

    #2
    Re: Using a variable for a name

    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:
    MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.


    "Alex" <pleazze@no.mai l> escribió en el mensaje
    news:ebSGk1uXFH A.3488@tk2msftn gp13.phx.gbl...[color=blue]
    > Given the following code:
    >
    > Private Sub ButtonShowTheIn tValue_Click(By Val sender As System.Object,
    > ByVal e
    > As System.EventArg s) Handles ButtonShowTheIn tValue.Click
    >
    > MsgBox(String.F ormat("TheClass Instance.TheInt = {0:G}",
    > TheClassInstanc e.TheInt))
    >
    > End Sub
    >
    > The code above displays a simple message box that shows the value of the
    > field
    > TheInt in the object TheClassInstanc e. 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
    > "TheClassInstan ce.TheInt" with a variable so that I would get a
    > compilation
    > error if the field's name was to be changed in its declaration?
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >[/color]


    Comment

    Working...