2

In .NET (originally .NET CF), how to get full address and name of an object?

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Button myButton = new Button();
            MessageBox.Show(GetFullName(myButton));
        }

        string GetFullName(object obj)
        {
            string path = null;
            // should retrieve the 'WindowsFormsApplication1.Form2.myButton' , but how?
            return path;
        }

    }
}
5
  • 1
    Note that your desired "full name" for myButton isn't really accurate under the usual interpretation of namespace.type.member. myButton is just a local variable, not a member of Form2. Can you tell us why you want this information?
    – dlev
    Commented Aug 7, 2011 at 5:57
  • and what if the 'myButton' be a global variable? Commented Aug 7, 2011 at 6:00
  • I don't think you can get the name in the way you would like, maybe if you can describe what it is you are trying to do, then we may be able to propose a solution. Commented Aug 7, 2011 at 6:01
  • There's not really such a thing in C# as a "global". You can have a public static field, but that's quite different from a local variable. Fields of a type do have names at run-time, and can be retrieved using reflection.
    – dlev
    Commented Aug 7, 2011 at 6:02
  • my object full path is a key in a text file and i just wanna send my object to a method that retrieves info from the text file. (for example : 'myButton.Text = GetResourceString(myButton);' Commented Aug 7, 2011 at 6:08

2 Answers 2

2

Like others have said, myButton is a variable in your example so it doesn't have a name like a member of a class normally would. However, you can try something like this (notice I set the Name property on the button and changed the parameter to a Control)...

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Button myButton = new Button();
            myButton.Name = "myButton";
            MessageBox.Show(GetFullName(myButton));
        }

        string GetFullName(Control ctl)
        {
            return string.Format("{0}.{1}", this.GetType().FullName, ctl.Name);
        }

    }
}
2
  • tnx Brian. i'll really think of that solution. Commented Aug 7, 2011 at 6:11
  • You can also use ctl.Parent.GetType() instead of this.GetType(). Then it isn't dependent on the GetFullName method being in the form class. Commented Aug 7, 2011 at 6:27
2
// should retrieve the 'WindowsFormsApplication1.Form2.myButton' , but how?

That's not possible.

An object in C# resides somewhere on the managed heap (it can even be moved around) and is identified by a reference to it. There can be many references to the same object, but there is no "back pointer" from the object to everywhere it is referenced.

class Program
{
  int number;
  public Program next;

  private static Program p1 { number = 1 };
  private static Program p2 { number = 2, next = p1 }

  private static int Main(int argc, string[] argv)
  {
    p2.DoStuff(p2);
  }

  void DoStuff(Program p)
  {
    // In here, the program with m_Number = 1 can be reached
    // as p1, p.next, p2.next and this.next. All of them are
    // references to the same object.
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.