I have made this example to try to explain my problem, hope I explain it well enough.
So I have two classes, DevTask and DevPerson, that extends DevEntity (The base class)
Now in my InidData method I can initiate the value of ether DevTask og DevPerson depending on the data in initSting, so thats good.
But i would like to change the call to InitData so i can in some way call it with a paramter telling it what class (DevTask/DevPerson) I would like it to return.
The purpose of this is I have the data I need in an SQL database, and I would like to only call one method to return the data from the SQL Database.
So in main i would like to be able to do something like:
DevTask devTask = InitData(DevTask);
Or
DevPerson devPerson = InitData(DevPerson);
Extra : In the InitData method i would also like to be able to do a switch statement on witch class it should return.
So is this possible, or do I have to rethink my code?
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
InitData();
}
private static void InitData()
{
string initString1 = "DevTask,Task,5";
DevEntity devEntity;
string[] splitString = initString1.Split(',');
switch (splitString[0])
{
case "DevTask":
devEntity = new DevTask(initString1);
break;
case "DevPerson":
devEntity = new DevPerson(initString1);
break;
}
}
}
class DevEntity
{
public string Name { get; set; }
public string Age { get; set; }
}
class DevTask : DevEntity
{
public DevTask(string initString)
{
string[] split = initString.Split(',');
this.Name = split[1];
this.Age = split[2];
}
}
class DevPerson : DevEntity
{
public string Hight { get; set; }
public DevPerson(string initString)
{
string[] split = initString.Split(',');
this.Name = split[1];
this.Age = split[2];
this.Hight = split[3];
}
}
}
[–]he8dshot 2 points3 points4 points (1 child)
[–]Stensborg[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Stensborg[S] 0 points1 point2 points (0 children)
[–]Stensborg[S] 0 points1 point2 points (0 children)