Click here to Skip to main content
15,922,155 members
Home / Discussions / C#
   

C#

 
GeneralRe: Sending keys to the active windows - almost there Pin
Dirso23-Jun-08 13:38
Dirso23-Jun-08 13:38 
GeneralRe: Sending keys to the active windows - almost there Pin
mav.northwind25-Jun-08 9:25
mav.northwind25-Jun-08 9:25 
GeneralRe: Sending keys to the active windows - almost there Pin
Dirso25-Jun-08 10:39
Dirso25-Jun-08 10:39 
GeneralRe: Sending keys to the active windows - almost there Pin
mav.northwind25-Jun-08 19:17
mav.northwind25-Jun-08 19:17 
GeneralRe: Sending keys to the active windows - almost there Pin
Dirso26-Jun-08 0:50
Dirso26-Jun-08 0:50 
QuestionToolStripItem visible problem Pin
Yulianto.21-Jun-08 4:56
Yulianto.21-Jun-08 4:56 
AnswerRe: ToolStripItem visible problem Pin
subai21-Jun-08 20:26
subai21-Jun-08 20:26 
QuestionAbstract classes hierarchy Pin
Metal7621-Jun-08 4:55
Metal7621-Jun-08 4:55 
Hi, I am a longtime embedded C programmer who has recently begun struggling with OOP Confused | :confused:

I'm writing code for a serial communication protocol; protocol messages can belong to 2 different types (commands and responses), which share several fields and methods that I decided to factor out in a common base class, more or less like the example below:

public abstract class Message
{
    // Several properties representing common fields ...
    private byte _FieldA;
    public byte FieldA
    {
         // Property getter and setter
    }

    private byte _FieldB;
    public byte FieldB
    {
         // Property getter and setter
    }

    private byte _FieldC;
    public byte FieldC
    {
         // Property getter and setter
         // NOTE: FieldC value is based on FieldA and Field B values!!!
    }

    // Common methods...
    public byte[] CompileHeader()
    {
         // Compiles packet header using all the fields above
    }
}

public abstract class Command : Message
{
    // Additional specific command fields and methods
    // NOTE: commands must set FieldB to 1!
}

public abstract class Response : Message
{
    // Additional specific response fields and methods
    // NOTE: commands must set FieldB to 2!
}

public class MyCommand : Command
{
    // Concrete command class, adds more data etc.
    // By deriving from Command, I'd like FieldB to be 
    // already set to 1 from the base classes
}


Message, Command and Response are abstract because they are not meant to be instantiated: it only makes sense to instantiate the concrete subclass (MyCommand).

The problem is: as you see, if the actual message is a Command or a Response, FieldB must be set to a different value. However, FieldB is also used by the base Message class so I cannot move it down the hierarchy.

I'd like FieldB to be set automatically to the appropriate value in my concrete classes, depending on the base class (e.g. if I inherit from Command I'd expect FieldB = 1).

How can I implement this behaviour in C#?

Thanks in advance to all of you gurus out there!
Andrea
AnswerRe: Abstract classes hierarchy Pin
User 665821-Jun-08 5:23
User 665821-Jun-08 5:23 
GeneralRe: Abstract classes hierarchy Pin
Metal7621-Jun-08 5:32
Metal7621-Jun-08 5:32 
GeneralRe: Abstract classes hierarchy Pin
User 665821-Jun-08 5:39
User 665821-Jun-08 5:39 
GeneralRe: Abstract classes hierarchy Pin
Mark Churchill21-Jun-08 22:02
Mark Churchill21-Jun-08 22:02 
AnswerRe: Abstract classes hierarchy Pin
Guffa21-Jun-08 5:45
Guffa21-Jun-08 5:45 
AnswerRe: Abstract classes hierarchy Pin
User 665821-Jun-08 5:48
User 665821-Jun-08 5:48 
GeneralRe: Abstract classes hierarchy Pin
Metal7621-Jun-08 8:51
Metal7621-Jun-08 8:51 
QuestionCrystal Report Performance Pin
Member 202517621-Jun-08 3:40
Member 202517621-Jun-08 3:40 
AnswerRe: Crystal Report Performance Pin
Paul Conrad21-Jun-08 3:46
professionalPaul Conrad21-Jun-08 3:46 
QuestionProduction Rules Pin
mymo_h21-Jun-08 3:29
mymo_h21-Jun-08 3:29 
AnswerRe: Production Rules Pin
Paul Conrad21-Jun-08 3:49
professionalPaul Conrad21-Jun-08 3:49 
GeneralRe: Production Rules Pin
mymo_h21-Jun-08 4:21
mymo_h21-Jun-08 4:21 
GeneralRe: Production Rules Pin
Paul Conrad21-Jun-08 4:39
professionalPaul Conrad21-Jun-08 4:39 
GeneralRe: Production Rules Pin
mymo_h21-Jun-08 4:41
mymo_h21-Jun-08 4:41 
GeneralRe: Production Rules Pin
User 665821-Jun-08 5:08
User 665821-Jun-08 5:08 
GeneralRe: Production Rules Pin
mymo_h21-Jun-08 5:12
mymo_h21-Jun-08 5:12 
AnswerRe: Production Rules Pin
Ashfield21-Jun-08 7:34
Ashfield21-Jun-08 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.