get name of variable as string?

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

    get name of variable as string?

    Shot in the dark, since I know C# doesn't have macros, and thus can't
    have a stringizer operator, but I know that you can get the name of
    enums as strings, so maybe you can do the same with an ordinary
    variable (like an int) somehow. Is it possible? int.ToString()
    returns the value of the int as a string, not the name, so I am
    thinking 'no'.

    Zytan

  • Bill Butler

    #2
    Re: get name of variable as string?

    "Zytan" <zytanlithium@g mail.comwrote in message
    news:1175215785 .890002.182370@ d57g2000hsg.goo glegroups.com.. .
    Shot in the dark, since I know C# doesn't have macros, and thus can't
    have a stringizer operator, but I know that you can get the name of
    enums as strings, so maybe you can do the same with an ordinary
    variable (like an int) somehow. Is it possible? int.ToString()
    returns the value of the int as a string, not the name, so I am
    thinking 'no'.
    >
    What are you attempting to do that you need this behavior?


    Comment

    • =?Utf-8?B?cm1hY2lhcw==?=

      #3
      RE: get name of variable as string?

      What you want to do is called Reflection. Google "C# Reflection". Also take
      a look at the System.Reflecti on namespace.

      "Zytan" wrote:
      Shot in the dark, since I know C# doesn't have macros, and thus can't
      have a stringizer operator, but I know that you can get the name of
      enums as strings, so maybe you can do the same with an ordinary
      variable (like an int) somehow. Is it possible? int.ToString()
      returns the value of the int as a string, not the name, so I am
      thinking 'no'.
      >
      Zytan
      >
      >

      Comment

      • MBR

        #4
        Re: get name of variable as string?


        "Bill Butler" <qwerty@asdf.co mwrote in message
        news:xdZOh.1125 19$fo5.27979@tr nddc07...
        "Zytan" <zytanlithium@g mail.comwrote in message
        news:1175215785 .890002.182370@ d57g2000hsg.goo glegroups.com.. .
        >Shot in the dark, since I know C# doesn't have macros, and thus can't
        >have a stringizer operator, but I know that you can get the name of
        >enums as strings, so maybe you can do the same with an ordinary
        >variable (like an int) somehow. Is it possible? int.ToString()
        >returns the value of the int as a string, not the name, so I am
        >thinking 'no'.
        >>
        >
        What are you attempting to do that you need this behavior?
        There seem to be several postings a week here ostensibly about reflection,
        when ordinary non-meta-level techniques are perfectly suitable.
        It may not be the case here, but I blame Perl for all this name vs. named
        confusion... (Always good to have something to blame :))




        --
        Posted via a free Usenet account from http://www.teranews.com

        Comment

        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

          #5
          Re: get name of variable as string?

          Zytan wrote:
          Shot in the dark, since I know C# doesn't have macros, and thus can't
          have a stringizer operator, but I know that you can get the name of
          enums as strings, so maybe you can do the same with an ordinary
          variable (like an int) somehow. Is it possible? int.ToString()
          returns the value of the int as a string, not the name, so I am
          thinking 'no'.
          To my best knowledge C# does not have anything
          similar to macro and stringifier operator.

          What you can get for enums is the string representation
          of the value not of the variable name.

          Arne

          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: get name of variable as string?

            This isn't correct. There is a one-to-many relationship between a class
            and the variables that can have a reference to it. That being said, you
            can't figure out all the references (the CLR knows, but that info isn't
            exposed to you) that are held on any class (or in any variable).


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "rmacias" <rmacias@newsgr oup.nospamwrote in message
            news:989ECD65-A698-4532-BC87-38A6DA04A3DD@mi crosoft.com...
            What you want to do is called Reflection. Google "C# Reflection". Also
            take
            a look at the System.Reflecti on namespace.
            >
            "Zytan" wrote:
            >
            >Shot in the dark, since I know C# doesn't have macros, and thus can't
            >have a stringizer operator, but I know that you can get the name of
            >enums as strings, so maybe you can do the same with an ordinary
            >variable (like an int) somehow. Is it possible? int.ToString()
            >returns the value of the int as a string, not the name, so I am
            >thinking 'no'.
            >>
            >Zytan
            >>
            >>

            Comment

            • =?ISO-8859-1?Q?G=F6ran_Andersson?=

              #7
              Re: get name of variable as string?

              Zytan wrote:
              Shot in the dark, since I know C# doesn't have macros, and thus can't
              have a stringizer operator, but I know that you can get the name of
              enums as strings, so maybe you can do the same with an ordinary
              variable (like an int) somehow. Is it possible? int.ToString()
              returns the value of the int as a string, not the name, so I am
              thinking 'no'.
              >
              Zytan
              >
              I suppose that it might be possible to use reflection to get the name,
              but why? What are you planning to use it for?

              --
              Göran Andersson
              _____
              Göran Anderssons privata hemsida.

              Comment

              • PhilipDaniels@foo.com

                #8
                Re: get name of variable as string?

                On 29 Mar 2007 17:49:45 -0700, "Zytan" <zytanlithium@g mail.comwrote:
                >Shot in the dark, since I know C# doesn't have macros, and thus can't
                >have a stringizer operator, but I know that you can get the name of
                >enums as strings, so maybe you can do the same with an ordinary
                >variable (like an int) somehow. Is it possible? int.ToString()
                >returns the value of the int as a string, not the name, so I am
                >thinking 'no'.
                >
                >Zytan
                In general, no, see Nicholas' response.

                A few further points:

                1. It is possible to figure out the name of the fields in a type using
                Reflection by calling the Type.GetFields( ) method. That's because
                these names form a part of the assembly's metadata. That's very
                different from figuring out the name of an instance of the type though
                - indeed an instance may not even have a name, it might be living as
                an element in a dictionary for example.

                2. It is not possible to figure out the name of a local variable
                because their names are not compiled into the assembly's metadata.


                However, both Reflector and ILDASM *can* get the names of local
                variables if the PDB file is available. (Don't even go there, you
                don't want to do this!)



                --
                Philip Daniels

                Comment

                • Zytan

                  #9
                  Re: get name of variable as string?

                  What are you attempting to do that you need this behavior?

                  Two things:

                  1. I want a function like Write(i) which will contstruct the string "i
                  = 45.", where 45 is the value that i is. This is for debugging, and
                  saves typing WriteLine("i = "+i); With macros and a stringizing
                  operator, this can be easily, but C# does not support this.

                  2. I want to get the name of a class. Again for debugging. I have a
                  WriteLine method that will state who called it by going up the call
                  stack: caller -writeline. But, I have wrappers for this WriteLine
                  method, again to save typing (this is debugging, remember, so I like
                  to speed things up), and this wrapper exists between the caller and
                  the ending function: caller -wrapper -writeline. So, you can see
                  that I have to know how far back up the callstack to go. If I had the
                  class name, I could go until I am out of the class, and get 'caller'
                  every time.

                  Zytan

                  Comment

                  • Zytan

                    #10
                    Re: get name of variable as string?

                    To my best knowledge C# does not have anything
                    similar to macro and stringifier operator.
                    No, it doesn't, as I stated, I know this. But, I thought maybe
                    there's another way.
                    What you can get for enums is the string representation
                    of the value not of the variable name.
                    I meant that it returns a string that is equivalent to what you type
                    in code. I didn't mean to imply it returns the hidden integer that
                    exists inside (that you don't have access to unless you explicitly
                    type cast it). Thus, the value returned is exactly the 'value' you
                    must type in code (not the hidden value that, for the most part, you
                    shouldn't ever see or use). This is precisely what I want for a
                    variable of type int. I want "i" from a variable "int i;", just as I
                    get "Google" from "MyEnum.Goo gle"

                    Zytan

                    Comment

                    • Zytan

                      #11
                      Re: get name of variable as string?

                      This isn't correct. There is a one-to-many relationship between a class
                      and the variables that can have a reference to it. That being said, you
                      can't figure out all the references (the CLR knows, but that info isn't
                      exposed to you) that are held on any class (or in any variable).
                      What isn't correct? Sorry, Nicholas, I don't follow you at all. I am
                      not looking for references, or multiple references, and I don't care
                      about any of the internals. I just want to get a string "i" if my
                      variable is named "i", regardless of what class or function or inner
                      loop it is declared in. I know it's probably impossible. Like I
                      said, just a shot in the dark, just in case.

                      Zytan

                      Comment

                      • Zytan

                        #12
                        Re: get name of variable as string?

                        I suppose that it might be possible to use reflection to get the name,
                        but why? What are you planning to use it for?
                        Ah, reflection, I know so little about this. I want it for debugging
                        purposes, to quickly write out a variable and its value, instead of
                        labouriously typing in WriteLine("i = "+i); each time. Please see
                        more detailed answer to your question in an another reply.

                        Zytan

                        Comment

                        • Zytan

                          #13
                          Re: get name of variable as string?

                          In general, no, see Nicholas' response.

                          Thought so.
                          A few further points:
                          >
                          1. It is possible to figure out the name of the fields in a type using
                          Reflection by calling the Type.GetFields( ) method. That's because
                          these names form a part of the assembly's metadata. That's very
                          different from figuring out the name of an instance of the type though
                          - indeed an instance may not even have a name, it might be living as
                          an element in a dictionary for example.
                          Right. And I want information on the instance. I don't even really
                          care about the type itself (as long as it supports ToString(), which
                          all types do).
                          2. It is not possible to figure out the name of a local variable
                          because their names are not compiled into the assembly's metadata.
                          Ok.
                          However, both Reflector and ILDASM *can* get the names of local
                          variables if the PDB file is available. (Don't even go there, you
                          don't want to do this!)
                          Well, it's just for debugging, so I may want to go there. It's
                          nothing that'll exist in production code. But, I think you're warning
                          me that this is a seriously complex solution to a simple matter.
                          However, if I solved it, I could throw it into a black box and lock it
                          and never open it again.

                          Thanks for your reply,

                          Zytan

                          Comment

                          • Zytan

                            #14
                            Re: get name of variable as string?

                            2. I want to get the name of a class.

                            this.Name works for instance classes. I need it for a static class.

                            Zytan

                            Comment

                            • Zytan

                              #15
                              Re: get name of variable as string?

                              2. I want to get the name of a class.
                              >
                              this.Name works for instance classes. I need it for a static class.
                              Solution:

                              System.Diagnost ics.StackTrace callStack = new
                              System.Diagnost ics.StackTrace( );
                              System.Diagnost ics.StackFrame frame = callStack.GetFr ame(0);
                              System.Reflecti on.MethodBase method = frame.GetMethod ();
                              string name = method.Declarin gType.Name;

                              Zytan

                              Comment

                              Working...