alternatives to unboxing..

Tech-Archive recommends: Fix windows errors by optimizing your registry



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

.



Relevant Pages

  • Re: alternatives to unboxing..
    ... Then fun() becomes just: ... public override void fun{ ... using Vector instances and VectorChild instances. ... In C++ the compiler is able to automatically cast down... ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: alternatives to unboxing..
    ... public override void fun ... using Vector instances and VectorChild instances. ... As VectorChild and VectorChildChild both are subclasses to your Vector, ... The code above would lead to an casting exception if the objects are of ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: alternatives to unboxing..
    ... you forgot to include that "A" also is a subclass of some ... public override void fun ... using Vector instances and VectorChild instances. ... If they weren't of an "arbitrary type", ...
    (microsoft.public.dotnet.languages.csharp)