Exercise: Constructors Static and ReadOnly
Constructors
- Add a constructor to the Car class.
- Initialize the value of all the variables.
- In the TestClass add one more car instance.
- Call the CarInfo method of the new car.
- Build and Test.
Your Car class supposed to look like this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Northwind.Exercises.MoreOnClass
{
class Car
{
public string carName;
public int carYear;
public int CarKM;
public string carType;
public Car()
{
carName = "No name given";
carYear = 0;
CarKM = 0;
carType = "No type given";
}
public void CarInfo()
{
MessageBox.Show("My " + this.carType + " car is " + this.carYear + " " + this.carName + "\n" +
" I drove it for " + this.CarKM + " KM");
}
}
}
Your TestClass class supposed to look like this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Northwind.Exercises.MoreOnClass
{
class TestClass
{
public void Run()
{
Car mainCar = new Car();
Car weekendCar = new Car();
Car funCar = new Car();
mainCar.carName = "BMW X6";
mainCar.carYear = 2015;
mainCar.CarKM = 20000;
mainCar.carType = "Main";
weekendCar.carName = "Ford mustang";
weekendCar.carYear = 1965;
weekendCar.CarKM = 160000;
weekendCar.carType = "Weekend";
mainCar.CarInfo();
weekendCar.CarInfo();
funCar.CarInfo();
}
Static
- In the Car class:
- Add static variable type int name it CarCounter.
- Add variable type int name it Id.
- In the class constructor:
- Set the CarCounter to increase by one.
- In the Class constructor set the Id value to the value of the CarCounter.
- Change the CarInfo method to reflect the new Id in the message box.
- Build and Test
Your Car class supposed to look like this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Northwind.Exercises.MoreOnClass
{
class Car
{
public string carName;
public int carYear;
public int CarKM;
public string carType;
static int CarCounter=0;
public int Id;
public Car()
{
CarCounter++;
Id = CarCounter;
carName = "No name given";
carYear = 0;
CarKM = 0;
carType = "No type given";
}
public void CarInfo()
{
- MessageBox.Show("My " + this.carType + " car is " + this.carYear + " " + this.carName + "\n" +
- " I drove it for " + this.CarKM + " KM");
MessageBox.Show("My number"+ this.Id + " " + this.carType + " car is " + this.carYear + " " + this.carName + "\n" +
" I drove it for " + this.CarKM + " KM");
}
}
}
ReadOnly
- In the TestClass class Try to update the Id of one of the cars to 10.
- Build and test.
- In the car class change the Id variable to readonly.
- Notice the error message in the TestClass class.
- Delete the update of the Id from the TestClass.
- Build and test.
Your Car class supposed to look like this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Northwind.Exercises.MoreOnClass
{
class Car
{
public string carName;
public int carYear;
public int CarKM;
public string carType;
static int CarCounter=0;
public readonly int Id;
public Car()
{
CarCounter++;
Id = CarCounter;
carName = "No name given";
carYear = 0;
CarKM = 0;
carType = "No type given";
}
public void CarInfo()
{
MessageBox.Show("My number"+ this.Id + " " + this.carType + " car is " + this.carYear + " " + this.carName + "\n" +
" I drove it for " + this.CarKM + " KM");
}
}
}
Your TestClass class supposed to look like this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Northwind.Exercises.MoreOnClass
{
class TestClass
{
public void Run()
{
Car mainCar = new Car();
Car weekendCar = new Car();
Car funCar = new Car();
mainCar.carName = "BMW X6";
mainCar.carYear = 2015;
mainCar.CarKM = 20000;
mainCar.carType = "Main";
weekendCar.carName = "Ford mustang";
weekendCar.carYear = 1965;
weekendCar.CarKM = 160000;
weekendCar.carType = "Weekend";
- mainCar.Id = 10;
mainCar.CarInfo();
weekendCar.CarInfo();
funCar.CarInfo();
}
}
}
Help us improve, Edit this page on GitHub
or email us at info@fireflymigration.com