Click here to Skip to main content
15,921,542 members
Home / Discussions / C#
   

C#

 
AnswerRe: Module based software Pin
Not Active4-Feb-10 21:20
mentorNot Active4-Feb-10 21:20 
Questionmultithreaded application Pin
Member 5903104-Feb-10 19:31
Member 5903104-Feb-10 19:31 
AnswerRe: multithreaded application Pin
Not Active4-Feb-10 20:12
mentorNot Active4-Feb-10 20:12 
AnswerRe: multithreaded application Pin
Calla4-Feb-10 20:52
Calla4-Feb-10 20:52 
Questionhow can i select null instances using datacontext.tablename.select ? Pin
tonyonlinux4-Feb-10 19:13
tonyonlinux4-Feb-10 19:13 
AnswerRe: how can i select null instances using datacontext.tablename.select ? Pin
Not Active4-Feb-10 20:10
mentorNot Active4-Feb-10 20:10 
GeneralRe: how can i select null instances using datacontext.tablename.select ? Pin
tonyonlinux4-Feb-10 20:21
tonyonlinux4-Feb-10 20:21 
GeneralRe: how can i select null instances using datacontext.tablename.select ? Pin
Not Active4-Feb-10 20:48
mentorNot Active4-Feb-10 20:48 
QuestionInheritance/Polymorphism [modified] Pin
Sundeepan Sen4-Feb-10 18:26
Sundeepan Sen4-Feb-10 18:26 
AnswerRe: Inheritance/Polymorphism Pin
Not Active4-Feb-10 20:07
mentorNot Active4-Feb-10 20:07 
GeneralRe: Inheritance/Polymorphism Pin
Sundeepan Sen4-Feb-10 20:28
Sundeepan Sen4-Feb-10 20:28 
AnswerRe: Inheritance/Polymorphism Pin
Wamuti4-Feb-10 21:58
Wamuti4-Feb-10 21:58 
AnswerRe: Inheritance/Polymorphism Pin
PIEBALDconsult5-Feb-10 3:19
mvePIEBALDconsult5-Feb-10 3:19 
GeneralRe: Inheritance/Polymorphism Pin
Sundeepan Sen5-Feb-10 7:04
Sundeepan Sen5-Feb-10 7:04 
GeneralRe: Inheritance/Polymorphism Pin
PIEBALDconsult5-Feb-10 7:50
mvePIEBALDconsult5-Feb-10 7:50 
GeneralRe: Inheritance/Polymorphism Pin
Sundeepan Sen5-Feb-10 9:24
Sundeepan Sen5-Feb-10 9:24 
AnswerRe: Inheritance/Polymorphism Pin
Keith Barrow5-Feb-10 5:23
professionalKeith Barrow5-Feb-10 5:23 
I actually almost say the contrary is true.

Mark Nischalke's answer holds true, you do have access to both the Moo() and Foo() methods by passing the subclass. However to do so couples the method to the subclass, and other instances of the animal class cannot be passed:
class Program
{
    static void MakeAnimalSpeak(Animal animal)
    {
        
        animal.MakeNoise();
    }

    static void MakeAnimalSpeakCoupled(Cow animal)
    {
        // This is more tighltly coupled to cow: 
        // this method cannot be called for duck
        animal.MakeNoise();
    }


    static void Milk(Cow animal)
    {
        animal.Milk();
        animal.MakeNoise(); // Cow is mooing with happiness
    }



    static void Main(string[] args)
    {
        Cow cow = new Cow();
        Duck duck = new Duck();
        MakeAnimalSpeak(cow);
        MakeAnimalSpeak(duck);
        MakeAnimalSpeakCoupled(cow);
        //MakeAnimalSpeakCoupled(duck); //Coupled to cow: this is a worse implementation in this context
        Milk(cow);
        //Milk(duck); //Can't, but not a problem. Milking a duck is nonsensical!
    }
}



abstract class Animal
{   
    public abstract void MakeNoise();  
}

class Cow : Animal
{
    public override void MakeNoise() { Console.WriteLine("Moo"); }
    public void Milk() { Console.Write("Sloooosh"); }
}

class Duck : Animal
{
    public override void MakeNoise() { Console.WriteLine("Quack"); }    
}


Of course it is horses for courses, as with everythign in the code world. In the example Mark gave, you should pass the cow if you need access to both Moo() and Foo() but otherwise IMO is is better to pass an Animal instead. Better yet, coding to Intefraces produces cleaner results still, and helps with testing (as mock objects can be passed more easily)

Cpianism: I have a negative number in my Rep so please fix it.
Chris Maunder: That isn't a bug.

GeneralRe: Inheritance/Polymorphism Pin
Sundeepan Sen5-Feb-10 10:05
Sundeepan Sen5-Feb-10 10:05 
QuestionWindowsAPICodePack GlassForm Question Pin
harsimranb4-Feb-10 17:06
harsimranb4-Feb-10 17:06 
AnswerRe: WindowsAPICodePack GlassForm Question Pin
#realJSOP5-Feb-10 0:17
professional#realJSOP5-Feb-10 0:17 
GeneralRe: WindowsAPICodePack GlassForm Question Pin
harsimranb5-Feb-10 9:14
harsimranb5-Feb-10 9:14 
GeneralRe: WindowsAPICodePack GlassForm Question Pin
#realJSOP5-Feb-10 9:37
professional#realJSOP5-Feb-10 9:37 
GeneralRe: WindowsAPICodePack GlassForm Question Pin
harsimranb5-Feb-10 14:45
harsimranb5-Feb-10 14:45 
GeneralRe: WindowsAPICodePack GlassForm Question Pin
#realJSOP5-Feb-10 22:28
professional#realJSOP5-Feb-10 22:28 
QuestionHow would i get the current Index ID of the currently displayed record Pin
tonyonlinux4-Feb-10 15:51
tonyonlinux4-Feb-10 15:51 

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.