467,169 Members | 983 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,169 developers. It's quick & easy.

requirement to cast from one custom object to another custom object

If I have two custom vb.net classes, where 80% of the properties are alike
and there is one method with a matching signature, can i cast between one
and the other? do i need to have each class implement a common interface?

thanks,

craig buchanan
Nov 20 '05 #1
  • viewed: 2119
Share:
14 Replies
No and Yes.

You should either define an interface or a common base class (same concept
really). Then you can access the derived class methods through references
to the base class.

"Craig Buchanan" <so*****@somewhere.com> wrote in message
news:eQ****************@TK2MSFTNGP11.phx.gbl...
If I have two custom vb.net classes, where 80% of the properties are alike
and there is one method with a matching signature, can i cast between one
and the other? do i need to have each class implement a common interface?

thanks,

craig buchanan

Nov 20 '05 #2

"Craig Buchanan" <so*****@somewhere.com> wrote in message
news:eQ****************@TK2MSFTNGP11.phx.gbl...
If I have two custom vb.net classes, where 80% of the properties are alike
and there is one method with a matching signature, can i cast between one
and the other? do i need to have each class implement a common interface?

Yep... or they inherit from the same parent... In which case those common
methods would be overridden (sp?) by the child classes, and then you could
cast to the parent type, but not directly between the types.

Interface would be fine too...
thanks,

craig buchanan

Nov 20 '05 #3
Craig,

BEWARE!!!

You could go the base class/interface route, but is that the "correct"
architectural decision. Are these two classes REALLY two subtypes of
another, more generic thing (inheritance)? Or do they both conform the same
contract (interfaces)?

Instead, you could create some sort of observer/inspector class that knows
how get the required information from your two classes. This keeps your
classes pure if inheritance/interface implementation it not called for.

Just a thought,
Andrew J. Marshall
MCP (Visual Basic)
Fairfax, VA

"Craig Buchanan" <so*****@somewhere.com> wrote in message
news:eQ****************@TK2MSFTNGP11.phx.gbl...
If I have two custom vb.net classes, where 80% of the properties are alike
and there is one method with a matching signature, can i cast between one
and the other? do i need to have each class implement a common interface?

thanks,

craig buchanan

Nov 20 '05 #4

"Andrew J. Marshall" <An*************@ObjectVision.netANTISPAMDEVICE> wrote
in message news:uR****************@TK2MSFTNGP10.phx.gbl...
Craig,

BEWARE!!!

You could go the base class/interface route, but is that the "correct"
architectural decision. Are these two classes REALLY two subtypes of
another, more generic thing (inheritance)? Or do they both conform the same contract (interfaces)?

Instead, you could create some sort of observer/inspector class that knows
how get the required information from your two classes. This keeps your
classes pure if inheritance/interface implementation it not called for.

What the hell are you talking about?
Just a thought,
Andrew J. Marshall
MCP (Visual Basic)
Fairfax, VA

"Craig Buchanan" <so*****@somewhere.com> wrote in message
news:eQ****************@TK2MSFTNGP11.phx.gbl...
If I have two custom vb.net classes, where 80% of the properties are alike and there is one method with a matching signature, can i cast between one and the other? do i need to have each class implement a common interface?
thanks,

craig buchanan


Nov 20 '05 #5
if i create an interface and implement in each class, and i cast from one to
the other, only the property represented by the interface will be preserved
in the target class, right?

craig

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c1*******************@news.demon.co.uk...
No and Yes.

You should either define an interface or a common base class (same concept
really). Then you can access the derived class methods through references
to the base class.

"Craig Buchanan" <so*****@somewhere.com> wrote in message
news:eQ****************@TK2MSFTNGP11.phx.gbl...
If I have two custom vb.net classes, where 80% of the properties are alike and there is one method with a matching signature, can i cast between one and the other? do i need to have each class implement a common interface?
thanks,

craig buchanan


Nov 20 '05 #6
Lol -

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com...

"Andrew J. Marshall" <An*************@ObjectVision.netANTISPAMDEVICE> wrote in message news:uR****************@TK2MSFTNGP10.phx.gbl...
Craig,

BEWARE!!!

