0

I have a dictionary

public static IDictionary<string, IList<string>> checksCollection = 
           new Dictionary<string, IList<string>>();

I add to the dictionary as follows:

public static void addCheck(string checkName, string hostName, 
          string port, string pollInterval, string alertEmail, 
          string alertSMS, string alertURI)
{
    checksCollection.Add(checkName, new[] { checkName, hostName, port, 
               pollInterval, alertEmail, alertSMS, alertURI });
}

How would it be possible to change the alertURI list value?

4

2 に答える 2

1

Quickest way, by getting the IList<string> from the dictionary and accessing its seventh element:

checksCollection[checkName][6] = "new value";

But if I were you, I'd make all those values in the string array its own class so you don't have to hardcode the index values, just in case you add or remove additional properties in the future. Create a class definition like so:

public class YourClass
{
    public string CheckName { get; set; }
    public string HostName { get; set; }
    public string Port { get; set; }
    public string PollInterval { get; set; }
    public string AlertEmail { get; set; }
    public string AlertSMS { get; set; }
    public string AlertURI { get; set; }
}

And change your dictionary definition:

public static IDictionary<string, YourClass> checksCollection = 
    new Dictionary<string, YourClass>();

And then to add to it (although preferably you would create a constructor on YourClass that takes the parameters):

public static void addCheck(string checkName, string hostName, string port, string pollInterval, string alertEmail, string alertSMS, string alertURI)
{
    checksCollection.Add(checkName, new YourClass() { 
        CheckName = checkName,
        HostName = hostName,
        Port = port,
        PollInterval = pollInterval,
        AlertEmail = alertEmail,
        AlertSMS = alertSMS,
        AlertURI = alertURI
    });
}

And then modifying becomes straightforward with no guesswork of array indices:

checksCollection[checkName].AlertURI = "new value";
于 2012-06-16T14:41:47.870 に答える
0

one way would be to do

checksCollection["somekey"][6] = "new value for alertURI"

I'd recommend creating a small object that represents those 7 values, something like

class Foo {
    public string HostName { get; set; }
    public string Port { get; set; }
    public string PollInterval { get; set; }
    public string AlertEmail { get; set; }
    public string AlertSMS { get; set; } 
    public string AlertURI { get; set; }
}

Then you could change it by

checksCollection["key"].AlertURI = "something else";
于 2012-06-16T14:41:37.960 に答える