Suppose I have a class called Vehicle with many fields. Now I make a new
class derived from Vehicle called Car. I'd like to make a method to copy the
data from a Vehicle instance to a Car instance. Is writing a long series of
assignments in a Clone() statement my only option?
I know that I can write a Clone method using MemberwiseClone(), but my
understanding is that the object created by MemberwiseClone is of the same
type. 11 2990
K.K.,
This isn't a good idea. What if you have a class, Airplane, derived
from Vehicle, and then pass that to a Car instance to be cloned? An
Airplane definitely isn't a car, so it shouldn't really work.
What is it that you are trying to do? If anything, a Car should clone a
Car, or anything deriving from Car.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"K.K." <kkaitan [at] gmail [dot] com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... Suppose I have a class called Vehicle with many fields. Now I make a new class derived from Vehicle called Car. I'd like to make a method to copy the data from a Vehicle instance to a Car instance. Is writing a long series of assignments in a Clone() statement my only option?
I know that I can write a Clone method using MemberwiseClone(), but my understanding is that the object created by MemberwiseClone is of the same type.
Nicholas,
Sorry, I guess I wasn't clear. I'm saying that I have an instance of a
Vehicle (the base class). Now I'd like to construct a new instance of a Car
whose base-class fields are equal to the Vehicle instance. I guess it's sort
of like a copy constructor for Car, except that I'm not using another Car as
the basis for the copying, but rather a Vehicle.
Hope that makes a little more sense. Thanks again!
- KK
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl... K.K.,
This isn't a good idea. What if you have a class, Airplane, derived from Vehicle, and then pass that to a Car instance to be cloned? An Airplane definitely isn't a car, so it shouldn't really work.
What is it that you are trying to do? If anything, a Car should clone
a Car, or anything deriving from Car.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"K.K." <kkaitan [at] gmail [dot] com> wrote in message news:%2****************@tk2msftngp13.phx.gbl... Suppose I have a class called Vehicle with many fields. Now I make a new class derived from Vehicle called Car. I'd like to make a method to copy the data from a Vehicle instance to a Car instance. Is writing a long series of assignments in a Clone() statement my only option?
I know that I can write a Clone method using MemberwiseClone(), but my understanding is that the object created by MemberwiseClone is of the
same type.
KK,
It does, but your logic is wrong, because a Vehicle can be something
that is not a Car, but a Car is always a Vehicle.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"K.K." <kkaitan [at] gmail [dot] com> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl... Nicholas,
Sorry, I guess I wasn't clear. I'm saying that I have an instance of a Vehicle (the base class). Now I'd like to construct a new instance of a Car whose base-class fields are equal to the Vehicle instance. I guess it's sort of like a copy constructor for Car, except that I'm not using another Car as the basis for the copying, but rather a Vehicle.
Hope that makes a little more sense. Thanks again!
- KK
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... K.K.,
This isn't a good idea. What if you have a class, Airplane, derived from Vehicle, and then pass that to a Car instance to be cloned? An Airplane definitely isn't a car, so it shouldn't really work.
What is it that you are trying to do? If anything, a Car should clone a Car, or anything deriving from Car.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"K.K." <kkaitan [at] gmail [dot] com> wrote in message news:%2****************@tk2msftngp13.phx.gbl... > Suppose I have a class called Vehicle with many fields. Now I make a > new > class derived from Vehicle called Car. I'd like to make a method to > copy > the > data from a Vehicle instance to a Car instance. Is writing a long > series > of > assignments in a Clone() statement my only option? > > I know that I can write a Clone method using MemberwiseClone(), but my > understanding is that the object created by MemberwiseClone is of the same > type. > >
But suppose I have an instance of something that is /just/ a Vehicle, as in
"Vehicle v = new Vehicle();". Such an instance doesn't represent a Car, or a
Boat, or an Airplane (yet). What I'm asking is how to place this information
/into/ a new Car, Boat, or Airplane.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ob**************@TK2MSFTNGP12.phx.gbl... KK,
It does, but your logic is wrong, because a Vehicle can be something that is not a Car, but a Car is always a Vehicle.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"K.K." <kkaitan [at] gmail [dot] com> wrote in message news:ON**************@TK2MSFTNGP10.phx.gbl... Nicholas,
Sorry, I guess I wasn't clear. I'm saying that I have an instance of a Vehicle (the base class). Now I'd like to construct a new instance of a Car whose base-class fields are equal to the Vehicle instance. I guess it's sort of like a copy constructor for Car, except that I'm not using another
Car as the basis for the copying, but rather a Vehicle.
Hope that makes a little more sense. Thanks again!
- KK
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... K.K.,
This isn't a good idea. What if you have a class, Airplane,
derived from Vehicle, and then pass that to a Car instance to be cloned? An Airplane definitely isn't a car, so it shouldn't really work.
What is it that you are trying to do? If anything, a Car should clone a Car, or anything deriving from Car.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"K.K." <kkaitan [at] gmail [dot] com> wrote in message news:%2****************@tk2msftngp13.phx.gbl... > Suppose I have a class called Vehicle with many fields. Now I make a > new > class derived from Vehicle called Car. I'd like to make a method to > copy > the > data from a Vehicle instance to a Car instance. Is writing a long > series > of > assignments in a Clone() statement my only option? > > I know that I can write a Clone method using MemberwiseClone(), but
my > understanding is that the object created by MemberwiseClone is of the same > type. > >
KK,
That's the thing, you wouldn't be able to, because your Vehicle should
be abstract, you shouldn't be able to have an instance of it. It should
always be an instance of a derived type.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"K.K." <kkaitan [at] gmail [dot] com> wrote in message
news:OG**************@TK2MSFTNGP09.phx.gbl... But suppose I have an instance of something that is /just/ a Vehicle, as in "Vehicle v = new Vehicle();". Such an instance doesn't represent a Car, or a Boat, or an Airplane (yet). What I'm asking is how to place this information /into/ a new Car, Boat, or Airplane.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Ob**************@TK2MSFTNGP12.phx.gbl... KK,
It does, but your logic is wrong, because a Vehicle can be something that is not a Car, but a Car is always a Vehicle.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"K.K." <kkaitan [at] gmail [dot] com> wrote in message news:ON**************@TK2MSFTNGP10.phx.gbl... > Nicholas, > > Sorry, I guess I wasn't clear. I'm saying that I have an instance of a > Vehicle (the base class). Now I'd like to construct a new instance of a > Car > whose base-class fields are equal to the Vehicle instance. I guess it's > sort > of like a copy constructor for Car, except that I'm not using another Car > as > the basis for the copying, but rather a Vehicle. > > Hope that makes a little more sense. Thanks again! > > - KK > > > "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> > wrote > in > message news:%2****************@TK2MSFTNGP09.phx.gbl... >> K.K., >> >> This isn't a good idea. What if you have a class, Airplane, derived >> from Vehicle, and then pass that to a Car instance to be cloned? An >> Airplane definitely isn't a car, so it shouldn't really work. >> >> What is it that you are trying to do? If anything, a Car should >> clone > a >> Car, or anything deriving from Car. >> >> Hope this helps. >> >> >> -- >> - Nicholas Paldino [.NET/C# MVP] >> - mv*@spam.guard.caspershouse.com >> >> "K.K." <kkaitan [at] gmail [dot] com> wrote in message >> news:%2****************@tk2msftngp13.phx.gbl... >> > Suppose I have a class called Vehicle with many fields. Now I make a >> > new >> > class derived from Vehicle called Car. I'd like to make a method to >> > copy >> > the >> > data from a Vehicle instance to a Car instance. Is writing a long >> > series >> > of >> > assignments in a Clone() statement my only option? >> > >> > I know that I can write a Clone method using MemberwiseClone(), but my >> > understanding is that the object created by MemberwiseClone is of >> > the > same >> > type. >> > >> > >> >> > >
Nick,
I don't think I'm explaining myself very well. Maybe this example will clear
it up:
class BankAccount
{
public string Name;
public int AccountNumber;
// ... many other details
}
class SavingsAccount : BankAccount
{
public int InterestRate;
}
class CheckingAccount : BankAccount
{
public int MaximumChecks;
}
<<<<
Going by the above, my questions are:
1.) Is it wrong for BankAccount to not be abstract?
2.) A SavingsAccount is only marginally different from a CheckingAccount,
and both are BankAccount objects. What if I had a new instance of a
CheckingAccount and I wanted to copy the BankAccount portion of an existing
SavingsAccount instance into the new CheckingAccount instance? How might I
do this, without simply making a long series of assignment statements? (Or
is that the only way?)
3.) As question (2), but now suppose that I just have a BankAccount object
instead of a SavingsAccount. How might I copy the BankAccount instance
fields into the new CheckingAccount?
Thanks again for all your help so far.
Nicholas,
I'm not sure I agree... Just because an object is a parent to a derived
class, it doesn't mean it has to be an abstract class or interface.
I derived from a Form for a scanner, and called it ScanForm. It "is a" form,
but has extended functionality, this doesn't mean that Form must therefore
be abstract.
The vehicle example does suggest that the parent will have no use other than
defining a template from which other classes inherit, but it's not
definitively so.
Dan.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl... KK,
That's the thing, you wouldn't be able to, because your Vehicle should be abstract, you shouldn't be able to have an instance of it. It should always be an instance of a derived type.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"K.K." <kkaitan [at] gmail [dot] com> wrote in message news:OG**************@TK2MSFTNGP09.phx.gbl... But suppose I have an instance of something that is /just/ a Vehicle, as in "Vehicle v = new Vehicle();". Such an instance doesn't represent a Car, or a Boat, or an Airplane (yet). What I'm asking is how to place this information /into/ a new Car, Boat, or Airplane.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Ob**************@TK2MSFTNGP12.phx.gbl... KK,
It does, but your logic is wrong, because a Vehicle can be something that is not a Car, but a Car is always a Vehicle.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"K.K." <kkaitan [at] gmail [dot] com> wrote in message news:ON**************@TK2MSFTNGP10.phx.gbl... > Nicholas, > > Sorry, I guess I wasn't clear. I'm saying that I have an instance of a > Vehicle (the base class). Now I'd like to construct a new instance of > a > Car > whose base-class fields are equal to the Vehicle instance. I guess > it's > sort > of like a copy constructor for Car, except that I'm not using another Car > as > the basis for the copying, but rather a Vehicle. > > Hope that makes a little more sense. Thanks again! > > - KK > > > "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> > wrote > in > message news:%2****************@TK2MSFTNGP09.phx.gbl... >> K.K., >> >> This isn't a good idea. What if you have a class, Airplane, derived >> from Vehicle, and then pass that to a Car instance to be cloned? An >> Airplane definitely isn't a car, so it shouldn't really work. >> >> What is it that you are trying to do? If anything, a Car should >> clone > a >> Car, or anything deriving from Car. >> >> Hope this helps. >> >> >> -- >> - Nicholas Paldino [.NET/C# MVP] >> - mv*@spam.guard.caspershouse.com >> >> "K.K." <kkaitan [at] gmail [dot] com> wrote in message >> news:%2****************@tk2msftngp13.phx.gbl... >> > Suppose I have a class called Vehicle with many fields. Now I make >> > a >> > new >> > class derived from Vehicle called Car. I'd like to make a method to >> > copy >> > the >> > data from a Vehicle instance to a Car instance. Is writing a long >> > series >> > of >> > assignments in a Clone() statement my only option? >> > >> > I know that I can write a Clone method using MemberwiseClone(), but my >> > understanding is that the object created by MemberwiseClone is of >> > the > same >> > type. >> > >> > >> >> > >
1).
This depends entirely on the context in which BankAccount is used.
If a BankAccount must be either Savings or Checking, then definately yes,
BankAccount must be abstract.
2)
You can't copy between two different types. A string can't be copied into an
int... It can, however, be converted. What I'd suggest for each class is
that if you need to cast/convert between two different accounts, then in
your SavingsAccount class, have a "ToCheckingAccount()" method. Conversely,
in the CheckingAccount, have a "ToSavingsAccount()" method. These functions
would return an instance of an object that has had the fields populated
according to the source. You'd have to reference each field in these objects
that you want to pass back to your accounts function.
3)
This relies on the answer to (1). If a BankAccount must be Savings or
Checking, then you should never have an instance of BankAccount.
Note that you can have the following though:
BankAccount myBankAccount = (BankAccount)new SavingsAccount();
This would allow you to create a base class variable, then depending what
you want, you could assign either a Savings or Checking account to it, and
access methods that are declared in the parent, but overridden in the
children.
HTH.
Dan.
"K.K." <kkaitan [at] gmail [dot] com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... Nick,
I don't think I'm explaining myself very well. Maybe this example will clear it up:
>
class BankAccount { public string Name; public int AccountNumber; // ... many other details }
class SavingsAccount : BankAccount { public int InterestRate; }
class CheckingAccount : BankAccount { public int MaximumChecks; } <<<<
Going by the above, my questions are:
1.) Is it wrong for BankAccount to not be abstract? 2.) A SavingsAccount is only marginally different from a CheckingAccount, and both are BankAccount objects. What if I had a new instance of a CheckingAccount and I wanted to copy the BankAccount portion of an existing SavingsAccount instance into the new CheckingAccount instance? How might I do this, without simply making a long series of assignment statements? (Or is that the only way?) 3.) As question (2), but now suppose that I just have a BankAccount object instead of a SavingsAccount. How might I copy the BankAccount instance fields into the new CheckingAccount?
Thanks again for all your help so far.
Dan,
You are right, this isn't definitive, but based on the example the OP
initially gave, it would seem so =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:uY**************@TK2MSFTNGP15.phx.gbl... Nicholas,
I'm not sure I agree... Just because an object is a parent to a derived class, it doesn't mean it has to be an abstract class or interface. I derived from a Form for a scanner, and called it ScanForm. It "is a" form, but has extended functionality, this doesn't mean that Form must therefore be abstract.
The vehicle example does suggest that the parent will have no use other than defining a template from which other classes inherit, but it's not definitively so.
Dan. "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... KK,
That's the thing, you wouldn't be able to, because your Vehicle should be abstract, you shouldn't be able to have an instance of it. It should always be an instance of a derived type.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"K.K." <kkaitan [at] gmail [dot] com> wrote in message news:OG**************@TK2MSFTNGP09.phx.gbl... But suppose I have an instance of something that is /just/ a Vehicle, as in "Vehicle v = new Vehicle();". Such an instance doesn't represent a Car, or a Boat, or an Airplane (yet). What I'm asking is how to place this information /into/ a new Car, Boat, or Airplane.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Ob**************@TK2MSFTNGP12.phx.gbl... KK,
It does, but your logic is wrong, because a Vehicle can be something that is not a Car, but a Car is always a Vehicle.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"K.K." <kkaitan [at] gmail [dot] com> wrote in message news:ON**************@TK2MSFTNGP10.phx.gbl... > Nicholas, > > Sorry, I guess I wasn't clear. I'm saying that I have an instance of > a > Vehicle (the base class). Now I'd like to construct a new instance of > a > Car > whose base-class fields are equal to the Vehicle instance. I guess > it's > sort > of like a copy constructor for Car, except that I'm not using another Car > as > the basis for the copying, but rather a Vehicle. > > Hope that makes a little more sense. Thanks again! > > - KK > > > "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> > wrote > in > message news:%2****************@TK2MSFTNGP09.phx.gbl... >> K.K., >> >> This isn't a good idea. What if you have a class, Airplane, derived >> from Vehicle, and then pass that to a Car instance to be cloned? An >> Airplane definitely isn't a car, so it shouldn't really work. >> >> What is it that you are trying to do? If anything, a Car should >> clone > a >> Car, or anything deriving from Car. >> >> Hope this helps. >> >> >> -- >> - Nicholas Paldino [.NET/C# MVP] >> - mv*@spam.guard.caspershouse.com >> >> "K.K." <kkaitan [at] gmail [dot] com> wrote in message >> news:%2****************@tk2msftngp13.phx.gbl... >> > Suppose I have a class called Vehicle with many fields. Now I make >> > a >> > new >> > class derived from Vehicle called Car. I'd like to make a method >> > to >> > copy >> > the >> > data from a Vehicle instance to a Car instance. Is writing a long >> > series >> > of >> > assignments in a Clone() statement my only option? >> > >> > I know that I can write a Clone method using MemberwiseClone(), >> > but my >> > understanding is that the object created by MemberwiseClone is of >> > the > same >> > type. >> > >> > >> >> > >
K.K.
I would solve this problem by providing an alternate constructor on
Car:
public Car (Vehicle baseVehicle, ... other initialization arguments ...
) : base(baseVehicle)
{
.... copy other arguments in here...
}
This requires only that you provide a constructor in Vehicle that takes
a Vehicle, and yes, you have to write a bunch of assignment statements.
The good news is that you have to do it only once, in the base class.
I would do it this way because it is not, in fact, a Clone operation.
You're instantiating a new Car based on an existing Vehicle. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: justanotherguy63 |
last post by:
Hi,
I am designing an application where to preserve the hierachy and for
code substitability, I need to pass an array of derived class object in
place of an array of base class object. Since I...
|
by: J.J. Feminella |
last post by:
(Please disregard the previous message; I accidentally sent it before it was
completed.)
I have source code similar to the following.
public class Vehicle
{
protected string dataV;
// ......
|
by: Edward Diener |
last post by:
Since a C++ using declaration isn't allowed in MC++, is there a way to
specify that a property, method, event, or field's access can be changed in
a derived class, ie. is protected in one class and...
|
by: Tom |
last post by:
I am trying to put together a system that involves 3 classes derived
form a common base class, which is working fine.
Except that I want to store them in a map and I find that I cannot do
this...
|
by: pazabo |
last post by:
Hi,
I'm trying to somehow implement polymorphic object cloning (just as it
is in Java), but when I write:
class Object {
public:
virtual Object * clone() const = 0;
// ...
}
| |
by: Frederic Rentsch |
last post by:
Hi all,
If I derive a class from another one because I need a few extra
features, is there a way to promote the base class to the derived one
without having to make copies of all attributes?
...
|
by: KishorAditya |
last post by:
Hi All,
Consider the following scenario:
class Top { };
class Left: virtual public Top { };
class Right: virtual public Top { };
class Bottom: public Left, public Right {};
Many books propose...
|
by: raylopez99 |
last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep
cloning versus shallow cloning. The scanned pages of this book are
found here: http://www.sendspace.com/file/mjyocg (Word...
|
by: Dom Jackson |
last post by:
I have a program which crashes when:
1 - I use static_cast to turn a base type pointer into a pointer to a
derived type
2 - I use this new pointer to call a function in an object of the...
|
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,...
|
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...
| |
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...
|
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...
|
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,...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |