Click here to Skip to main content
15,899,474 members
Home / Discussions / C#
   

C#

 
GeneralRe: Classes inheritance and something that confuses me Pin
Richard MacCutchan2-Jun-11 22:28
mveRichard MacCutchan2-Jun-11 22:28 
GeneralRe: Classes inheritance and something that confuses me Pin
MicroVirus3-Jun-11 1:40
MicroVirus3-Jun-11 1:40 
GeneralRe: Classes inheritance and something that confuses me Pin
Richard MacCutchan3-Jun-11 3:31
mveRichard MacCutchan3-Jun-11 3:31 
GeneralRe: Classes inheritance and something that confuses me Pin
nstk3-Jun-11 11:48
nstk3-Jun-11 11:48 
GeneralRe: Classes inheritance and something that confuses me Pin
BobJanova3-Jun-11 4:29
BobJanova3-Jun-11 4:29 
GeneralRe: Classes inheritance and something that confuses me Pin
nstk3-Jun-11 20:35
nstk3-Jun-11 20:35 
AnswerRe: Classes inheritance and something that confuses me Pin
Łukasz Nowakowski2-Jun-11 22:05
Łukasz Nowakowski2-Jun-11 22:05 
AnswerRe: Classes inheritance and something that confuses me Pin
_Erik_3-Jun-11 3:35
_Erik_3-Jun-11 3:35 
Let's make a better example for this:

C#
class Employee
{
    public string Name { get; set; }
}

class ContractEmployee : Employee
{
    public object Contract { get; set; }
}

class Program
{
    static void Main()
    {
        Employee e = new ContractEmployee();
        e.Name = "Ricky"; // No problem with this
        e.Contract = new object(); // Problem: this does not compile
    }
}

nstk wrote:
which properties and methods does the object e has?

e object, at runtime, has the properties and methods defined within ContractEmployee class, because it is the object you have created with new, so it has Name (inherited from Employee class), and Contract (defined within ContractEmployee). However, without any casting, from e object you will only be able to access those members defined within Employee class (Name property in this case), because it is the type you have used to declare the object. Why? Because e object is declared as Employee, and Employee does not define a member named Contract. Yes, e is a ContractEmployee instance and, yes, it has a Contract property, but the compiler does not know it. All the compiler knows is that e object is declared as Employee, so it allows you to access only the members defined in Employee class. So, if you want to access the Contract property of e object, in this case, you would need a previous casting operation:

C#
((ContractEmployee)e).Contract = new object();

// or

ContractEmployee ce = (ContractEmployee)e;
ce.Contract = new object();


nstk wrote:
how should I read this?

Any ContractEmployee object is a Employee, always, so you can declare an Employee and instantiate a ContractEmployee, but not in the other direction, I mean, not all of the Employee objects have to be ContractEmployee.
AnswerRe: Classes inheritance and something that confuses me Pin
Ravi Bhavnani3-Jun-11 5:48
professionalRavi Bhavnani3-Jun-11 5:48 
AnswerRe: Classes inheritance and something that confuses me Pin
Abhinav S3-Jun-11 21:23
Abhinav S3-Jun-11 21:23 
QuestionBasic Question On Constructor Stuff Pin
PozzaVecia2-Jun-11 10:10
PozzaVecia2-Jun-11 10:10 
AnswerRe: Basic Question On Constructor Stuff Pin
Ian Shlasko2-Jun-11 10:15
Ian Shlasko2-Jun-11 10:15 
GeneralRe: Basic Question On Constructor Stuff Pin
PozzaVecia2-Jun-11 10:21
PozzaVecia2-Jun-11 10:21 
AnswerRe: Basic Question On Constructor Stuff Pin
PIEBALDconsult2-Jun-11 16:23
mvePIEBALDconsult2-Jun-11 16:23 
GeneralRe: Basic Question On Constructor Stuff Pin
PozzaVecia3-Jun-11 11:10
PozzaVecia3-Jun-11 11:10 
AnswerRe: Basic Question On Constructor Stuff Pin
BobJanova3-Jun-11 4:34
BobJanova3-Jun-11 4:34 
QuestionGrid rendering problem in windows forms Pin
venomation2-Jun-11 5:38
venomation2-Jun-11 5:38 
GeneralRe: Grid rendering problem in windows forms Pin
thatraja2-Jun-11 8:21
professionalthatraja2-Jun-11 8:21 
AnswerRe: Grid rendering problem in windows forms Pin
Luc Pattyn2-Jun-11 8:47
sitebuilderLuc Pattyn2-Jun-11 8:47 
GeneralRe: Grid rendering problem in windows forms Pin
venomation2-Jun-11 9:28
venomation2-Jun-11 9:28 
AnswerRe: Grid rendering problem in windows forms Pin
Luc Pattyn2-Jun-11 9:36
sitebuilderLuc Pattyn2-Jun-11 9:36 
GeneralRe: Grid rendering problem in windows forms Pin
venomation2-Jun-11 9:57
venomation2-Jun-11 9:57 
AnswerRe: Grid rendering problem in windows forms Pin
Luc Pattyn2-Jun-11 10:02
sitebuilderLuc Pattyn2-Jun-11 10:02 
AnswerRe: Grid rendering problem in windows forms Pin
Luc Pattyn2-Jun-11 10:54
sitebuilderLuc Pattyn2-Jun-11 10:54 
QuestionNot Working RegistryKey.openSubKey in windows 7 Pin
vishnukamath2-Jun-11 3:32
vishnukamath2-Jun-11 3:32 

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.