Click here to Skip to main content
15,902,939 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
public Connector(IShape source,IShape desti, double cost, IShape sourceentrance, IShape destientrance) : base(source.Center, desti.Center)
{
  this._cost = cost;
  this._source = source;
  this._desti = desti;
  this._entrancesourceID = sourceentrance;
  this._entrancedestiID = destientrance;
}

Class name is Connector and this is a constructor call
I want to do to this:
C#
if(source == null)
  base(sourceentrance.Centre, desti.Centre)
else 
  base(source.Centre, desti.Centre)

How can I do this?
Posted
Updated 16-Dec-10 4:20am
v4

You cannot do that.

The Base class constructor has to be called before the execution of any derived class constructor code - otherwise classes derived from a base class could not rely on or use any of the base class elements.

As a result, you can only access the base constructor from the definition of your class, by providing a fixed construtor reference using a colon and the base keyword.

To do what you want, you will have to extract the code from the base constructor into a public/protected method and access that from your derived constructor.
 
Share this answer
 
The simplest way is to change the base class constructor to take three parameters and make it decide what to do. The call would be:

base(source, sourceentrance, desti)


If you don't want to change anything you could try:

base( (source != NULL) ? source.Centre : sourceentrance.Centre, desti.Centre)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900