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. How to convert variable name to string?

How to convert variable name to string?

Scheduled Pinned Locked Moved C#
tutorialquestion
8 Posts 6 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.
  • C Offline
    C Offline
    Chesnokov Yuriy
    wrote on last edited by
    #1

    How to convert variable name to string?

    class SomeClass
    {
    public int SomeVariable;
    };
    string className = typeof(SomeClass).Name; // returns "SomeClass"
    // need to obtain in the same way "SomeVariable"

    Чесноков

    O P L S 5 Replies Last reply
    0
    • C Chesnokov Yuriy

      How to convert variable name to string?

      class SomeClass
      {
      public int SomeVariable;
      };
      string className = typeof(SomeClass).Name; // returns "SomeClass"
      // need to obtain in the same way "SomeVariable"

      Чесноков

      O Offline
      O Offline
      Orcun Iyigun
      wrote on last edited by
      #2

      if i understand you correct; What you need to look at is Boxing and Unboxing in C#. MSDN Boxing and Unboxing[^] boxing-and-unboxing-c#/[^]

      1 Reply Last reply
      0
      • C Chesnokov Yuriy

        How to convert variable name to string?

        class SomeClass
        {
        public int SomeVariable;
        };
        string className = typeof(SomeClass).Name; // returns "SomeClass"
        // need to obtain in the same way "SomeVariable"

        Чесноков

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        If you are using .NET 3 or above, you can use an expression tree to accomplish this. Here's an example of retrieving the field -

        private static FieldInfo GetFieldInfo<T>(Expression<Func<T>> field)
        {
        FieldInfo fi = (field.Body as MemberExpression).Member as FieldInfo;
        if (fi == null)
        throw new ArgumentException("The field parameter should point to a valid field");
        return fi;
        }

        I'm not a stalker, I just know things. Oh by the way, you're out of milk.

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Onyx

        M 1 Reply Last reply
        0
        • C Chesnokov Yuriy

          How to convert variable name to string?

          class SomeClass
          {
          public int SomeVariable;
          };
          string className = typeof(SomeClass).Name; // returns "SomeClass"
          // need to obtain in the same way "SomeVariable"

          Чесноков

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Can't be done in general as: - Values don't have names; - Most objects don't have names (major exception is WinForm Controls, they have a Name property which is heavily used by Visual Designer); - Variables have a name, obviously, inside the source file; the information is not preserved when compilation finishes. That is why Reflector uses mock names for everything that is a local variable. Hoowever data members such as your SomeVariable do have meta-data inside the class, so you can use reflection to enumerate them, or obtain the value of a variable whose name you know at run-time.

          Chesnokov Yuriy wrote:

          string className = typeof(SomeClass).Name;

          is just silly. Less typing is required to do string className = "SomeVariable"; I suggest you describe the real problem you are trying to solve, not a detail along the wrong path you seem to have chosen; and/or read up on reflection. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          C 1 Reply Last reply
          0
          • C Chesnokov Yuriy

            How to convert variable name to string?

            class SomeClass
            {
            public int SomeVariable;
            };
            string className = typeof(SomeClass).Name; // returns "SomeClass"
            // need to obtain in the same way "SomeVariable"

            Чесноков

            S Offline
            S Offline
            SledgeHammer01
            wrote on last edited by
            #5

            Use reflection. I.e. PropertyInfo.Name for the variable names.

            1 Reply Last reply
            0
            • C Chesnokov Yuriy

              How to convert variable name to string?

              class SomeClass
              {
              public int SomeVariable;
              };
              string className = typeof(SomeClass).Name; // returns "SomeClass"
              // need to obtain in the same way "SomeVariable"

              Чесноков

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Are you looking into the classic infoof[^] problem?

              I'm not a stalker, I just know things. Oh by the way, you're out of milk.

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Onyx

              1 Reply Last reply
              0
              • P Pete OHanlon

                If you are using .NET 3 or above, you can use an expression tree to accomplish this. Here's an example of retrieving the field -

                private static FieldInfo GetFieldInfo<T>(Expression<Func<T>> field)
                {
                FieldInfo fi = (field.Body as MemberExpression).Member as FieldInfo;
                if (fi == null)
                throw new ArgumentException("The field parameter should point to a valid field");
                return fi;
                }

                I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Onyx

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                That little snippet deserves T&T status.

                Never underestimate the power of human stupidity RAH

                1 Reply Last reply
                0
                • L Luc Pattyn

                  Can't be done in general as: - Values don't have names; - Most objects don't have names (major exception is WinForm Controls, they have a Name property which is heavily used by Visual Designer); - Variables have a name, obviously, inside the source file; the information is not preserved when compilation finishes. That is why Reflector uses mock names for everything that is a local variable. Hoowever data members such as your SomeVariable do have meta-data inside the class, so you can use reflection to enumerate them, or obtain the value of a variable whose name you know at run-time.

                  Chesnokov Yuriy wrote:

                  string className = typeof(SomeClass).Name;

                  is just silly. Less typing is required to do string className = "SomeVariable"; I suggest you describe the real problem you are trying to solve, not a detail along the wrong path you seem to have chosen; and/or read up on reflection. :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  C Offline
                  C Offline
                  Chesnokov Yuriy
                  wrote on last edited by
                  #8

                  Luc Pattyn wrote:

                  I suggest you describe the real problem you are trying to solve, not a detail along the wrong path you seem to have chosen; and/or read up on reflection.

                  It is in the question, given class variable as input convert it to string:

                  string name = FieldToString(SomeClass.SomeVariable); // name = "SomeVariable"

                  Чесноков

                  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