[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

A collection of different objects How to Use Interface, Inheritance

Asked by dbradshawhome in C# Programming Language

Tags: connect, different, how, method, using

I’m a bit new to the finer points of OOP (inheritance, etc) so please excuse (and feel free to correct ) any terminology errors.

I have a number of different sensors all of which have certain common properties such as ipaddress, port and description, and different methods to read or calibrate.

Obviously I can create individual classes for each sensor type but that would mean writing the same code to read and write properties, therefore there is a class SensorBase that each sensor (SensorA, SensorB & SensorC extend) .

The base class is as follows:

   public class SensorBase
    {
        private int portNo;
        private string iP;
        private string description;
        public int PortNo
        {
            get
            {
                return portNo;
            }
            set
            {
                portNo = value;

            }
        }
        public string IP
        {
            get
            {
                return iP;
            }
            set
            {
                iP = value;

            }
        }
        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;

            }
        }

A typical sensor class is as follows:

    public class SensorB : SensorBase
    {
        public int Read()
        {
            //real code would read sensor of type B
            return System.DateTime.Now.Minute;
        }
        public int Connect()
        {
            return 0;
        }
    }

So now I can instansiate an object MySensorB and have IPAddress, Port, Description and a Read method. – So far so good.

What I really want is a collection of different sensors with a method to read all the sensors. So I need to guarantee that all Sensors that might be added to the collection have certain properties and methods – I believe I need to implement an interface. So I add the following

    public interface ISensors
    {
        int Read();
        int Connect();
        string Description();
    }

and change my Sensor code to:

    public class SensorB : SensorBase,ISensors
    {
        public int Read()
        {
            return System.DateTime.Now.Minute;
        }
        public int Connect()
        {
            return 0;
        }
    }

Now if I do not implement either Read or Connect I get a compile error. – Again just what I want.

I create my collection:
    public class SensorCollection:CollectionBase
    {
        public virtual void Add(ISensors Item)
        {
            this.List.Add(Item);
        }

        public void ReadAll()
        {
            foreach (ISensors Sensor in this.List)
            {
                int Value=Sensor.Read();
                Console.WriteLine ("Read Value {0} From Sensor {1}", Value, Sensor.Description);
            }
        }
    }

This is where the problem starts – I can leave Description in my Interface whereupon I have to implement Description in each Sensor (the project is OK, but I have identical code in each sensor class – which seems kind of wrong).

If I comment out Description from the interface, I cannot access Sensor.Description in the ReadAll method as the object Sensor is of type Isensors (even though all the objects in the collection will be SensorA, SensorB and SensorC and will have a Description that is defined in SensorBase).

Is there a way that each different sensor can inherit from SensorBase and the inherited properties, methods and events can be used in my collection?
[+][-]09/18/05 03:04 AM, ID: 14906535Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: C# Programming Language
Tags: connect, different, how, method, using
Sign Up Now!
Solution Provided By: navalarya1982
Participating Experts: 2
Solution Grade: A
 
[+][-]09/17/05 11:00 AM, ID: 14904697Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/18/05 04:11 AM, ID: 14906636Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/18/05 04:43 AM, ID: 14906690Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92