473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Cast ?

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.
Nov 16 '05 #1
8 1126
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" <no****@no.spam> wrote in message
news:Oq**************@TK2MSFTNGP12.phx.gbl...
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.


Nov 16 '05 #2
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 [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:O7**************@TK2MSFTNGP10.phx.gbl...
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" <no****@no.spam> wrote in message
news:Oq**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 16 '05 #3
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:uI**************@TK2MSFTNGP09.phx.gbl...
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 [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:O7**************@TK2MSFTNGP10.phx.gbl...
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" <no****@no.spam> wrote in message
news:Oq**************@TK2MSFTNGP12.phx.gbl...
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.



Nov 16 '05 #4

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> a ecrit
dans le message de news:%2****************@TK2MSFTNGP10.phx.gbl...
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.
Nov 16 '05 #5

"Mathieu Chavoutier" <no****@no.spam> wrote in message
news:Ox**************@TK2MSFTNGP15.phx.gbl...

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> a ecrit dans le message de news:%2****************@TK2MSFTNGP10.phx.gbl...
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
Nov 16 '05 #6
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 [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:%2****************@TK2MSFTNGP10.phx.gbl...
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:uI**************@TK2MSFTNGP09.phx.gbl...
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 [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:O7**************@TK2MSFTNGP10.phx.gbl...
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" <no****@no.spam> wrote in message
news:Oq**************@TK2MSFTNGP12.phx.gbl...
> 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.
>
>


Nov 16 '05 #7

"Nick Malik" <ni*******@hotmail.nospam.com> a écrit dans le message de
news:WGX1d.48862$MQ5.29137@attbi_s52...

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 ?
Nov 16 '05 #8
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

"Mathieu Chavoutier" <no****@no.spam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"Nick Malik" <ni*******@hotmail.nospam.com> a écrit dans le message de
news:WGX1d.48862$MQ5.29137@attbi_s52...

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 ?

Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: --...
3
by: Mike | last post by:
I am using MS-Access as a front end for my MS-SQL DB. I have a sql view that uses the following: SELECT TOP 100 PERCENT RECID, PATNUMBER AS , SVCCODE AS , QTY, PROF_CHRGS AS , AMOUNT,...
4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
3
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
6
by: Lore Leunoeg | last post by:
Hello I derived a class MyControl from the Control class. Public Class MyControl Inherits Control Sub New() MyBase.New() End Sub End Class
9
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
5
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
7
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.