dimanche 8 novembre 2020

Trying to use static block to initialize values in a dictionary in C#

I am trying to load values into a dictionary that hold a count value then a value from a constructor. This dictionary is used to hold the tasks that will be assigned randomly.

In my class that holds each robot's task, I would also like to hold a dictionary that holds each BotTask (the constructor in the class) along with a count.

Here is my BotTask class:

public BotTask(string description, int duration, int taskType)
    {
        Description = description;
        Duration = duration;
        TaskType = taskType;
    }

    public static Dictionary<int, BotTask> getBotTaskMap()
    {
        Dictionary<int, BotTask> botTaskMap = new Dictionary<int, BotTask>();

        int count = 0;
        foreach (BotTask value in botTaskMap)
        {
            botTaskMap.Add(count, BotTask);
            count++;
        }

        return botTaskMap;
    }

Then I am trying to use it to store a randomized BotTask into the dictionary. Here is how I am trying to call and randomize it:

public void assignBotTask()
{
    List<int> completeBotTask = new List<int>();
    HashSet<int> usedTask = new HashSet<int>();
    Random rand = new Random();
    //find random numbers then assign them
    for(int i = 0; i < 5; i++)
    {
        int taskNum = rand.Next(16);
        while(usedTask.Contains(taskNum))
        {
            taskNum = rand.Next(16);
        }
        usedTask.Add(taskNum);
        completeTask(i, taskNum, BotTask.BotTaskMap.get(taskNum));
    }
}

I can't understand why I can't add values into botTaskMap using botTaskMap.Add(count, BotTask);

Also an error on the foreach loop saying Cannot Convert Type.

Appreciate any help.




Aucun commentaire:

Enregistrer un commentaire