0

We have some document-types:

class Document
{
    public void virtual Print()
}

class PDF : Document
{
    public void override Print()
    {
        Console.WriteLine("PDF Printed");
    }
}

class Excel : Document
{
    public void override Print()
    {
        Console.WriteLine("Excel Printed");
    }
}

Suppose we have a list of documents (Document objects) and we call the virtual function Print() on all of them.

foreach(Document doc in DocumentsList)
{
    doc.Print();
}

I know Polymorphism is much sophisticated way of implementing it but can we really do the same using switch statement as well? I had a long argument with a fellow on it and he says it's possible. Is it?

Failed Scientist
  • 1,977
  • 3
  • 29
  • 48
  • 7
    Why wouldn't it be possible? It'd just be more annoying. – SLaks Apr 21 '15 at 14:50
  • 3
    Everything is possible without polymorphism. But it will look awkward. – Sriram Sakthivel Apr 21 '15 at 14:52
  • 2
    Looks like this post might have what you're looking for: http://stackoverflow.com/questions/298976/is-there-a-better-alternative-than-this-to-switch-on-type But I agree with the others, polymorphism is almost certainly the preferred way to do it. – Dan McElroy Apr 21 '15 at 14:54
  • 5
    The usual answer to "possible" is "yes" with Turing-complete languages. – Peter - Reinstate Monica Apr 21 '15 at 14:55
  • If anyone could read the question properly, I have already mentioned that Polymorphism is a SOPHISTICATED way. I find it ridiculous that question is being downvoted and answers which are just yes or no are voted up – Failed Scientist Apr 21 '15 at 15:01
  • Dont get offended. The problem is just that it is kind of hard to understand why you are in doubt whether this can be done with a switch statement. Why not? Maybe you have something in mind why it should not be possible, but thats not apparent from your question. – 463035818_is_not_an_ai Apr 21 '15 at 15:06

4 Answers4

5

Yes you can, of course you can interrogate the member of the list on each iteration, and there are plenty of techniques that can achieve this (e.g attributes, internal Type field etc).

In fact everything you do in C# you can also do in Assembler! However I can hardly see any benefit of avoiding the advantages of OOP.

5

Yes, you can always use a switch (or an if-else chain), asking for the type. You can keep document type information in your object, for example as an enum, and use it in a switch.

Why you shouldn't do that? Because you have to extend all these switches when you add a new derived type.

To avoid that, you can implement something more automatic: Have a list of pointers to functions (or delegates in C#) which you fill for each single object with the functions suitable for this particular object (for example, the print delegate would point to print_pdf() for PDF files and to print_excel() for excel files). And hey, there you have hand-crafted a poor man's polymorphism.

The sweet thing about polymorphism is that the code using a polymorphic object can be type agnostic. It does not know what precise type the object actually has, and does not want to know. The precise type may not have existed back when the code handling the object was written. All the detail information is hidden within the type which is awesome for maintenance.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
4

Its a bit of a funny question, because the example using the switch is usually used to demonstrate how you usually dont want to do it.

Instead of using inheritance, you could have your document class like this:

class Doc {
    public: 
        enum DocType{PDF,EXCEL}
    private:
        DocType docType;
}

And then you simply use

switch (doc.getDocType()){
    ...etc...
}

However, there are several reasons why usually it is nicer to use inheritance. Imagine for example, you want to add another document type. Then not only you have to add another enum field, but (and here comes the big problem) you also would have to change every single switch statement in all code that is using your Document class. That is annoying and usually you do not want to do this.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

Polymorphism is the way to go for this. If you choose switch statement, you would need to use Reflection or is keyword to figure out which object's Print() you want to invoke.

JunaidKirkire
  • 878
  • 1
  • 7
  • 17