Mike Malter
Mike Malter

Reputation: 1038

How do I determine the instantiated name of a class when I create it

I have a class with a few members and properties and when I create it how can I know what the instantiated name is at the time of creation?

I have a class PDInt and so I'll instantiate it as a member and then will wrap that member in a property. Here is the class and followed by the property:

    public class PDInt : PDBase
{
    #region Members

    int m_Value;
    public int Value
    {
        get
        {
            return m_Value;
        }
        set
        {
            if ( m_Value != value )
            {

            }
        }
    }

    #endregion

    public PDInt()
    {
        this.Init();
    }

    public PDInt( int Value )
    {
        this.Init();
        this.Value = Value;
    }

    public PDInt( int Value, string ControlName )
    {
        this.Init();

        this.Value       = Value;
        this.ControlName = ControlName;
    }

    private void Init()
    {
        this.Value = 0;
    }

    public void Map( int Value, string ControlName )
    {
        this.Value       = Value;
        this.ControlName = ControlName;
        this.Validate    = true;
    }

    public void Map( int Value, string ControlName, bool Validate )
    {
        this.Value       = Value;
        this.ControlName = ControlName;
        this.Validate    = Validate;
    }
}

Here is member and then property usage

        PDInt m_PrescriptionID;
    public PDInt PrescriptionID
    {
        get
        {
            if ( m_PrescriptionID == null )
            {
                m_PrescriptionID = new PDInt();
            }

            return m_PrescriptionID;
        }
        set
        {
            if ( m_PrescriptionID == null )
            {
                m_PrescriptionID = new PDInt();
            }
        }
    }

It would be very helpful to me to be able to determine programatically what the actual instantiated name is so I can put it in a string inside of the class to be referred to later.

I am using reflection throughout my app and I just can't seem to figure out how to get at the name when the class is instantiated.

Thanks.

Upvotes: 3

Views: 134

Answers (1)

Daniel Mošmondor
Daniel Mošmondor

Reputation: 19956

If I understand correctly, you would like to know in a class what is the instance name that is used from somewhere else when the class is instantiated?

If that is so - you can't. c# don't allow that.

EDIT:

Why do you need that? Maybe you have a problem that can be solved some other way?

Upvotes: 2

Related Questions