You could go the base class/interface route, but is that the "correct"
architectural decision. Are these two classes REALLY two subtypes of
another, more generic thing (inheritance)? Or do they both conform the

same
contract (interfaces)?

Instead, you could create some sort of observer/inspector class that knows
how get the required information from your two classes. This keeps your
classes pure if inheritance/interface implementation it not called for.


What the hell are you talking about?
Just a thought,
Andrew J. Marshall
MCP (Visual Basic)
Fairfax, VA

"Craig Buchanan" <so*****@somewhere.com> wrote in message
news:eQ****************@TK2MSFTNGP11.phx.gbl...
If I have two custom vb.net classes, where 80% of the properties are

alike and there is one method with a matching signature, can i cast between one and the other? do i need to have each class implement a common interface?
thanks,

craig buchanan



Nov 20 '05 #7
You don't cast to the other class, you cast to the interface...

and right.. only those common will be accessible through the interface
itself, but you could always recast it if so desired, but, thats kinda
pointless now isn't it. =)
"Craig Buchanan" <so*****@somewhere.com> wrote in message
news:eO****************@tk2msftngp13.phx.gbl...
if i create an interface and implement in each class, and i cast from one to the other, only the property represented by the interface will be preserved in the target class, right?

craig

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c1*******************@news.demon.co.uk...
No and Yes.

You should either define an interface or a common base class (same concept
really). Then you can access the derived class methods through references to the base class.

"Craig Buchanan" <so*****@somewhere.com> wrote in message
news:eQ****************@TK2MSFTNGP11.phx.gbl...
If I have two custom vb.net classes, where 80% of the properties are

alike and there is one method with a matching signature, can i cast between one and the other? do i need to have each class implement a common interface?
thanks,

craig buchanan



Nov 20 '05 #8
* "Craig Buchanan" <so*****@somewhere.com> scripsit:
If I have two custom vb.net classes, where 80% of the properties are alike
and there is one method with a matching signature, can i cast between one
and the other? do i need to have each class implement a common interface?


Both classes will have to implement a common interface and/or inherit
from the same base class.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet/>
Nov 20 '05 #9

"Andrew J. Marshall" <An*************@ObjectVision.netANTISPAMDEVICE> wrote
in message news:%2***************@TK2MSFTNGP12.phx.gbl...
LOL as well!!!

I'm just trying to impress an obvious beginner that inheritance isn't always the right thing to do. In fact it's the wrong thing to do.

Again... I reiterate... what the hell are you talking about?
Just because my implementations of Moose and Pig share 80% of the same
functionality (namely Burp, Defecate and Urinate), doesn't mean I should
create a BurperDefecaterAndUrinator class/interface for them to
inherit/implement.

Why wouldn't you? That is actually a perfect example of using inheritance,
espeically implementation inheritance. I begin to question who is the
obvious beginner...
That's what the hell I'm talking about, BABY! 8-)

Andrew J. Marshall
MCP (Visual Basic)
Fairfax, VA

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c1*******************@news.demon.co.uk...
Lol -

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com...

"Andrew J. Marshall" <An*************@ObjectVision.netANTISPAMDEVICE>

wrote
in message news:uR****************@TK2MSFTNGP10.phx.gbl...
> Craig,
>
> BEWARE!!!
>
> You could go the base class/interface route, but is that the "correct" > architectural decision. Are these two classes REALLY two subtypes of
> another, more generic thing (inheritance)? Or do they both conform the same
> contract (interfaces)?
>
> Instead, you could create some sort of observer/inspector class that

knows
> how get the required information from your two classes. This keeps your > classes pure if inheritance/interface implementation it not called for. >

What the hell are you talking about?

> Just a thought,
> Andrew J. Marshall
> MCP (Visual Basic)
> Fairfax, VA
>
> "Craig Buchanan" <so*****@somewhere.com> wrote in message
> news:eQ****************@TK2MSFTNGP11.phx.gbl...
> > If I have two custom vb.net classes, where 80% of the properties are alike
> > and there is one method with a matching signature, can i cast between one
> > and the other? do i need to have each class implement a common
interface?
> >
> > thanks,
> >
> > craig buchanan
> >
> >
>
>



