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 does one get the name of a variable which is passed to a method?

How does one get the name of a variable which is passed to a method?

Scheduled Pinned Locked Moved C#
question
12 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.
  • J Offline
    J Offline
    jeffb42
    wrote on last edited by
    #1

    I wish to do something along the lines of: MyObject theObject = new MyObject(); DumpObject(theObject); ... elsewhere... public void DumpObject(Object obj) { string name = GetName(obj); // <<--- This is the part I'm missing System.Console.WriteLine("The name of the object is " + name); // this will print out: // "The name of the object is theObject" } I thought I could use reflection to get the name of the object but I'm not having any luck. I know this must be possible, but I'm stumped. TIA, Jeff

    M L G N 4 Replies Last reply
    0
    • J jeffb42

      I wish to do something along the lines of: MyObject theObject = new MyObject(); DumpObject(theObject); ... elsewhere... public void DumpObject(Object obj) { string name = GetName(obj); // <<--- This is the part I'm missing System.Console.WriteLine("The name of the object is " + name); // this will print out: // "The name of the object is theObject" } I thought I could use reflection to get the name of the object but I'm not having any luck. I know this must be possible, but I'm stumped. TIA, Jeff

      M Offline
      M Offline
      Mbah Dhaim
      wrote on last edited by
      #2

      how you can get object name if it doesn't have Name property :confused:, or you just want to know its type?

      dhaim program is hobby that make some money as side effect :)

      J 1 Reply Last reply
      0
      • J jeffb42

        I wish to do something along the lines of: MyObject theObject = new MyObject(); DumpObject(theObject); ... elsewhere... public void DumpObject(Object obj) { string name = GetName(obj); // <<--- This is the part I'm missing System.Console.WriteLine("The name of the object is " + name); // this will print out: // "The name of the object is theObject" } I thought I could use reflection to get the name of the object but I'm not having any luck. I know this must be possible, but I'm stumped. TIA, Jeff

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

        Hi Jeff, 1. you are confusing objects with references. An object can have a name (say you provide a Name property), a reference only refers to the object. Also there could be many references to the same object. How about

        MyObject theObject=new MyObject("Paul");
        MyObject anotherReference=theObject;

        DumpObject(anotherReference);

        Now which name would you want to get? The only one you will be able to get is "Paul" provided it got stored somewhere *inside* the object. 2. It is however possible to get the name of the reference itself, using reflection. But then you have to pass the reference itself, hence something like:

        MyObject theObject=new MyObject("Paul");
        MyObject anotherReference=theObject;

        DumpObject(ref anotherReference);

        Now DumpObject gets hold of anotherReference itself, and through reflection should be able to get at its name. Haven't done this though, when names are important I store them inside the object. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Voting for dummies? No thanks. X|


        J 1 Reply Last reply
        0
        • M Mbah Dhaim

          how you can get object name if it doesn't have Name property :confused:, or you just want to know its type?

          dhaim program is hobby that make some money as side effect :)

          J Offline
          J Offline
          jeffb42
          wrote on last edited by
          #4

          No, I can't assume that the object has a Name property, and I don't want to know the type (which I'd just do Type t = myObject.GetType()). I'm developing a (generic) debugging routine, so if I passed a variable named someObject: MyMethod(someObject); ...at runtime I want to get "someObject". I'd like to, and I'm sure there is a way, but I'm not seeing how to do it. Thanks though, Jeff

          1 Reply Last reply
          0
          • J jeffb42

            I wish to do something along the lines of: MyObject theObject = new MyObject(); DumpObject(theObject); ... elsewhere... public void DumpObject(Object obj) { string name = GetName(obj); // <<--- This is the part I'm missing System.Console.WriteLine("The name of the object is " + name); // this will print out: // "The name of the object is theObject" } I thought I could use reflection to get the name of the object but I'm not having any luck. I know this must be possible, but I'm stumped. TIA, Jeff

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

            No, it's not possible. An instance of an object doesn't have a name, neither has the reference to it. The variable holding the reference does have a name, but that is not passed along to the method, neither is a reference to the variable. Also, unless the assembly is started with debug information, I don't think that you can even get the name of a local variable. There might not even be a variable holding the reference. Consider these examples: Here you have no variable at all:

            DumpObject(new MyObject());

            Here the variable is just a member of a KeyValuePair structure in a dictionary:

            Dictionary<int,MyObject> MyDictionary = new Dictionary<int,MyObject>();
            MyDictionary.Add(42, new MyObject());
            DumpObject(MyDictionary[42]);

            What would you imagine that the name should be; "Value", "42" or "MyDictionary"?

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

            L 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi Jeff, 1. you are confusing objects with references. An object can have a name (say you provide a Name property), a reference only refers to the object. Also there could be many references to the same object. How about

              MyObject theObject=new MyObject("Paul");
              MyObject anotherReference=theObject;

              DumpObject(anotherReference);

              Now which name would you want to get? The only one you will be able to get is "Paul" provided it got stored somewhere *inside* the object. 2. It is however possible to get the name of the reference itself, using reflection. But then you have to pass the reference itself, hence something like:

              MyObject theObject=new MyObject("Paul");
              MyObject anotherReference=theObject;

              DumpObject(ref anotherReference);

              Now DumpObject gets hold of anotherReference itself, and through reflection should be able to get at its name. Haven't done this though, when names are important I store them inside the object. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Voting for dummies? No thanks. X|


              J Offline
              J Offline
              jeffb42
              wrote on last edited by
              #6

              Luc Pattyn wrote:

              MyObject theObject=new MyObject("Paul"); MyObject anotherReference=theObject; DumpObject(anotherReference); Now which name would you want to get? The only one you will be able to get is "Paul" provided it got stored somewhere *inside* the object.

              With the above code, I'd want to get the string "anotherReference" inside of DumpObject(). If the code was DumpObject(theObject), I'd want to get the string "theObject" inside of DumpObject().

              Luc Pattyn wrote:

              It is however possible to get the name of the reference itself, using reflection.

              Luc Pattyn wrote:

              and through reflection should be able to get at its name.

              That's the part I'm not able to do. Thanks though, Jeff

              1 Reply Last reply
              0
              • G Guffa

                No, it's not possible. An instance of an object doesn't have a name, neither has the reference to it. The variable holding the reference does have a name, but that is not passed along to the method, neither is a reference to the variable. Also, unless the assembly is started with debug information, I don't think that you can even get the name of a local variable. There might not even be a variable holding the reference. Consider these examples: Here you have no variable at all:

                DumpObject(new MyObject());

                Here the variable is just a member of a KeyValuePair structure in a dictionary:

                Dictionary<int,MyObject> MyDictionary = new Dictionary<int,MyObject>();
                MyDictionary.Add(42, new MyObject());
                DumpObject(MyDictionary[42]);

                What would you imagine that the name should be; "Value", "42" or "MyDictionary"?

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

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

                Right. What was I thinking again? so only solution is to put a Name inside the object, and forget reference names altogether. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Voting for dummies? No thanks. X|


                P 1 Reply Last reply
                0
                • L Luc Pattyn

                  Right. What was I thinking again? so only solution is to put a Name inside the object, and forget reference names altogether. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Voting for dummies? No thanks. X|


                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  But then, what's the name of the Name? :-D

                  L 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    But then, what's the name of the Name? :-D

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

                    .NET 4.0 is rumored to provide introspection for that very purpose. :laugh:

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    Voting for dummies? No thanks. X|


                    J 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      .NET 4.0 is rumored to provide introspection for that very purpose. :laugh:

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      Voting for dummies? No thanks. X|


                      J Offline
                      J Offline
                      jeffb42
                      wrote on last edited by
                      #10

                      That's cool, but I can't wait that long. For now what I'm going to do is something like: DumpObject(theObject, "theObject"); Thanks, Jeff

                      L 1 Reply Last reply
                      0
                      • J jeffb42

                        That's cool, but I can't wait that long. For now what I'm going to do is something like: DumpObject(theObject, "theObject"); Thanks, Jeff

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

                        OK. FYI: There used to be a little preprocessor trick to automate that in C and C++ (using some ## magic), but that is not available in C#... :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        Voting for dummies? No thanks. X|


                        1 Reply Last reply
                        0
                        • J jeffb42

                          I wish to do something along the lines of: MyObject theObject = new MyObject(); DumpObject(theObject); ... elsewhere... public void DumpObject(Object obj) { string name = GetName(obj); // <<--- This is the part I'm missing System.Console.WriteLine("The name of the object is " + name); // this will print out: // "The name of the object is theObject" } I thought I could use reflection to get the name of the object but I'm not having any luck. I know this must be possible, but I'm stumped. TIA, Jeff

                          N Offline
                          N Offline
                          nelsonpaixao
                          wrote on last edited by
                          #12

                          Hi, do you want to acess a object from a form in otherform?

                          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