Link to home
Start Free TrialLog in
Avatar of yaronusa
yaronusa

asked on

Public property GET issue in c# csharp c sharp .net

Concrete class C_UserDefinedInstrument inherits from abstract class A_Instrument.

OK, so I try to SET a property on an instance of the concrete class, and while debugging one line of execution at a time, I noticed that execution goes into the GET and then SET of the property of the abstract class!

I expected execution to go only into the concrete class to SET the property, NOT into the GET and then SET of the property of the abstract class instead. Why is execution going into the abstract class, and why is it entering the GET part of the property at all when I called a SET?!

Is that really normal? Because I don't think it is.

Below is the following code:
1) Main Program
2) Abstract class A_Instrument
3) Concrete class C_UserDefinedInstrument

//
// 1) Main Program
//
class Program
    {
        static void Main(string[] args)
        {
            C_UserDefinedInstrument myInstrument;
 
            myInstrument = new C_UserDefinedInstrument(new GpibAddress(1, 2, "3"));
 
            //  SET-causing statment from the main program.
            myInstrument.PhysicalGpibAddress.AddressNumber = 5;
        }
    }
 
 
//
// 2) Abstract class
//
namespace Gpib
{
    abstract public class A_Instrument
    {
        private GpibAddress _physicalInstrumentGpibAddress;   
 
        protected A_Instrument(GpibAddress address)
        {
            System.Console.Out.WriteLine("A_Instrument constructor begin...");
            this._physicalInstrumentGpibAddress = address;
            System.Console.Out.WriteLine("A_Instrument constructor end.");
        }
 
        public GpibAddress PhysicalGpibAddress
        {
            get 
            {
                // Why does execution go here when I called SET?
                System.Console.Out.WriteLine("A_Instrument PhysicalGpibAddress get.");
                return this._physicalInstrumentGpibAddress; 
            }
            set 
            {
                System.Console.Out.WriteLine("A_Instrument PhysicalGpibAddress set.");
                this._physicalInstrumentGpibAddress = value; 
            }
        }
    }
}
 
 
//
// 3) Concrete Class
//
namespace Gpib
{
    class C_UserDefinedInstrument: A_Instrument, I_Instrument
    {
        private DateTime _createdOnThisDateAndTime;
        protected DateTime _modifiedOnThisDateAndTime;
        protected string _alias;
        private InstrumentState.states _state;
        private string _instrumentUniquenessKey;
 
        public C_UserDefinedInstrument(GpibAddress address) : base(address)
        {
            System.Console.Out.WriteLine("UserDefinedInstrument constructor begin...");
            this.PhysicalGpibAddress = address;            
            System.Console.Out.WriteLine("UserDefinedInstrument constructor end.");
        }
 
        GpibAddress PhysicalGpibAddress
        {
            get 
            {
                System.Console.Out.WriteLine("UserDefinedInstrument PhysicalGpibAddress get...");
                return base.PhysicalGpibAddress;
            }
            set 
            {
                // I expected execution to go here, but it doesn't.
                System.Console.Out.WriteLine("UserDefinedInstrument PhysicalGpibAddress set...");
                base.PhysicalGpibAddress = value;
            }
        }
 
        public string ReturnConnectionString
        {
            get { throw new Exception("Not implemented."); }
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ororiole
ororiole
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yaronusa
yaronusa

ASKER

Thanks! Yeah, I guess I missed the point of the abstract class. You folks gave me the information I need to now experiment with the code... thank you.