Nov 20 '05 #10
Cor
Hi CJ,
Just because my implementations of Moose and Pig share 80% of the same
functionality (namely Burp, Defecate and Urinate), doesn't mean I should
create a BurperDefecaterAndUrinator class/interface for them to
inherit/implement.


Some believe that Darwin was right, some tell he is totaly wrong, I think
this is not the newsgroup to discus about that.

But just my thoughts.

:-))

(I think he was right by the way)

Cor
Nov 20 '05 #11
Hehe...

I agree.

=)
"Cor" <no*@non.com> wrote in message
news:ej*************@tk2msftngp13.phx.gbl...
Hi CJ,
Just because my implementations of Moose and Pig share 80% of the same
functionality (namely Burp, Defecate and Urinate), doesn't mean I should create a BurperDefecaterAndUrinator class/interface for them to
inherit/implement.


Some believe that Darwin was right, some tell he is totaly wrong, I think
this is not the newsgroup to discus about that.

But just my thoughts.

:-))

(I think he was right by the way)

Cor

Nov 20 '05 #12
Not trying to offend you or anything... just letting you know your telling
people the wrong things...

I'm glad you contribute... but, this is one of those times you were just
flat out wrong.. I'm wrong too (alot) and Cor/Herfried let me know.. or
Peter Huang, but he doesn't count.

-CJ

"Andrew J. Marshall" <An*************@ObjectVision.netANTISPAMDEVICE> wrote
in message news:%2***************@TK2MSFTNGP12.phx.gbl...
LOL as well!!!

I'm just trying to impress an obvious beginner that inheritance isn't always the right thing to do. In fact it's the wrong thing to do.

Just because my implementations of Moose and Pig share 80% of the same
functionality (namely Burp, Defecate and Urinate), doesn't mean I should
create a BurperDefecaterAndUrinator class/interface for them to
inherit/implement.

That's what the hell I'm talking about, BABY! 8-)

Andrew J. Marshall
MCP (Visual Basic)
Fairfax, VA

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c1*******************@news.demon.co.uk...
Lol -

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com...

"Andrew J. Marshall" <An*************@ObjectVision.netANTISPAMDEVICE>

wrote
in message news:uR****************@TK2MSFTNGP10.phx.gbl...
> Craig,
>
> BEWARE!!!
>
> You could go the base class/interface route, but is that the "correct" > architectural decision. Are these two classes REALLY two subtypes of
> another, more generic thing (inheritance)? Or do they both conform the same
> contract (interfaces)?
>
> Instead, you could create some sort of observer/inspector class that

knows
> how get the required information from your two classes. This keeps your > classes pure if inheritance/interface implementation it not called for. >

What the hell are you talking about?

> Just a thought,
> Andrew J. Marshall
> MCP (Visual Basic)
> Fairfax, VA
>
> "Craig Buchanan" <so*****@somewhere.com> wrote in message
> news:eQ****************@TK2MSFTNGP11.phx.gbl...
> > If I have two custom vb.net classes, where 80% of the properties are alike
> > and there is one method with a matching signature, can i cast between one
> > and the other? do i need to have each class implement a common
interface?
> >
> > thanks,
> >
> > craig buchanan
> >
> >
>
>



Nov 20 '05 #13
Aw crap, my bad.

I meant to say "In fact, *SOMETIMES* it's the wrong thing to do."

(NOTE: I must have looked like a complete idiot, instead of the partial
idiot that I am.)

Anyway, please don't tell me you think inheritance is right ALL the time.
What should I do when I want my Moose and Pig classes to have Flatulate
methods, add that to my BurperDefecaterAndUrinator class? But wait, my
Jabberwocky class also inherits from BurperDefecaterAndUrinator (because it
LOVES to do those things!), but due to a cruel evolutionary twist can not
generate flatulence. Now what?

I'm just trying to get Craig to think about alternatives that might not have
such a "permanent" impact on his code/architecture.

Andrew J. Marshall
MCP (Visual Basic)
Fairfax, VA

p.s. Offense only partly taken - given that this is a public forum, you
could (should?) have said "This seems like a radical comment. Could you
please explain in more detail?" That said, it was still funny as hell!

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com...
Not trying to offend you or anything... just letting you know your telling
people the wrong things...

