Solved

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

Posted on 2008-10-20
3
573 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:yaronusa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Accepted Solution

by:
ororiole earned 250 total points
ID: 22762829
You want PhysicalGpibAddress of the concrete class to override the property of the abstract class right? And your property in the concrete is set to private by default. So it goes to the only Property it can
access, the public one in the abstract class.
Mark the concrete property with

public ovverrides GpibAddress PhysicalGpibAddress

and the abstract one with
public virtual  GpibAddress PhysicalGpibAddress

0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 total points
ID: 22763258
Honestly, I didn't see any abstract in your abstract class. I.e., no abstract methods, props etc. What's the point for it to be abstract? Anyway, even if the class is abstract it does not mean all members are abstract. The class may have implemented properties and they can be used.

OK, now about your implemented class let's have a look at the code:

           set
            {
                // I expected execution to go here, but it doesn't.
                System.Console.Out.WriteLine("UserDefinedInstrument PhysicalGpibAddress set...");
                base.PhysicalGpibAddress = value;
            }

base.PhysicalGpibAddress = value; - you call the base class!
In the first part you call get trying to access the property:
base.PhysicalGpibAddress
 and then you are trying to set the value:
 = value.

And you can't expect anything else - the only way to access "_physicalInstrumentGpibAddress" variable is to enter abstract class property.
Does this fact causing any problems?
0
 

Author Closing Comment

by:yaronusa
ID: 31508043
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.
0

Featured Post

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question