1

C# .net 4.5
Visual Studio 2013 Update 3
Windows NT 6.3 build 9200 (Windows 8.1 Home Premium Edition) i586

Looking for a method/process to extract the current local variable name of an object. Not the class name.

The following code example displays the class i.e. Beast, SonOfBeast, GrandSonOfBeast with this.GetType().Name ...but I'm looking for a way to extract the current object name i.e. Monkey, MonkeyJr, MonkeyIII without passing along an ID-tag.

namespace WhoAreYou { class Program { static void Main(string[] args) // Example using Inheritance { Console.WriteLine("~~~~Who is that masked beast~~~~");

        Beast Monkey = new Beast();
        Monkey.Prowl();

        SonOfBeast MonkeyJr = new SonOfBeast();
        MonkeyJr.Prowl();

        GrandSonOfBeast MonkeyIII = new GrandSonOfBeast();
        MonkeyIII.Prowl();

        Console.ReadKey();
    }
}

class SonOfBeast:Beast{ }
class GrandSonOfBeast:SonOfBeast{ }

class Beast
{
    public void Prowl()
    {
        int frequency, duration;
        Console.Write("Your {0} is loose again! ", this.GetType().Name);
        for (int i = 0; i < 3; i++)
        {
            Console.Write(".~.>  ");
            System.Threading.Thread.Sleep(125);    // 0.125 second
            for (int j = 37; j <= 637; j += 300)
            {
                if (j < 637) {frequency = j; duration = 660 - j;}
                else 
                   {frequency = j - 480; duration = j / 2;}
                Console.Beep(frequency, duration);
            }
        }
        Console.WriteLine(); 
    }
}

}

3
  • 3
    The variable name is there for you to understand the code; the compiler won't keep it (specially since it is a local variable) Commented Oct 12, 2014 at 19:21
  • 2
    Identifiers doesn't available at runtime. Maybe you can achieve this using Roslyn. Commented Oct 12, 2014 at 19:22
  • 2
    I'll go ahead and throw it out there that the upcoming C# 6 is going to have a nameof operator that could potentially be used for this purpose... but even so, you wouldn't be able to use it in the place where you want to use it (the variable name would need to be in scope, so Program.Main in your example, not Beast.Prowl). Any in any case, this language feature is not available yet anyway, and there are better ways to do what you want (as suggested in the answers). Commented Oct 12, 2014 at 19:39

2 Answers 2

7

An object doesn't have a name. A variable has a name, but multiple variables could have values which refer to the same object. For example:

Beast monkey = new Beast();
Beast donkey = monkey;

Now they both refer to the same object - so if there were a way of getting at a name for an object, what would it return? And sometimes there could be no names at all...

If you want an object to have the concept of a name, you should provide it yourself. For example:

public sealed class Beast
{
    private readonly string name;

    public string Name { get { return name; } }

    public Beast(string name)
    {
        this.name = name;
    }
}

Then you could use:

Beast x = new Beast("Monkey");
Console.WriteLine(x.Name); // Prints "Monkey"
Sign up to request clarification or add additional context in comments.

Comments

3

There are many ways you can skin your cat.. It totally depends on your requirements, as to what particular design may be appropriate. Two options which come to mind are

  1. You need to have different behavior for your Moneky, Pig etc.. Which means, you'd create new classes with appropriate Names, which will extend Beast class. In this case, following would work

    class Monkey : Beast
    {
        public void MonkeyBehavior()
        {
            ....
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Monkey monkey = new Monkey();
            Pig pig = new Pig();
            Rino rino = new Rino();
            monkey.Prowl(); pig.Prowl(); rino.Prowl();
            Console.ReadKey();
        }
    }
    
  2. The other option is, that all Beast are just Beast, with no difference in behavior, but only different property value, lets say Specie. In this case, just add this property to Beast class, and you can set it while constructing instances of Beast class, and read them anywhere you reference those instances.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.