Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Programatically determine variable name

Programatically determine variable name

Scheduled Pinned Locked Moved C#
tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    redivider
    wrote on last edited by
    #1

    Is there a way I can determine the name of the variable that is being accessed. In the following example, when I call Item22.Description (or any other property of BigClass), I'd like to be able to find variablename (Item22 in this case).

    class BigClass
    {
    public static Hashtable DescriptionsTable; //this would be a singleton
    DataItem Item01;
    ....
    DataItem Item64;
    }

    class DataItem
    {
    double _Value;
    string Description
    { get { return BigClass.DescriptionsTable[variablename]; } }
    }

    If I don't have access to this name I figure I'll have to add a second property to BigClass for each Item (ex. Item22Description...), which seems like a waste. And I don't want to have the descriptions in each BigClass because they can get quite lengthy and there will be hundreds of BigClass instances at a time. Any ideas? If there's another option, how could I re-architect without wasting too much space?

    E G 2 Replies Last reply
    0
    • R redivider

      Is there a way I can determine the name of the variable that is being accessed. In the following example, when I call Item22.Description (or any other property of BigClass), I'd like to be able to find variablename (Item22 in this case).

      class BigClass
      {
      public static Hashtable DescriptionsTable; //this would be a singleton
      DataItem Item01;
      ....
      DataItem Item64;
      }

      class DataItem
      {
      double _Value;
      string Description
      { get { return BigClass.DescriptionsTable[variablename]; } }
      }

      If I don't have access to this name I figure I'll have to add a second property to BigClass for each Item (ex. Item22Description...), which seems like a waste. And I don't want to have the descriptions in each BigClass because they can get quite lengthy and there will be hundreds of BigClass instances at a time. Any ideas? If there's another option, how could I re-architect without wasting too much space?

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      The approach you are using is probably not ideal. The only thing I can think of is to eliminate the members and add an indexor. Ideally, however, each property should access an individual member. It is the most logical method.

      Need a C# Consultant? I'm available.
      Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

      1 Reply Last reply
      0
      • R redivider

        Is there a way I can determine the name of the variable that is being accessed. In the following example, when I call Item22.Description (or any other property of BigClass), I'd like to be able to find variablename (Item22 in this case).

        class BigClass
        {
        public static Hashtable DescriptionsTable; //this would be a singleton
        DataItem Item01;
        ....
        DataItem Item64;
        }

        class DataItem
        {
        double _Value;
        string Description
        { get { return BigClass.DescriptionsTable[variablename]; } }
        }

        If I don't have access to this name I figure I'll have to add a second property to BigClass for each Item (ex. Item22Description...), which seems like a waste. And I don't want to have the descriptions in each BigClass because they can get quite lengthy and there will be hundreds of BigClass instances at a time. Any ideas? If there's another option, how could I re-architect without wasting too much space?

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        You can't get the variable name from the object, as you are not accessing the variable. You are just accessing an object that the variable happens to be referencing. There is no backwards reference from the object to the variable. If you want the DataItem object to know the name of the variable, you have to store the name in the object.

        Despite everything, the person most likely to be fooling you next is yourself.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups