Casting and interface question

J

Jack Addington

Can 2 objects that are quite different but share a common interface be
casted to one another? I would like to have some generic code have a
ValidatorInstance and I would like a specific BusinessObject to also have
that instance. At some point when I decide that the generic object is going
to have the BO validations I want to sub the BO in as the validator?

ie)

Class A : AncA, ISomething, ISomethingElse, ICommon

Class B: AncB, IDifferent, ICommon

Class C
{
A ObjA = null;

}

Class D
{
B ObjB = new B( );
C ObjC = new C( );

someMethod( ){
objB.SomeAction;
objC.A = (A)objB; *** Is this legit?
objC.A.ICommonMethod( )
}
}
 
P

Peter Rilling

No, unless they share the same hierarchy. But you could cast them as the
interface.
 
N

Nicholas Paldino [.NET/C# MVP]

Jack,

No, you can't do that. Ultimately, they are two different types. You
can only cast A and B to common implementations, in this case, ICommon. To
be able to do otherwise would imply a transformation, which the CLR doesn't
know how to do.

Hope this helps.
 
T

Tom Porterfield

Jack said:
Can 2 objects that are quite different but share a common interface be
casted to one another? I would like to have some generic code have a
ValidatorInstance and I would like a specific BusinessObject to also have
that instance. At some point when I decide that the generic object is going
to have the BO validations I want to sub the BO in as the validator?

ie)

Class A : AncA, ISomething, ISomethingElse, ICommon

Class B: AncB, IDifferent, ICommon

Class C
{
A ObjA = null;

}

Class D
{
B ObjB = new B( );
C ObjC = new C( );

someMethod( ){
objB.SomeAction;
objC.A = (A)objB; *** Is this legit?
objC.A.ICommonMethod( )
}
}

I believe you will find you need to cast to what is common, the
interface. So your line in question would be:

objC.A = (ICommon)objB;
 
J

Jack Addington

perfect..

(ICommon)ObjB.ICommonMethod( ) works fine for me.

Nicholas Paldino said:
Jack,

No, you can't do that. Ultimately, they are two different types. You
can only cast A and B to common implementations, in this case, ICommon.
To be able to do otherwise would imply a transformation, which the CLR
doesn't know how to do.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Jack Addington said:
Can 2 objects that are quite different but share a common interface be
casted to one another? I would like to have some generic code have a
ValidatorInstance and I would like a specific BusinessObject to also have
that instance. At some point when I decide that the generic object is
going to have the BO validations I want to sub the BO in as the
validator?

ie)

Class A : AncA, ISomething, ISomethingElse, ICommon

Class B: AncB, IDifferent, ICommon

Class C
{
A ObjA = null;

}

Class D
{
B ObjB = new B( );
C ObjC = new C( );

someMethod( ){
objB.SomeAction;
objC.A = (A)objB; *** Is this legit?
objC.A.ICommonMethod( )
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top