'create a specific class type from a variable and set data into the class
I have a project which I am working on currently and I am facing a problem at the moment.
What I am trying to figure out is how to load a class variable with data so I can create a specific type of a class using that variable:
private Vehicle vehicleDetails;
vehicleDetails = new Vehicle();
public Order(string ordLine, string vehLine)
{
orderDetails = new JobDetails(ordLine);
string temp = vehLine.Substring(0, 1);
if (temp == "P")
{
vehicleDetails.vehicleDetails.splitter(vehLine);
vehicleDetails = new PersonalUseCars();
}
}
An example of vehLine will be something like this - "P-LU12SAK-Ford-6m-...." In the variable temp we store the first letter which is the type of car we want to create. The vehicle class looks like this.
using System;
using System.Collections.Generic;
using System.Text;
namespace CarGarageSoftware
{
class Vehicle
{
public string type;
public string Lplate;
public string Make;
public string LMOT;
public string Esize;
public string Transmission;
public string Mileage;
public string FuleT;
public Vehicle()
{
}
public virtual string returnString()
{
string temp = type + "-" + Lplate + "-" + Make + "-" + LMOT + "-" + Esize + "-" + Transmission + "-" + Mileage + "-" + FuleT;
return temp;
}
public virtual void splitter(string x)
{
string[] strings = x.Split("-");
type = strings[0];
Lplate = strings[1];
Make = strings[2];
LMOT = strings[3];
Esize = strings[4];
Transmission = strings[5];
Mileage = strings[6];
FuleT = strings[7];
}
}
class PersonalUseCars : Vehicle
{
public override string returnString()
{
string temp = type + "-" + Lplate + "-" + Make + "-" + LMOT + "-" + Esize + "-" + Transmission + "-" + Mileage + "-" + FuleT ;
return temp;
}
}
class BusinessCars : Vehicle
{
public string CompanyN;
public string CompanyA;
public string FleetN;
public override string returnString()
{
string temp = type + "-" + Lplate + "-" + Make + "-" + LMOT + "-" + Esize + "-" + Transmission + "-" + Mileage + "-" + FuleT + "-" + CompanyN + "-" + CompanyA + "-" + FleetN;
return temp;
}
}
class WorkVehicle : Vehicle
{
public string CompanyN;
public string CompanyA;
public string purpose;
public string Type;
public override string returnString()
{
string temp = type + "-" + Lplate + "-" + Make + "-" + LMOT + "-" + Esize + "-" + Transmission + "-" + Mileage + "-" + FuleT + "-" + CompanyN+"-"+CompanyA+"-"+purpose ;
return temp;
}
}
class Motobikes : Vehicle
{
}
}
I want vehicleDetails to return the right data into the class, and when the right type of vehicle is created I would like to load the new vehicle with the right data from vehLine, so that the class contains the right values.
I have tried using different techniques but I am just a beginner so nothing comes to my mind.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
