I'm working on a project and I need some help with C# constructors. Let's say I have the following;

public class vehicle
{
   public vehicle()
  {
    //do some initialization for all vehicles
  }
}

public class automobile : vehicle
{
   public auto : base
  {
    // do specific initialization for auto
  }
}

public class truck : auto
{
   public truck : base
  {
    // do specific initialization for truck
  }
}

Now, I want truck to inherit from from automobile because there are methods that I want to reuse. However, I do not want to the call the auto constructor, I want to call the vehicle constructor instead.

Anyone know of a way I can do that?

Thanks in advance.

Recommended Answers

All 9 Replies

Put the common methods in the vehicle class that are common to both truck and auto (car?).

Also, just FYI, you named class Automobile with constructor auto...

That will work, but I was hoping for something a bit cleaner. Like being able to call the constructor from the Vehicle class directly somehow

That will work, but I was hoping for something a bit cleaner. Like being able to call the constructor from the Vehicle class directly somehow

Gosh, now that I've been programming in C# for a little while, I want to ask why, but I remember wanting to do something similar some time ago....elaborate why you want to do this.

Well I want to call the base (vehicle) constructor but not the auto one because I want to pick up the vehicle initialization code.

Well, I don't know what a vehicle intialization code is, but it is probably something that both automobile (car) and truck have, and something that definitely belongs in a base like vehicle and you probably don't need it defined in auto. However, you can override methods to provide different information depending on the usage....

First please copy free-syntax error code

public class vehicle
        {
            public vehicle()
            {
                MessageBox.Show("I'm Vehicle");
                //do some initialization for all vehicles
            }
        }

        public class automobile : vehicle
        {
            public automobile()
            //: base()
            {
                MessageBox.Show("I'm Automobile");
                // do specific initialization for auto
            }
        }

        public class truck : automobile
        {
            public truck() //: base()
                
            {
                MessageBox.Show("I'm Truck");
                // do specific initialization for truck
            }
        }

So, you don't need Automobile construction

public class automobile : vehicle
        {
            //public automobile()
            //    //: base()
            //{
            //    MessageBox.Show("I'm Automobile");
            //    // do specific initialization for auto
            //}
        }

You can't stop the execution of base class constructor.

Constructors of base/super class are always executed before the execution of derived class constructor. By default, a no argument constructor of base class is called or invoked when you instantiate a derived/sub class.

Take a look at this code,

public class Base{
   public Base() { 
     Console.WriteLine("Base : No argument constructor");
   }
   public Base(int n) { 
     Console.WriteLine("Base: Int argument"); 
   }
}
public class Der1 : Base{
  public Der1() { 
     Console.WriteLine("Der1 : No argument constructor");
  }
  public Der1(int n) { 
     Console.WriteLine("Der1: Int argument");
  }
  public Der1(String n) { 
     Console.WriteLine("Der1: String argument"); 
  }
}
public class Der2 : Der1{
  public Der2() { 
     Console.WriteLine("Der2 : No argument constructor");
  }
  public Der2(float n) { 
     Console.WriteLine("Der2: Int argument");
  }
  public Der2(String n) { 
     Console.WriteLine("Der2: String argument"); 
  }
}
public class Test{
     static void Main(string[] args){
            Der2 a = new Der2(10.0f);
            Der2 b = new Der2();
            Der2 c = new Der2("Hello");
      }
}

Use base keyword to execute suitable base class constructor. The base keyword is used to access members of the base/super class from within a derived/sub class.

You shouldn't want to stop base constructors from being called -- ever. This is probably a design issue so please post more code or what your intensions are. If a base class implements a List<string> that and has classes the derive from it -- then it stands to reason they will all use the same member since they're inheriting from the base (extendending the class) so it only makes sense that the base constructor should be responsible for members declared within its scope. This is a "separation of duties" if you will.

Regardless as adatapost's excellent example showed the base constructor will be called first you could always override the behavior in your constructor which will run later. Yes this might burn a microsecond of processing time BUT oh well, it is still a better decision from a design standpoint.

Thank you all for your suggestions, I have rewritten it and got it working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.