Cast ?

  • Thread starter Thread starter Mathieu Chavoutier
  • Start date Start date
M

Mathieu Chavoutier

I have a virtual mother class, and some children. My objective is to do
something like

Mother m = getCurrentChild()

Switch m =
Child1 : do that
Child2 : do that
Etc

What is the best way to do so ?

Thanks in advance.
 
Hi,

The thing you want is known as polymorphic behaviour. You should implement
an abstract method in the Mother class (or at a virtual one if the Mother
class is instantiable and there is an appropriate basic implementation),
called, say, DoThat(). Now, each class inheriting from Mother should
override this method and provide its own implementation. Now, if you have a
reference of type Mother, invoking DoThat on the reference would call the
correct implementation depending on the actual type of the class instance
the reference points to.
 
Hi,

In addition to Dmitriy comments, if you want to know the type of a variable
you can use GetType()

cheers,




--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation




Dmitriy Lapshin said:
Hi,

The thing you want is known as polymorphic behaviour. You should implement
an abstract method in the Mother class (or at a virtual one if the Mother
class is instantiable and there is an appropriate basic implementation),
called, say, DoThat(). Now, each class inheriting from Mother should
override this method and provide its own implementation. Now, if you have a
reference of type Mother, invoking DoThat on the reference would call the
correct implementation depending on the actual type of the class instance
the reference points to.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Mathieu Chavoutier said:
I have a virtual mother class, and some children. My objective is to do
something like

Mother m = getCurrentChild()

Switch m =
Child1 : do that
Child2 : do that
Etc

What is the best way to do so ?

Thanks in advance.
 
Ignacio,

Fully agree, but, as a comment to the original poster, I wouldn't call
deciding what to do based on the type a good programming style :-)

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

In addition to Dmitriy comments, if you want to know the type of a variable
you can use GetType()

cheers,




--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation




Dmitriy Lapshin said:
Hi,

The thing you want is known as polymorphic behaviour. You should implement
an abstract method in the Mother class (or at a virtual one if the Mother
class is instantiable and there is an appropriate basic implementation),
called, say, DoThat(). Now, each class inheriting from Mother should
override this method and provide its own implementation. Now, if you
have
a
reference of type Mother, invoking DoThat on the reference would call the
correct implementation depending on the actual type of the class instance
the reference points to.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Mathieu Chavoutier said:
I have a virtual mother class, and some children. My objective is to do
something like

Mother m = getCurrentChild()

Switch m =
Child1 : do that
Child2 : do that
Etc

What is the best way to do so ?

Thanks in advance.
 
Dmitriy Lapshin said:
Ignacio,

Fully agree, but, as a comment to the original poster, I wouldn't call
deciding what to do based on the type a good programming style :-)

In fact, I have a list of children (they are children of Mother), and I want
to do a things like : For all clindren of that type, sum that specific
thing.
I have used a virtual method because it is best for the rest. But, now, it
will be far far far easier to know who is who.

Thanks anyway.
 
Mathieu Chavoutier said:
"Dmitriy Lapshin [C# / .NET MVP]" <[email protected]> a ecrit
dans le message de news:%[email protected]...
Ignacio,

Fully agree, but, as a comment to the original poster, I wouldn't call
deciding what to do based on the type a good programming style :-)

In fact, I have a list of children (they are children of Mother), and I want
to do a things like : For all clindren of that type, sum that specific
thing.
I have used a virtual method because it is best for the rest. But, now, it
will be far far far easier to know who is who.

You do not provide any details to explain why.

If there would be a lot of repetitive code, then why not use a Strategy
pattern.

In the base class, define a concrete method that performs the common code,
with calls out to virtual methods, defined in the base class and overridden
in the children, to perform "child-specific" functionality.

For example, If I was writing my own type-safe collection class, and I want
to be able to do a quick-sort, I would write the .Sort method on the base
class, and simply have the decendents define a method that allows two
different items to be compared (which is precisely what the .NET framework
collection classes do).

There are other techniques that could be useful as well. Unfortunately,
Mathieu, you've decided not to provide us enough information to help you
find one.

--- Nick
 
Hi,

If the poster would have posted more details of what he want to do we
could suggest a better solution, as he did not I think is better if he knows
the different ways to do what he originaly asked.

this approach may be useful in some escenario, even it's not good
programming style.

cheers,


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Dmitriy Lapshin said:
Ignacio,

Fully agree, but, as a comment to the original poster, I wouldn't call
deciding what to do based on the type a good programming style :-)

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:[email protected]...
Hi,

In addition to Dmitriy comments, if you want to know the type of a variable
you can use GetType()

cheers,




--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation




in message news:[email protected]...
Hi,

The thing you want is known as polymorphic behaviour. You should implement
an abstract method in the Mother class (or at a virtual one if the Mother
class is instantiable and there is an appropriate basic implementation),
called, say, DoThat(). Now, each class inheriting from Mother should
override this method and provide its own implementation. Now, if you
have
a
reference of type Mother, invoking DoThat on the reference would call the
correct implementation depending on the actual type of the class instance
the reference points to.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

I have a virtual mother class, and some children. My objective is to do
something like

Mother m = getCurrentChild()

Switch m =
Child1 : do that
Child2 : do that
Etc

What is the best way to do so ?

Thanks in advance.
 
Nick Malik said:
You do not provide any details to explain why.

Okay, I give you details.

If I have a virtual class, it's because I'm doing a drawing tool. So, it is
(at leat I think) the best way to do it : when you have a thing to draw, you
call the draw() method, and it works for every things you have. It was
possible to do in other way, perhaps, but it's too late (I have too many
code to go backward).

But, after the user have done a paint, there is a phase of "compilation". To
be more precise, what was drawn was computer (only an icon), network, walls
and lot of things like that. So, after, I look all elements placed, and I
have to do specifics things with computer only. So, I think that I can do
what I want with .getType(), even if it's not the best way to do so.

But if you have a better solution, give it :o)
I don't have begin to program anything, so I am still open.
If you need any other info, ask; perhaps I was not clear ?
 
Hello Mathieu,

You describe a classic case for inheritance: have a base class declare a
virtual method that each child must define and which the CLIENT class will
use.

However, I was describing something different.
I describe the strategy pattern. The distinction is that the base class
declares a virtual method that each child must define, but which the BASE
CLASS will use.

This allows you to put "common code" for a complex or highly specific
algorithm into the base class. The only things that are in the child
classes are protected override methods, methods that you do not need to
expose to the client class at all. These methods allow you to perform the
specific elements of the complex operation without the client even knowing
that a difference exists.

I'm not sure what specific things you need to do with that one object or
each individual object. Some things are hard to isolate, and I understand
if you need to create methods that are specific to the child class.
However, over half of the time, if you think about it, you can create a
generic interface that will allow you to pass in an object that provides
data, and your child class will react in a manner, or manipulate that object
in a way, that is specific to the child class... and you don't need to
create or call methods that are specific to the object. It isn't always
true, but it is true more often than it isn't.

Anyway, good luck. I hope this conversation was useful to you.

--- Nick
 
Back
Top