?
=?iso-8859-1?B?Z/ZraGFu?=
Hi!
I fear this question might be too basic, however being a c++ veteran I
have trouble to get a good desing in c# running.
// a base class
class Vector{};
// inherited class
class VectorChild:Vector{};
class VectorChildChild:VectorChild{};
// some other class
class A
{
public void vectorfun(Vector a,Vector b){}
public override void fun(object a,object b) {
// ***********************
// my ugly solution....
// ***********************
if (typeof(Vector) == a.GetType() )
vectorfun( (Vector) a , (Vector) b); // unboxing to Vector
if (typeof(VectorChild) == a.GetType() )
vectorfun( (VectorChild) a , (VectorChild) b); // unboxing to
VectorChild
if (typeof(VectorChildChild) == a.GetType() )
vectorfun( (VectorChildChild) a , (VectorChildChild) b); //
unboxing to VectorChildChild
// ***********************
// ***********************
}
}
I would like to call the "fun" member of an instance of A
using Vector instances and VectorChild instances.
However, sofar I have to explicitly deal with any possible type using
the
if (typeof () == GetType)
lines...
Even if explictly define that VectorChild can be casted into Vector...
( which seems to be ridicilous since its a child) I have to deal with
objects in the ugly way as shown above.
What is the right thing to do?
In C++ the compiler is able to automatically cast down... How can I
achieve this behaviour with
C#?
Unfortunately I have to stick with the object type since my objects (of
arbitrary type) are stored in a HashTable.
I am a totally newbie to c# so excuses if I miss some fundamental
point...
which you guys hopefully tell me...
Thanks for any comments
gökhan
I fear this question might be too basic, however being a c++ veteran I
have trouble to get a good desing in c# running.
// a base class
class Vector{};
// inherited class
class VectorChild:Vector{};
class VectorChildChild:VectorChild{};
// some other class
class A
{
public void vectorfun(Vector a,Vector b){}
public override void fun(object a,object b) {
// ***********************
// my ugly solution....
// ***********************
if (typeof(Vector) == a.GetType() )
vectorfun( (Vector) a , (Vector) b); // unboxing to Vector
if (typeof(VectorChild) == a.GetType() )
vectorfun( (VectorChild) a , (VectorChild) b); // unboxing to
VectorChild
if (typeof(VectorChildChild) == a.GetType() )
vectorfun( (VectorChildChild) a , (VectorChildChild) b); //
unboxing to VectorChildChild
// ***********************
// ***********************
}
}
I would like to call the "fun" member of an instance of A
using Vector instances and VectorChild instances.
However, sofar I have to explicitly deal with any possible type using
the
if (typeof () == GetType)
lines...
Even if explictly define that VectorChild can be casted into Vector...
( which seems to be ridicilous since its a child) I have to deal with
objects in the ugly way as shown above.
What is the right thing to do?
In C++ the compiler is able to automatically cast down... How can I
achieve this behaviour with
C#?
Unfortunately I have to stick with the object type since my objects (of
arbitrary type) are stored in a HashTable.
I am a totally newbie to c# so excuses if I miss some fundamental
point...
which you guys hopefully tell me...
Thanks for any comments
gökhan