requirement to cast from one custom object to another custom object

  • Thread starter Thread starter Craig Buchanan
  • Start date Start date
C

Craig Buchanan

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
 
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 said:
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...
 
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
 
Andrew J. Marshall said:
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?
 
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
 
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 said:
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.
 
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
 
Andrew J. Marshall said:
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...
 
Hi CJ,

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
 
Hehe...

I agree.

=)
Cor said:
Hi CJ,


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
 
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
 
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!
 
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...

=)
 
Back
Top