I'm glad you contribute... but, this is one of those times you were just
flat out wrong.. I'm wrong too (alot) and Cor/Herfried let me know.. or
Peter Huang, but he doesn't count.

-CJ

"Andrew J. Marshall" <An*************@ObjectVision.netANTISPAMDEVICE> wrote in message news:%2***************@TK2MSFTNGP12.phx.gbl...
LOL as well!!!

I'm just trying to impress an obvious beginner that inheritance isn't

always
the right thing to do. In fact it's the wrong thing to do.

Just because my implementations of Moose and Pig share 80% of the same
functionality (namely Burp, Defecate and Urinate), doesn't mean I should
create a BurperDefecaterAndUrinator class/interface for them to
inherit/implement.

That's what the hell I'm talking about, BABY! 8-)

Andrew J. Marshall
MCP (Visual Basic)
Fairfax, VA

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c1*******************@news.demon.co.uk...
Lol -

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com...
>
> "Andrew J. Marshall" <An*************@ObjectVision.netANTISPAMDEVICE> wrote
> in message news:uR****************@TK2MSFTNGP10.phx.gbl...
> > Craig,
> >
> > BEWARE!!!
> >
> > You could go the base class/interface route, but is that the "correct" > > architectural decision. Are these two classes REALLY two subtypes of > > another, more generic thing (inheritance)? Or do they both conform the > same
> > contract (interfaces)?
> >
> > Instead, you could create some sort of observer/inspector class that knows
> > how get the required information from your two classes. This keeps

your
> > classes pure if inheritance/interface implementation it not called

for.
> >
>
> What the hell are you talking about?
>
> > Just a thought,
> > Andrew J. Marshall
> > MCP (Visual Basic)
> > Fairfax, VA
> >
> > "Craig Buchanan" <so*****@somewhere.com> wrote in message
> > news:eQ****************@TK2MSFTNGP11.phx.gbl...
> > > If I have two custom vb.net classes, where 80% of the properties are > alike
> > > and there is one method with a matching signature, can i cast

between
> one
> > > and the other? do i need to have each class implement a common
> interface?
> > >
> > > thanks,
> > >
> > > craig buchanan
> > >
> > >
> >
> >
>
>



Nov 20 '05 #14
Andrew,

Not a problem at all. I know that some of us (especially me) come off
rather coarse sometimes. But remember, its hard to express emotion on here
too.. I wasn't upset with you, was just like jokingly thinking, what on
earth are you talking about.
Aw crap, my bad.

I meant to say "In fact, *SOMETIMES* it's the wrong thing to do."

This is true...
(NOTE: I must have looked like a complete idiot, instead of the partial
idiot that I am.)

Anyway, please don't tell me you think inheritance is right ALL the time.
By no means is inheritance correct at all times. Appleman covers this topic
many times in his books (I like Dan, he's a good author), and in fact it can
be dangerous to use it if you don't know how/why your using inheritance.

A lot of people use it because it's the "IN" thing in programming (if there
is such a thing) and apparently if you use inheritance it makes you a better
programmer. Yes and no. Yes, if you can explain WHY you used inheritance.
What should I do when I want my Moose and Pig classes to have Flatulate
methods, add that to my BurperDefecaterAndUrinator class? But wait, my
Jabberwocky class also inherits from BurperDefecaterAndUrinator (because it LOVES to do those things!), but due to a cruel evolutionary twist can not
generate flatulence. Now what?

And you bring up an excellent point right there. What to do? Humans are
interesting creatures (sometimes amazes me how we built these amazing
machines) because we tend to try to simplify things based on
repition/previous results. i.e. take your going to the bathroom idea, do
you think about having to get up, then walk x steps, lift toilet seat (if
your a man... and married...) and THEN do your business? Nope... we just
want to make it as easy as possible and don't really think about it. Now,
if computers could do this it would be great, and they can but because of
programmers. =)

So, what is to say we cannot break it out first by intefaces. I.e.

IDefactor
::Defacate()
IUrinator
::Urinate()
IBurper
::Burp()

Now we could build a class that implements all three... or only a few...
in to a collection of base classes that our "animals"... (Ahh we can use
both types of inheritance, kinda sounds like ADO.NET doesn't it =) DataRow
which is abstract (mustinherit) is an implementation of IList, ITypedList,
and IBindingList...

So to answer your question, you can build a build a base type, but you don't
have to use that base type to do your cross reference calls (remember that
implementation gets pushed up through the heirarchy as well) so your base
class that implements IUrinator and then something inherits your base
class... that new class also inherits IUrinator, so we can directly cast to
IUrinator.. Thats if the only thing you care about is making your animal
urniate, not how... just the fact it does.
I'm just trying to get Craig to think about alternatives that might not have such a "permanent" impact on his code/architecture.

Exactly... my whole point is (as many will agree) inheritance is a
wonderful thing, however, it does take careful planning and management (as
most software does). So your point is valid there, your explanation was a
little off to why though. =)

Take care,

CJ

P.S. This is a fun place to be, don't get discouraged by doing anything
wrong... Hell, if I had a nickle for everythign I ever said wrong.. I'd
have a ****load of nickles...

=)

Andrew J. Marshall
MCP (Visual Basic)
Fairfax, VA

p.s. Offense only partly taken - given that this is a public forum, you
could (should?) have said "This seems like a radical comment. Could you
please explain in more detail?" That said, it was still funny as hell!

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com...
Not trying to offend you or anything... just letting you know your telling
people the wrong things...

I'm glad you contribute... but, this is one of those times you were just flat out wrong.. I'm wrong too (alot) and Cor/Herfried let me know.. or
Peter Huang, but he doesn't count.

-CJ

"Andrew J. Marshall" <An*************@ObjectVision.netANTISPAMDEVICE> wrote
in message news:%2***************@TK2MSFTNGP12.phx.gbl...
LOL as well!!!

I'm just trying to impress an obvious beginner that inheritance isn't

always
the right thing to do. In fact it's the wrong thing to do.

Just because my implementations of Moose and Pig share 80% of the same
functionality (namely Burp, Defecate and Urinate), doesn't mean I should create a BurperDefecaterAndUrinator class/interface for them to
inherit/implement.

That's what the hell I'm talking about, BABY! 8-)

Andrew J. Marshall
MCP (Visual Basic)
Fairfax, VA

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:c1*******************@news.demon.co.uk...
> Lol -
>
> "CJ Taylor" <no****@blowgoats.com> wrote in message
> news:10*************@corp.supernews.com...
> >
> > "Andrew J. Marshall" <An*************@ObjectVision.netANTISPAMDEVICE> > wrote
> > in message news:uR****************@TK2MSFTNGP10.phx.gbl...
> > > Craig,
> > >
> > > BEWARE!!!
> > >
> > > You could go the base class/interface route, but is that the

"correct"
> > > architectural decision. Are these two classes REALLY two subtypes of
> > > another, more generic thing (inheritance)? Or do they both
conform the
> > same
> > > contract (interfaces)?
> > >
> > > Instead, you could create some sort of observer/inspector class that > knows
> > > how get the required information from your two classes. This

keeps your
> > > classes pure if inheritance/interface implementation it not called for.
> > >
> >
> > What the hell are you talking about?
> >
> > > Just a thought,
> > > Andrew J. Marshall
> > > MCP (Visual Basic)
> > > Fairfax, VA
> > >
> > > "Craig Buchanan" <so*****@somewhere.com> wrote in message
> > > news:eQ****************@TK2MSFTNGP11.phx.gbl...
> > > > If I have two custom vb.net classes, where 80% of the properties are
> > alike
> > > > and there is one method with a matching signature, can i cast
between
> > one
> > > > and the other? do i need to have each class implement a

common > > interface?
> > > >
> > > > thanks,
> > > >
> > > > craig buchanan
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 20 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Alberto Giménez | last post: by
13 posts views Thread by Jack MacRank | last post: by
3 posts views Thread by mra | last post: by
5 posts views Thread by Nick Flandry | last post: by
12 posts views Thread by Jeremy Chapman | last post: by
12 posts views Thread by Belebele | last post: by
1 post views Thread by =?Utf-8?B?U2NvdHQ=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.