I am going to be creating date and string variables that I will need to be able to sort by the times. This collection of information will need to have entries added to and removed as the script runs. Based on my research, it seems that a viable method to accomplish would be to create a hashtable that uses the key to create an epoch value of time with an associated custom class that has the properties for the DateTime and String information. This would allow me to sort the hash by the key and I can add and remove items from the hashtable as necessary. The custom class that I created is the Executor below.
public class Executor {
public DateTime StartTime { get; set; }
public string Name { get; set; }
public string Executable { get; set; }
public Executor(DateTime starttime, string name, string executable) {
StartTime = starttime;
Name = name;
Executable = executable;
}
}
With this class I can create a loop to create and add instances to the hashtable for the various allowed by a while loop. The code below is creating the information that needs to be stored in hashtable and accepts a list which provides the input of the rules to generate the hash. Some of the code that defines the times and loops through the list are removed from the example below for simplicity.
static void CreateScriptScehdule(List<Script> scriptList) {
// Declare and initialize variables
Hashtable htExecutionList = new Hashtable();
// Code to create a list of the times and manage the item properties
...
...
// Create a loop to define the exeuction times of the script between the start and stop time.
// use a tempTime to compare to the stop time
while (DateTime.Compare(tempTime,timeStop) < 0) {
// Create an object for the executable based on the script rules
Executor exe = new Executor(tempTime,item.Name,item.Executable);
// Add the executable object to the hashtable htExecutionList
double exeTimeEpoch = ttoe(tempTime);
exeKey = exeTimeEpoch.ToString() + item.Name;
htExecutionList.Add(exeKey,exe);
}
// Loop through the hashtable to print out the stored information to verify the creation for debugging
foreach (DictionaryEntry s in htExecutionList) {
//Console.WriteLine(s.Value);
}
}
When I check the value of s.Value, I receive the value ReceiveConfigFile.Executor. This makes me believe that the object Executor is be is being stored from the ReceiveConfigFile script, but when I try to retrieve a properties such as the StartTime or Name I receive an error. I have been trying to print the value as s.Value.Name thinking that I can get the object property. The error that I am receiving is:
error CS1061: 'System.Collection.DictionaryEntry' does not contain a definition for 'Namel accepting a first argument of type 'System.Collections.DictionaryEntry' could be found (are you missing a using directive or an assembly reference)
I was able to accomplish my goal for this through an object that uses a string for the key and a custom class for the value. One item that caused me some trouble to get started was that i needed to load System.Collections.Generic.
using System.Collections.Generic;
First I started off by initializing the dictionary as a string key with the Executor class as the value.
Dictionary<string, Executor> ExecutionDict = new Dictionary<string, Executor>();
Next I needed to define the key as an value that was unique so I created a variable that was associated with a specific time and description to know that it was never duplicated even if the times were associated. The exe variable stored an instance of the Executor class with variables that were defined earlier in the code. The last line added the entry to the dictionary. I had this code in a loop so I was able to continue to add the same variables with new values each time.
While (condition) {
Executor exe = new Executor(tempTime,item.Name,item.Executable);
exeKey = exeTimeEpoch.ToString() + item.Name;
ExecutionDict.Add(exeKey,exe);
}
Finally, I created a loop through the dictionary to find all of the instances and printed out the value to test the creation of the variable.
foreach(KeyValuePair<string,Executor> temp in ExecutionDict) {
Console.WriteLine("For the Key {0} the Name is {1} and the Name is {2} and the Name is {3}", temp.Key, temp.Value.Name, temp.Value.Executable, temp.Value.StartTime);
}