views:

389

answers:

5

I have the following in a C# class:

public static readonly SortedDictionary<string, string> Fields =
    new SortedDictionary<string, string>
        { ... }

I was hoping there was a way to get Intellisense to provide prompts for all the keys defined in Fields. It supplies lists of the methods and properties in the class, including the Fields property, but when I go to access Fields[ or Fields[", it says nothing other than that I need to supply a string key. Is there a way to have it tell me a list of the string keys since this is a static class property that is not at all dynamic or changed after compilation?

+9  A: 

If the keys are static wouldn't you be better off using an enumeration as your key instead of a string?

With an enumeration your compiler can tell you what the options are, but you can't do that with strings.

Welbog
+4  A: 

Do this instead:

public enum MyKeys
{
   Key1,
   Key2,
   Key3
}

public static readonly SortedDictionary<MyKeys, string> Fields =
    new SortedDictionary<MyKeys, string>
        { ... }

This will cause intellisense to pick up the enum type so you'll get the desired effect.

Spencer Ruport
+1  A: 

The best way for you to do this is probably to create public static readonly fields or properties.

If you need dictionary-like behavior, you should make the keys an enum as other people have suggested.

Remember that all of the dictionary class are mutable, which means that other code can add or remove items from your dictionary. The only way to prevent this would be to inherit a ReadOnlyCollection around a KeyedCollection and expose the indexer.

SLaks
Yes, it would. IntelliSense automatically completes enum keys
SLaks
@SLaks: I guess I misread your answer.
Welbog
A: 

I have used static properties to do this.

What I have done for a Settings class of mine in order to get Intellisense was to create a code snippet that creates a static property. The code snippet is set up so that I enter the name of the setting and that same name is used in both the Property name and the lookup name.

Like this:

 <Code Language="CSharp" Kind="method decl">
    <![CDATA[     public static string $name$
    {
        get { return GetSetting("$name$", $default$); }
        set { SaveSetting("$name$", value); }
    }
    ]]>
 </Code>

The GetSetting method would do something like this:

 private static string GetSetting(string name)
 {
      if (!_Fields.ContainsKey(name))
          _Fields.Add(name, default);
      return _Fields[name];
 }

This makes it really easy to add static properties and still get Intellisense.

Chris Thompson
+3  A: 

It seems to me that you assume that readonly there means that the dictionary won't ever change. This isn't the case - it only means that the value of the field (which is a reference to the dictionary) won't change (and even then it can still change while inside constructor, and can be null before it executes). The dictionary itself is quite mutable.

Pavel Minaev