2016-10-04 1 views
1

まず、ハッシュテーブルとその値を宣言します。ハッシュテーブルエントリのキーはGUIDであり、値は文字列値が少数のオブジェクトです。キーでハッシュテーブルobjを取得し、公開プロパティを変更します。

Guid g = Guid.NewGuid(); 
    Hashtable hash = new Hashtable(); 
    InstallationFiles instFiles = new InstallationFiles(string1, string2, string3); 
    hash.Add(g, instFiles); 
    //...add many other values with different GUIDs... 

私の目標は、ユーザーに文字列1、文字列2、文字列3を編集できるようにすることです。長い話を短くカットするために、私は私が編集する必要がエントリの「GUID gを」得ることができる位置にいます:

public void edit() 
    { 
     //here I retrieve the GUID g of the item which has to be edited: 
     object objectHash = item.Tag; 
     //here i loop through all hash entries to find the editable one: 
     foreach(DictionaryEntry de in hash) 
     { 
      if(de.Key.ToString() == objectHash) 
      { 
      //here I would like to access the selected entry and change string1 - 
      //the line below is not working. 

      hash[de.Key].string1 = "my new value"; 
      } 
     } 

    } 

は、どのように私はこのラインを動作させるのですか?

hash[de.Key].string1 = "my new value"; 
+2

'' Dictionary ''を使用して、変数を強く型付けし、そのプロパティにアクセスします。 – usercr

答えて

1

使用Dictionary<Guid, InstallationFiles>代わりHashTable

UPD。あなたはこれを使うことができます。

(hash[de.Key] as InstallationFiles).string1 = "asdasd" 

[OK]を、説明:

ハッシュテーブルは、一般的なタイプではないので、それはオブジェクトとしてキーと値の参照が含まれています。

hashtable[mykey]の値にアクセスすると、Objectが参照されます。あなたのタイプ(InstallationFiles)を参照するには、 "Objectへの参照"から "InstallationFilesへの参照"を取得する必要があります。私のサンプル私はこれを行うには "as"演算子を使用します。

+0

私の上司は私にHashtableを使用したいと考えています。私はHashtableで動作させることはできません。 – Dovile

+0

したがって、 '' '(InstallationFilesとしてのhash [de.Key]).string1 =" asdasd "' '' – tym32167

+0

ありがとうございました。できます。私は次回の辞書を使用しようとします。 – Dovile

関連する問題