How to display the name of an object instance?

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi,

I have a ToolbarButton and I want to print out the instance name. This
is easy to do with objects that inherit from Control because there is
a .Name property. However, with other objects like ToolbarButton I
don't see how to do this.

Advice? Ideas? THANKS!

ToolbarButton tbbMyButton;

public static void ShowName(object o)
{
if ( o is ToolbarButton )
{
Console.Writeline(tbbMyButton.Name); // Doesn't work, no Name
property
}
}

Maybe something with reflection?

Thanks!!!

John
 
John,

The Name property of a Control instance isn't really applicable. Say
you had a button, which was named mobjButton. What happens when you do
this?

// Assign the button locally.
Button pobjButton = mobjButton;

What should the name be then? mobjButton or pobjButton? Both are
legitimate names for the object instance.

If you want to get the name of the field for an object instance, then
you can call the GetFields method on the Type class. Since all instances
will have the same field names, you can just use these.

Hope this helps.
 
John said:
I have a ToolbarButton and I want to print out the instance name. This
is easy to do with objects that inherit from Control because there is
a .Name property. However, with other objects like ToolbarButton I
don't see how to do this.

Advice? Ideas? THANKS!

ToolbarButton tbbMyButton;

public static void ShowName(object o)
{
if ( o is ToolbarButton )
{
Console.Writeline(tbbMyButton.Name); // Doesn't work, no Name
property
}
}

Maybe something with reflection?

Objects don't *have* names usually. What do you actually want to see?

Don't forget that there's a big difference between an object and a
variable containing a reference to an object. A variable has a name,
but there could be lots of variables with references to a specific
object - or none!
 
That helps a lot. Thank you!

I was so focused on the reference to the instance that I forgot to
consider the FieldInfo that I get from the type. FieldInfo has exactly
what I need.

THANKS!!!
John

Nicholas Paldino said:
John,

The Name property of a Control instance isn't really applicable. Say
you had a button, which was named mobjButton. What happens when you do
this?

// Assign the button locally.
Button pobjButton = mobjButton;

What should the name be then? mobjButton or pobjButton? Both are
legitimate names for the object instance.

If you want to get the name of the field for an object instance, then
you can call the GetFields method on the Type class. Since all instances
will have the same field names, you can just use these.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John said:
Hi,

I have a ToolbarButton and I want to print out the instance name. This
is easy to do with objects that inherit from Control because there is
a .Name property. However, with other objects like ToolbarButton I
don't see how to do this.

Advice? Ideas? THANKS!

ToolbarButton tbbMyButton;

public static void ShowName(object o)
{
if ( o is ToolbarButton )
{
Console.Writeline(tbbMyButton.Name); // Doesn't work, no Name
property
}
}

Maybe something with reflection?

Thanks!!!

John
 
Back
Top