473,402 Members | 2,053 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,402 software developers and data experts.

Casting and interface question

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( )
}
}


Nov 16 '05 #1
4 949
No, unless they share the same hierarchy. But you could cast them as the
interface.

"Jack Addington" <ja********@shaw.ca> wrote in message
news:uN**************@TK2MSFTNGP15.phx.gbl...
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( )
}
}

Nov 16 '05 #2
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]
- mv*@spam.guard.caspershouse.com
"Jack Addington" <ja********@shaw.ca> wrote in message
news:uN**************@TK2MSFTNGP15.phx.gbl...
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( )
}
}

Nov 16 '05 #3
Jack Addington wrote:
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;
--
Tom Porterfield
Nov 16 '05 #4
perfect..

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

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OC**************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com
"Jack Addington" <ja********@shaw.ca> wrote in message
news:uN**************@TK2MSFTNGP15.phx.gbl...
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( )
}
}


Nov 16 '05 #5

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

Similar topics

35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
4
by: Chris | last post by:
I created a control that derives from the System.Windows.Forms.Control namespace. I then created an interface to which this new control must adhere, so that I could require future controls to...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
3
by: JimM | last post by:
I am trying to create a method in VS 2003 that validates an object argument is of the proper type and within a range of values. I am trying to use a Type to define the casting and object type for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.