2012-11-10 91 views
-1

我需要寫一個簡單的類。 我需要2種方法:類只是簡單的矢量在C#

Vector property = new Vector(); 
property.add("key", "value"); //set value of key 
property.get("key"); //return value of key 

CSHARP是否有一個這樣的類?

我試圖寫我自己的類

string[] keys; 

public void add(string key, string value) 
{ 
this.keys[key] = value; 
} 

但字符串不能爲數組的索引(但必須)。

任何想法? 謝謝。

+8

聽起來像一個'字典'。不知道你爲什麼稱這個「矢量」。 – CodesInChaos

+0

是的,但字典沒有像get那樣的方法。我需要(因爲很簡單) – Sekhmet

+1

@SeCorp當然[它](http://msdn.microsoft.com/en-us/library/9tee9ht2(v = vs.90).aspx)。沒有它會有什麼用處? – GSerg

回答

1

您可以使用Dictionary<TKey,TValue>來做到這一點。您也可以定義Vector作爲一類Dictionary,如果你想使用Vector作爲Dictionary

class Vector : Dictionary<string,string> 
{ 
    public string Get(string Key) //Create a new void Get(string Key) which returns a particular value from a specific Key in the Dictionary (Vector) 
    { 
     return this[Key]; //Return the key from the Dictionary 
    } 
    public void add(string Key, string Value) //Create a new void Add(string Key, string Value) which creates a particular value referring to a specific Key in the Dictionary (Vector) 
    { 
     this.Add(Key, Value); //Add the key and its value to Vector 
    } 
} 

Vector property = new Vector(); //Initialize a new class Vector of name property 
property.add("key", "value"); //Sets a key of name "key" and its value "value" of type stirng 
MessageBox.Show(property.Get("key")); //Returns "value" 
//MessageBox.Show(property["key"]); //Returns "value" 

這將創建一個新的類Vector它實現Dictionary,這樣你就可以能夠使用Vector作爲Dictionary

注意事項Dictionary<TKey, TValue>是一個泛型類,提供從一組鍵到一組值的映射。字典中的每個添加項都包含一個值及其關聯的鍵。使用密鑰檢索值非常快,因爲Dictionary<TKey, TValue>類是作爲散列表實現的。

謝謝,
我希望對您有所幫助:)

+0

要添加:這是不必要的。他只用「矢量」來描述他試圖完成的事情,但並不真正需要媒介。字典本身具有他需要的所有目的(存儲要用鍵訪問的內容)。 – phil13131

2

你可以很容易地使用一個字典。

Dictionary<string, string> dict = new Dictionary<string, string>(); 
dict.Add("Key", "Value"); 

//access using: 
dict["Key"]; 

編輯: 如果你願意,你也可以用字典爲其他對象不僅字符串。如果你的「價值」實際上是數字,你也可以去:

var dict = new Dictionary<string, double>(); 

,這可能會節省你一些轉換回數字。

+0

是的,謝謝,我不知道我不能通過這種方式獲得acces! – Sekhmet

+0

在許多方面,如果你不喜歡,字典會失去它的全部目的! – phil13131

3
Vector property = new Vector(); --> var property = new Dictionary<string, string>(); 
property.add("key", "value"); --> property.Add("key", "value"); 
property.get("key")    --> property["key"] 

異常處理: 如果該鍵不字典中的最後一個可能拋出異常。它不會拋出的另一種方法是:

string value; 
bool keyFound = property.TryGetValue("key", out value); 

術語:,你有什麼想法通常被稱爲字典地圖;術語矢量,與標量相反,通常保留用於簡單數組或值列表。


P.S:您可以創建自己的類(見下文)—但你爲什麼拒絕Dictionary<TKey,TValue>僅僅是因爲相關的方法沒有被命名addget是超越我。

class PropertyMap 
{ 
    private Dictionary<string, string> map = new Dictionary<string, string>(); 

    public string add(string key, string value) { map.Add(key, value); } 
    public string @get(string key) { return map[key]; } 

    public string this[string key] // <-- indexer allows you to access by string 
    { 
     get 
     { 
      return @get(key); 
     } 
     set 
     { 
      add(key, value); 
     } 
    } 
} 
+0

是的,謝謝,我不知道我不能通過這種方式獲得acces。 PS。向量(是僞代碼,我想向你展示我的內容)。 – Sekhmet

2

使用此代碼...

private Dictionary<string, string> keys = new Dictionary<string, string>(); 

public void add(string key, string value) 
{ 
    this.keys.Add(key, value); 
} 

public string get(string key) 
{ 
    return this.keys[key]; 
}