I have a basic class like this:
public class task
{
public string tName;
// Construct
public job(string t)
{
tName = t;
}
}
and currently I can do this:
public class TASKCLASS
{
private task myTaskName1 = new task("name of task 1");
public Dictionary<string, task> tasks = new Dictionary<string, task>();
// Construct
public TASKCLASS()
{
tasks.Add(myTaskName1.tName, myTaskName1);
}
}
Great so that works! But I have a lot of tasks to add to the dictionary - now I know I can't add my tasks directly in the .Add section like so:
public class TASKCLASS
{
public Dictionary<string, task> tasks = new Dictionary<string, task>();
// Construct
public TASKCLASS()
{
tasks.Add("name of task 1", myTaskName1 = new task("name of task 1"));
}
}
Because the object myTaskName1 doesn't exist even though I'm trying to create it in the .Add line
But I'm wondering if there is any way I can structure my task class so that I can both create a new one with the tName and have it added to my dictionary in the same line to prevent DRY errors somewhere down the line?
IDEA: Can I create a new method for my jobs dictionary so that the .Add function will simply take the tName and the name of the task object?
EDIT: I think I might just use task as a base class and derive a bunch of classes called the handy names I want. I can probably cycle through a list of objects and look at their properties fairly easily while also being able to call each instance by their name at any time.
tasks.Add("name of task 1", new task("name of task 1"));Addmethod bynewing it up. Otherwise, it's not very clear what you're asking, what you're trying to accomplish and why.taskclass exposes itstNameas a publicly mutable field. That means that whatever value it had when it was added to the dictionary isn't necessarily the name it currently has.