Click here to Skip to main content
15,898,538 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to open-with a file using my application ? Pin
Luc Pattyn10-Dec-07 11:59
sitebuilderLuc Pattyn10-Dec-07 11:59 
GeneralRe: How to open-with a file using my application ? Pin
Ian Uy10-Dec-07 20:05
Ian Uy10-Dec-07 20:05 
GeneralCreating a Sound(say, a Beep) Pin
polishprogrammer10-Dec-07 11:14
polishprogrammer10-Dec-07 11:14 
GeneralRe: Creating a Sound(say, a Beep) Pin
Ravi Bhavnani10-Dec-07 11:18
professionalRavi Bhavnani10-Dec-07 11:18 
GeneralRe: Creating a Sound(say, a Beep) Pin
PIEBALDconsult10-Dec-07 12:20
mvePIEBALDconsult10-Dec-07 12:20 
GeneralRe: Creating a Sound(say, a Beep) Pin
JoeRip10-Dec-07 14:22
JoeRip10-Dec-07 14:22 
GeneralRe: Creating a Sound(say, a Beep) Pin
polishprogrammer11-Dec-07 9:18
polishprogrammer11-Dec-07 9:18 
QuestionCasting from a Type object Pin
Leonardo Pelisoli10-Dec-07 10:55
Leonardo Pelisoli10-Dec-07 10:55 
Hi,

I'd like to know if casting based on a Type object is possible. Consider the following example:
using System;

namespace ConsoleApplication1
{
    class BaseClass
    {
    }

    class ClassA : BaseClass
    {
    }

    class ClassB : BaseClass
    {
    }

    class Program
    {
        static void Main(string[] args)
        {
            BaseClass obj1 = new ClassA();
            BaseClass obj2 = new ClassB();

            Program prog = new Program();
            prog.TreatAnything(obj1);
            prog.TreatAnything(obj2);
        }

        public void TreatAnything(BaseClass obj)
        {
            if (obj is ClassA)
                TreatClass((ClassA)obj);
            else if (obj is ClassB)
                TreatClass((ClassB)obj);
            else
            {
            }
        }

        private void TreatClass(ClassA obj)
        {
            // do stuff
        }

        private void TreatClass(ClassB obj)
        {
            // do stuff
        }
    }
}

The TreatAnything function receives a BaseClass object, and is forced to check its type by the is operator before casting it and calling the appropriate function. Now, I know the example looks simplistic, but imagine there are tens of classes derived rom BaseClass. Sure, I could make tens of if clauses and call the correct function each time... What I would really love to do, though, is turn the TreatAnything function into something like this:
public void TreatAnything(BaseClass obj)
{
    Type t = obj.GetType();
    TreatClass(SomeHypotheticalCastingFunction(t, obj));
}

So the tens of if clauses would be reduced to a small number of statements. Is there any way to do this kind of thing?

In case you want to know what I'm trying to do: I made a socket that parses what is receives and builds an object which is then passed around various methods. I have a Message base class, and then several derived classes. Each derived class represents a specific type of message, whose reception puts in motion very different actions on the server side. No, I don't think it's feasible to make a virtual method Treat() on the Message class and override it on each derived class, as the actions involved require much interaction with other objects the messages know nothing about.

I thank you in advance for any suggestions you might have.

Leonardo Pelisoli
GeneralRe: Casting from a Type object Pin
m@u10-Dec-07 11:54
m@u10-Dec-07 11:54 
GeneralRe: Casting from a Type object [modified] Pin
PIEBALDconsult10-Dec-07 12:25
mvePIEBALDconsult10-Dec-07 12:25 
GeneralRe: Casting from a Type object Pin
PIEBALDconsult10-Dec-07 14:20
mvePIEBALDconsult10-Dec-07 14:20 
GeneralRe: Casting from a Type object Pin
Leonardo Pelisoli11-Dec-07 12:14
Leonardo Pelisoli11-Dec-07 12:14 
GeneralC# Properties - Stack overflow. Pin
gunner_uk200010-Dec-07 10:34
gunner_uk200010-Dec-07 10:34 
GeneralRe: C# Properties - Stack overflow. Pin
Luc Pattyn10-Dec-07 10:43
sitebuilderLuc Pattyn10-Dec-07 10:43 
GeneralRe: C# Properties - Stack overflow. Pin
martin_hughes10-Dec-07 10:45
martin_hughes10-Dec-07 10:45 
GeneralRe: C# Properties - Stack overflow. Pin
hofmadresu10-Dec-07 17:17
hofmadresu10-Dec-07 17:17 
GeneralMouse_Click on User Control class Pin
Fragment10-Dec-07 9:42
Fragment10-Dec-07 9:42 
Generalreal time timer Pin
iman_kh10-Dec-07 8:53
iman_kh10-Dec-07 8:53 
GeneralRe: real time timer Pin
nallanch_srinivas10-Dec-07 9:00
nallanch_srinivas10-Dec-07 9:00 
GeneralRe: real time timer Pin
Luc Pattyn10-Dec-07 9:18
sitebuilderLuc Pattyn10-Dec-07 9:18 
GeneralRe: real time timer Pin
Pete O'Hanlon10-Dec-07 9:39
mvePete O'Hanlon10-Dec-07 9:39 
GeneralRe: real time timer Pin
Luc Pattyn10-Dec-07 9:48
sitebuilderLuc Pattyn10-Dec-07 9:48 
GeneralRe: real time timer Pin
Pete O'Hanlon10-Dec-07 9:55
mvePete O'Hanlon10-Dec-07 9:55 
GeneralRe: real time timer Pin
Luc Pattyn10-Dec-07 10:12
sitebuilderLuc Pattyn10-Dec-07 10:12 
GeneralRe: real time timer Pin
Pete O'Hanlon10-Dec-07 10:18
mvePete O'Hanlon10-Dec-07 10:18 

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.