Hi there all,
Im curently working on my course work and got stuck on of the questions.
I have three classes : car, minibus and truck.
I want the car to be my base and minibus, truck to inherit the property of the car class.
I keep getting an error message :
'xxxx.Car' does not contain the constructor that takes 0 arguments. This is for both minibus and truck.
Here is my car class : -
-
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
-
namespace SD2coursework
-
{
-
/*
-
* Used as a GUI to create or edit instances of Car.
-
* The Car being created or set is stored in the 'car' propety
-
*
-
* To edit a car, instanciate this class, set the car property to the car object
-
* being edited, then use the ShowDialog() method to display the form.
-
*
-
* After calling showDialog() the newly created/modified car object can be accesed
-
* via the car property
-
*
-
*/
-
-
public partial class FrmCar : Form
-
{
-
private Car theCar = null; //Stores the value of the car property
-
-
public Car car //The car property
-
{
-
get
-
{
-
return theCar;
-
}
-
set
-
{ /*
-
* Used when editing an existing car
-
*/
-
theCar = value;
-
txtColour.Text = theCar.colour;
-
txtMake.Text = theCar.make;
-
txtRegNumber.Text = theCar.regnumber;
-
txtModel.Text = theCar.model;
-
}
-
}
-
-
public FrmCar()
-
{
-
InitializeComponent();
-
}
-
-
private void btnClose_Click(object sender, EventArgs e)
-
{
-
//Close the form. Causes FrmCar_FormClosed() to be called.
-
this.Close();
-
}
-
-
private void FrmCar_FormClosed(object sender, FormClosedEventArgs e)
-
{
-
/*
-
* When the form is closed, update the car property
-
* If the car property was not set previously (ie this is a new car) then
-
* create a new car object
-
*/
-
if (theCar == null) //If new car...
-
theCar = new Car(txtMake.Text, txtColour.Text, txtRegNumber.Text, txtModel.Text);
-
else
-
{ //if editing an existing car
-
theCar.colour = txtColour.Text;
-
theCar.make = txtMake.Text;
-
theCar.regnumber = txtRegNumber.Text;
-
theCar.model = txtModel.Text;
-
}
-
}
-
-
private void FrmCar_Load(object sender, EventArgs e)
-
{
-
-
}
-
-
private void txtMake_TextChanged(object sender, EventArgs e)
-
{
-
-
}
-
}
-
}
-
Minibus class : -
-
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
-
namespace SD2coursework
-
{
-
public class MiniBus : Car
-
/*This class represents a MiniBus. The MiniBus has three properties, make, colour and seat capacity.
-
*Note that the constructor requires values for make, colour and seat capacity */
-
{
-
-
private string theMake; //Holds the value of the make property
-
private string theColour; //Holds the value of the colour property
-
private string theSeatCap; //Holds the value of the seat capacity property
-
-
//Default constructor
-
public MiniBus(string aMake, string aColour, string aSeatCap)
-
{
-
theMake = aMake;
-
theColour = aColour;
-
theSeatCap = aSeatCap;
-
-
}
-
-
//Make property
-
public string make2
-
{
-
get
-
{
-
return theMake;
-
}
-
set
-
{
-
theMake = value;
-
}
-
}
-
//Colour property
-
public string colour2
-
{
-
get
-
{
-
return theColour;
-
}
-
set
-
{
-
theColour = value;
-
}
-
}
-
//Seat capacity property
-
public string seatcap
-
{
-
get
-
{
-
return theSeatCap;
-
}
-
set
-
{
-
theSeatCap = value;
-
}
-
}
-
-
}
-
}
-
Truck class : -
-
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
-
namespace SD2coursework
-
{
-
public class Truck : Car
-
/*This class represents a Truck. The truck has three properties, make, colour and gross weight.
-
*Note that the constructor requires values for make, colour and gross weight */
-
{
-
-
private string theMake; //Holds the value of the make property
-
private string theColour; //Holds the value of the colour property
-
private string theGrossW; //Holds the value of the gross weight property
-
-
//Default constructor
-
public Truck(string aMake, string aColour, string aGrossW)
-
{
-
theMake = aMake;
-
theColour = aColour;
-
theGrossW = aGrossW;
-
}
-
-
//Make property
-
public string make3
-
{
-
get
-
{
-
return theMake;
-
}
-
set
-
{
-
theMake = value;
-
}
-
}
-
//Colour property
-
public string colour3
-
{
-
get
-
{
-
return theColour;
-
}
-
set
-
{
-
theColour = value;
-
}
-
}
-
//Gross weight property
-
public string grossw
-
{
-
get
-
{
-
return theGrossW;
-
}
-
set
-
{
-
theGrossW = value;
-
}
-
}
-
-
}
-
}
-
I was wondering if Im doing the Inheritance right and what's this error message.
Thanks,
Mike
11 2116
Your car class is called FrmCar : - public partial class FrmCar : Form
So why do you call it by the name "Car" ? - public class MiniBus : Car
FrmCar is my form in which i create/add new cars.
Car.cs is the car class with properites ect.
Am I doing it the wrong way?
Thanks for you reply,
Mike
p.s Im using VS if that makes a difference.
Hi again,
I posted in the wrong code. It was not car.cs you were right it was FrmCar : )
Here is my code for the car class : -
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
-
namespace SD2coursework
-
{
-
public class Car
-
/*This class represents a Car. The car has four properties, make, colour, registration number and model.
-
*Note that the constructor requires values for make, colour, registration number and model */
-
-
{
-
private string theMake; //Holds the value of the make property
-
private string theColour; //Holds the value of the colour property
-
private string theRegNumber; //Holds the value of the registration number property
-
private string theModel; //Holds the value of the model property
-
-
//Default constructor
-
public Car(string aMake, string aColour, string aRegNumber, string aModel)
-
{
-
theMake = aMake;
-
theColour = aColour;
-
theRegNumber = aRegNumber;
-
theModel = aModel;
-
}
-
-
//Make property
-
public string make
-
{
-
get
-
{
-
return theMake;
-
}
-
set
-
{
-
theMake = value;
-
}
-
}
-
//Colour property
-
public string colour
-
{
-
get
-
{
-
return theColour;
-
}
-
set
-
{
-
theColour = value;
-
}
-
}
-
//Registration number property
-
public string regnumber
-
{
-
get
-
{
-
return theRegNumber;
-
}
-
set
-
{
-
theRegNumber = value;
-
}
-
}
-
//Model property
-
public string model
-
{
-
get
-
{
-
return theModel;
-
}
-
set
-
{
-
theModel = value;
-
}
-
}
-
}
-
}
-
I keep getting the error regarding the constructor which I mentioned erlier.
Sorry for the confusion and hopefuly you will be able to help me now.
Thanks Mike
Hi Michael,
A few things are wrong, both code and setup.
Lets start with the setup. A minibus is not a type of car, its a type of bus. A truck is also not a car.
What you want is something along of: - public interface IVehicle
-
{
-
Color bodycolor;
-
DateTime MakeYear;
-
//etc..
-
}
-
-
public class Car : IVehicle
-
{
-
{implement IVehicle members}
-
}
Because a minibus, a car and a truck are a type of vehicle.
Now for the error:
If you inherit a class, and you define a constructor in your base class with arguments, you need to define that too in the child class like so: - public abstract Vehicle
-
{
-
Color bodycolor;
-
DateTime MakeYear;
-
public Vehicle(Color color, DateTime creationDate)
-
{
-
bodycolor = color;
-
MakeYear = creationDate;
-
}
-
}
-
-
public class Car : Vehicle
-
{
-
int AmountDoors;
-
public Car(Color color, DateTime creationDate, int amountDoors) : base(Color color, DateTime creationDate)
-
{
-
AmountDoors = amountDoors;
-
}
-
}
The base class needs its arguments and is given by the child class through ": base ()".
-Bryan
Firstly, thanks for your help Bryan.
Now, Do I need to create a new class called vehicle which is going to be my base class and car, truck, minibus to inherit the properties of vehicle. By the way, this makes more sense: )
Now all three classes have two properties in common : make and colour. Which basically means my vehicle will only have these two right?
The only thing I am not to sure about is, how do I make it work. Let's say I have vehicle class with make and colour. How do I make the car, truck and minibus to use it and at the same time create the right vehicles?
I know it sounds confusing but that's where I am now :)
Thanks,
Mike
Hey Michael,
Its actually easier to do these things if you think of making sense, then of how the technique works ;)
Your Vehicle could be a abstract class or a interface, depends on what responsibilty it should have(thinking process).
If you want to make it like a blueprint you choose Interface because when you implement a interface, each implementing class must have the members the interface has. But a abstract class could already have the member so you would not need to implement it in the child classes. Also remember interface members are always public!
With interface: - public interface IVehicle
-
{
-
Color bodyColor;
-
DateTime makeDate;
-
}
-
-
public class Car : IVehicle
-
{
-
Color bodyColor;
-
DateTime makeDate;
-
public Car(Color bodycolor, DateTime makedate)
-
{
-
bodyColor = bodycolor;
-
makeDate = makedate;
-
}
-
}
-
Call: - Car yourcar = new Car(Color.Red, DateTime.Now);
With abstraction: - public abstract class Vehicle
-
{
-
Color bodyColor;
-
DateTime makeDate;
-
-
public Vehicle(Color bodyColor, DateTime makedate)
-
{
-
bodyColor = bodycolor;
-
makeDate = makedate;
-
}
-
}
-
-
public class Car : Vehicle
-
{
-
int amountDoors;
-
public Car(Color bodycolor, DateTime makedate, int amountDoors)
-
: base (bodycolor, makedate)
-
{
-
amountDoors = amountDoors;
-
}
-
}
Call: - Car yourcar = new Car(Color.Red, DateTime.Now, 4);// no vehicle call, the codes gets it because of the inheritence
Notice that with abstract you dont repeat the member because its already in the abstract base class. You just have to pass it. Now the next example is also a possibility: - public abstract class Vehicle
-
{
-
Color bodyColor;
-
DateTime makeDate;
-
-
public Vehicle()
-
{
-
}
-
}
-
-
public class Car : Vehicle
-
{
-
int amountDoors;
-
public Car(Color bodycolor, DateTime makedate, int amountDoors)
-
{
-
amountDoors = amountDoors;
-
bodyColor = bodycolor;
-
makeDate = makedate;
-
}
-
}
Because Car inherits the members of Vehicle, the field bodyColor and makeDate is also accesible(unless set private).
First think of the setup and the responsibility of each object before deciding you want to use abstract or interface :) depending on the responsibility you choose your technique.
-Bryan
Hi again everyone,
Last error :D
Finishing with FrmHire, please help! -
-
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
-
namespace SD2coursework
-
{
-
public partial class FrmHireCo : Form
-
/*
-
* This is the main form for the car hire system.
-
* It allows the adding to new cars to the system, and displaying them in the fleet list
-
*
-
*/
-
{
-
private Fleet myFleet = new Fleet();
-
//Fleet object used to store cars
-
-
public FrmHireCo()
-
{
-
//Default constructor
-
InitializeComponent();
-
}
-
-
-
private void updateFleetList()
-
{ //Used to update the fleet displayed on the form
-
lstFleet.Items.Clear();
-
foreach (Car c in myFleet.fleet)
-
{
-
string line = "Car: " + c.make+" " + c.colour+" " + c.regnumber+" " + c.model;
-
lstFleet.Items.Add(line);
-
}
-
lstFleet.Items.Clear();
-
foreach (MiniBus b in myFleet.fleet)
-
{
-
string line = "MiniBus: " + b.make + " " + b.colour +" " + b.seatcap;
-
lstFleet.Items.Add(line);
-
}
-
lstFleet.Items.Clear();
-
foreach (Truck t in myFleet.fleet)
-
{
-
string line = "Truck: " + t.make + " " + t.colour + " " + t.grossw;
-
lstFleet.Items.Add(line);
-
}
-
}
-
-
private void btnAddCar_Click(object sender, EventArgs e)
-
{
-
//Add a new car
-
FrmCar carGui = new FrmCar(); //Form used to add new car
-
carGui.ShowDialog();
-
Car myCar = carGui.car; //Get new car from form
-
myFleet.addToFleet(myCar); //Add to fleet list
-
updateFleetList(); //Uodate fleet list
-
-
}
-
-
private void lstFleet_SelectedIndexChanged(object sender, EventArgs e)
-
{
-
/*
-
* This method is used to control the list box
-
* It is called when a row is selected by the user, it then displays frmCar
-
* with the car details
-
*/
-
if (lstFleet.SelectedIndex > -1)
-
{
-
int index = lstFleet.SelectedIndex;
-
Car myCar = myFleet.fleet.ElementAt(index);
-
FrmCar carGui = new FrmCar();
-
carGui.car = myCar;
-
carGui.Show();
-
}
-
}
-
-
private void btnAddTruck_Click(object sender, EventArgs e)
-
{
-
//Add a new truck
-
FrmTruck truckGui = new FrmTruck(); //Form used to add new car
-
truckGui.ShowDialog();
-
Truck myTruck = truckGui.truck; //Get new car from form
-
myFleet.addToFleet(myTruck); //Add to fleet list
-
updateFleetList(); //Uodate fleet list
-
-
}
-
-
private void lstFleet_SelectedIndexChanged(object sender, EventArgs e)
-
{
-
/*
-
* This method is used to control the list box
-
* It is called when a row is selected by the user, it then displays frmCar
-
* with the car details
-
*/
-
if (lstFleet.SelectedIndex > -1)
-
{
-
int index = lstFleet.SelectedIndex;
-
Truck myTruck = myFleet.fleet.ElementAt(index);
-
FrmTruck truckGui = new FrmTruck();
-
truckGui.truck = myTruck;
-
truckGui.Show();
-
}
-
}
-
-
private void btnAddMiniBus_Click(object sender, EventArgs e)
-
{
-
//Add a new minibus
-
FrmBus minibusGui = new FrmBus(); //Form used to add new car
-
minibusGui.ShowDialog();
-
MiniBus myMiniBus = minibusGui.minibus; //Get new car from form
-
myFleet.addToFleet(myMiniBus); //Add to fleet list
-
updateFleetList(); //Uodate fleet list
-
-
}
-
-
private void lstFleet_SelectedIndexChanged(object sender, EventArgs e)
-
{
-
/*
-
* This method is used to control the list box
-
* It is called when a row is selected by the user, it then displays frmCar
-
* with the car details
-
*/
-
if (lstFleet.SelectedIndex > -1)
-
{
-
int index = lstFleet.SelectedIndex;
-
MiniBus myMiniBus = myFleet.fleet.ElementAt(index);
-
FrmBus minibusGui = new FrmBus();
-
minibusGui.minibus = myMiniBus;
-
minibusGui.Show();
-
}
-
}
-
-
-
-
}
-
}
-
The error message : Error 1 Type 'SD2coursework.FrmHireCo' already defines a member called 'lstFleet_SelectedIndexChanged' with the same parameter types xxxxxx3\assessment\Assessment\WindowsFormsApplicat ion1\FrmHire.cs 90 22 SD2coursework
You should read the error messages more often :( (no disrespect meant).
What the error message says is: the type(class) SD2coursework.FrmHireCo has two members called the same(lstFleet_SelectedIndexChanged), your first step should be, check if 'lstFleet_SelectedIndexChanged' is defined twice. And it is(line 64 and line 92).
I see that in the first case its a Car and second case its a Truck. You should make a check on type and use maybe a switch?
-Bryan
Thanks Bryan will work on that.
Michael, a little help, here is where Vehicle can come in: - Vehicle myVehicle = myFleet.fleet.ElementAt(index);
-
-
switch(typeof(myVehicle).ToString().ToLower())
-
{
-
case "car":
-
FrmCar carGui = new FrmCar();
-
carGui.car = myCar;
-
carGui.Show();
-
break;
-
case "truck":
-
FrmTruck truckGui = new FrmTruck();
-
truckGui.truck = myTruck;
-
truckGui.Show();
-
break;
-
}
Thanks Bryan it works now : )
On to the next one,
Cheers,
Mike
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dave Theese |
last post by:
Hello all,
The example below demonstrates proper conformance to the C++ standard.
However, I'm having a hard time getting my brain around which language rules
make this proper...
The error...
|
by: Bryan Parkoff |
last post by:
I find an interesting issue that one base class has only one copy for
each derived class. It looks like that one base class will be copied into
three base classes while derived class from base...
|
by: dumboo |
last post by:
hi there
i m having a base class
class base
{
...
};
and two derived class, which have inherited 'base' but the inheritance is
not virtual
|
by: Walt Karas |
last post by:
The following gives an error in the declaration of the
member function x() of the class template Tpl, compiliing
with a recent version of GCC under Solaris:
class A { };
class B { };
...
|
by: hazz |
last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain.
I am calling the class at the bottom, "public class D" from a client...
|
by: Doug |
last post by:
Can someone point me to a very simple example in VB.NET/ASP.NET of
inheritance from a base class? I would like to define a BasePage class
from which all of my pages can inherit.
I would like to...
|
by: hurcan solter |
last post by:
Consider the code fragment;
class A
{
public:
A(){}
A(int prm):mprm(prm){}
int mprm;
};
class B:public A
{
|
by: nyathancha |
last post by:
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is...
|
by: Ravi |
last post by:
Is this the correct way to think of "base class"?
The "base class" is a class from which other classes are derived.
The "base class" will never be derived from another class.
|
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,...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
| |