3

の実例がありDictionary<string, int>ます。を a に設定しDictionaryprivate Dictionary値を確認する必要があります。

現在私は持っています:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    Dictionary<string, int> docTypeValue = new Dictionary<string, int>();
    docTypeValue.Add("OSD", 1);
    docTypeValue.Add("POD", 2);
    docTypeValue.Add("REC", 3);
    docTypeValue.Add("CLAINF", 4);
    docTypeValue.Add("RATE", 5);
    docTypeValue.Add("OTHER", 6);
    docTypeValue.Add("CARINV", 7);
    docTypeValue.Add("PODDET", 8);
    docTypeValue.Add("BOLPO", 9);

    litText.Text = docTypeValue[txtDocType.Text].ToString();
}

これは期待どおりに機能します。プロパティを使用する必要がありますか? すなわち下

private Dictionary<string, int> DocTypeValue
{
    get;
    set; 
}

上記のものをリファクタリングして、提案されたものを作成するにはどうすればよいprivate Dictionaryですか?

4

5 に答える 5

7

あなたはこのようなものを探しています。Collection Initializer機能を利用します。

private Dictionary<string, int> docTypeValue = new Dictionary<string, int> 
{ 
    { "OSD", 1 },
    {"POD", 2},
    {"REC", 3},
    {"CLAINF", 4},
    //...
};
于 2013-10-08T15:31:43.190 に答える
2

非メンバーが辞書の内容を変更できるようにしたくないが、利用可能にしたい場合は、次のようにすることができます。

private Dictionary<String, Int32> dictionary = ...
public IEnumerable<Int32> Dictionary { get{ return dictionary.Values; } }

// Other methods in the class can still access the 'dictionary' (lowercase).
// But external users can only see 'Dictionary' (uppercase).
void AddItemToDictoinary(String key, Int32 value) {
    dictionary.Add(key, value);  // dictionary is accessible within the class.
}

または、次のようなインデクサーを使用します。

private Dictionary<String, Int32> dictionary = ...
public Int32 this[String key] { get { return dictionary[key]; } }

// Same as above - within the class you can still add items to the dictionary.
void AddItemToDictoinary(String key, Int32 value) {
    dictionary.Add(key, value);
}

インデクサーを使用すると、BST ビハインドDictionary<T, U>を利用できます (シーケンシャル検索を使用するのではなく)。したがって、辞書が次のように定義されている場合:

class SneakyDictionary { 
    private Dictionary<String, Int32> dictionary = ...
    public Int32 this[String key] { get { return dictionary[key]; } }

    // Same as above - within the class you can still add items to the dictionary.
    void AddItemToDictoinary(String key, Int32 value) {
        dictionary.Add(key, value);
    }
}

次のように使用します。

public static void Main() {
    SneakyDictionary dictionary = ...
    dictionary.AddItemToDictionary("one", 1);
    dictionary.AddItemToDictionary("two", 2);
    dictionary.AddItemToDictionary("three", 3);

    // Access items in dictionary using indexer:
    Console.WriteLine(dictionary["one"]);
}
于 2013-10-08T15:26:55.907 に答える
1

それは範囲の問題です。ディクショナリがクラス全体に役立つ場合は、初期化子を使用してプライベートな静的 (またはそうでない) 読み取り専用フィールドとしてインスタンス化できます。

private static readonly Dictionary<string, int> docTypeValue = new Dictionary<string, int> 
{ 
    { "OSD", 1 },
    {"POD", 2},
    {"REC", 3},
    {"CLAINF", 4},
    // and so on
};

ただし、静的コンストラクターと呼ばれる .Net 機能に依存することもできます。

private static Dictionary<string, int> docTypeValue;

static YOURCLASSNAME()
{
    docTypeValue = new Dictionary<string, int>();
    docTypeValue.Add("OSD", 1);
   // and so on
}

またはこれらの組み合わせ。

どちらの場合も、現在のアプローチに対して、辞書は一度初期化されます。

于 2013-10-08T15:39:00.797 に答える
0

プロパティを必要としないプライベート メンバーの場合は、- を使用してください。

private Dictionary<string, int> _docTypeValue;
于 2013-10-08T15:24:46.637 に答える
0

あなたの要件についての私の理解によると、「辞書をプライベート辞書に設定し、値を確認する必要があります。」このディクショナリはクラス レベルで必要です。ディクショナリのプロパティを作成する必要はありません。プライベート ディクショナリとして作成するだけです。

上記の回答と同様です。

于 2013-10-08T15:25:12.403 に答える