Inherit values from a base class parameter in a constructor?

B

Bjørn Brox

Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit


Hi!

Using .Net CF3.

I am receiving some class objects from a web service, and would like to
store this in my own class object with some additional values where the
base values is received as a parameter in the constructor.

My idea is something like the code example below, which gives the error
"Use of keyword 'base' is not valid in this context".

Any idea on how to solve this without having to copy each element or by
having my own "base" variable?

public class MyPassengerRC : PassengerRC
{
public bool is_arrived;

public MyPassengerRC(PassengerRC p)
{
base = p;
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Bjørn,

You can't do this. Basically, you will have to assign each member of
the base class to the corresponding member of the class you are trying to
copy the contents to.
 
P

parez

Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

Hi!

Using .Net CF3.

I am receiving some class objects from a web service, and would like to
store this in my own class object with some additional values where the
base values is received as a parameter in the constructor.

My idea is something like the code example below, which gives the error
"Use of keyword 'base' is not valid in this context".

Any idea on how to solve this without having to copy each element or by
having my own "base" variable?

public class MyPassengerRC : PassengerRC
{
public bool is_arrived;

public MyPassengerRC(PassengerRC p)
{
base = p;
}
}

you could create a constructor in your base class
PassengerRC(PassengerRC passengerRC)
{
//you will have to assign it here..
}


public class MyPassengerRC : PassengerRC
{
public bool is_arrived;

public MyPassengerRC(PassengerRC p)
: base(p)
{

}
}
 
B

Bjørn Brox

parez skrev:
you could create a constructor in your base class
PassengerRC(PassengerRC passengerRC)
{
//you will have to assign it here..
}

As I said: The base class PassengerRC comes from a webservice, not
locally declared and maintained.

Adding something to this class is not possible (If I could I would put
my own variables there).
 
I

Ignacio Machin ( .NET/ C# MVP )

Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

Hi!

Using .Net CF3.

I am receiving some class objects from a web service, and would like to
store this in my own class object with some additional values where the
base values is received as a parameter in the constructor.

My idea is something like the code example below, which gives the error
"Use of keyword 'base' is not valid in this context".

Any idea on how to solve this without having to copy each element or by
having my own "base" variable?

     public class MyPassengerRC : PassengerRC
     {
         public bool is_arrived;

         public MyPassengerRC(PassengerRC p)
         {
            base = p;
         }
     }

Hi,

that is not possible at all.


You have two options:
- If your class inherit from the received (do this only if they share
some features) you can manually assign the base properties,
- If you do not have to inherit your class can hold a refeence to the
received class as a member variable and you simply expose the
properties you need:

public class MyPassengerRC :
{
PassengerRC p;
public string Name{ get{ p.Name;} }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top