473,378 Members | 1,543 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,378 software developers and data experts.

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
14 2362
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Alberto Giménez | last post by:
Hi, I've seen some object oriented programming bits out there and i'm not sure if they're legal. For example: struct Object { int field1; int field2; }; struct SubObject { int field1; /*...
13
by: Jack MacRank | last post by:
Hello, I'm coding a webform application in C# (ASP.NET 1.1 SP1 with VS.NET 2003 Pro on WinXP SP2 using IIS 5.1). I created a seperate "data" class to house all the MySQL connection and sql...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
3
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
12
by: Jeremy Chapman | last post by:
I want to access the viewstate of a page object from outside of the class (in a helper class), so I made a class inherited from Page, and exposed a property that returns the viewstate object. I my...
12
by: Belebele | last post by:
Suppose that a class A depends on class B because an object of class B is passed into A's constructor. See below: class B { ... }; class A { public: A(B const& b); ... };
0
by: Pieter | last post by:
Hi, I'm using NHibernate 1.2 (CR1), and I'm using a custom list (inherited from BindingList(Of T) ) for all my lists. The NHibernate documentation told me that I had to implement...
1
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, Using VS2008 in a C# web service application, a class has been created that inherits from the ConfigurationSelection. This class file has been placed in the App_Code folder. The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.