2017-08-31 16 views
-1

ReadOnlyCollectionのカバレッジに問題があります。ReadOnlyCollectionのインスタンス化

Iが一旦Iは

ToponymeGeoDb.roListeToponymesGeoDb =new ReadOnlyCollection<ToponymeGeoDb>(ToponymeGeoDb.ListeToponymesGeoDb); 
との間でデータを転送移入

public static List<ToponymeGeoDb> ListeToponymesGeoDb = new List<ToponymeGeoDb>(); 

public static ReadOnlyCollection<ToponymeGeoDb> roListeToponymesGeoDb = new ReadOnlyCollection<ToponymeGeoDb>(ListeToponymesGeoDb); 

でソートしReadOnlyCollection でコピーし、2つのコレクション、Accessデータベースが移入されるものを使用してい

私のroListeToponymesGeoDbには私のデータが含まれていますが、私のプログラムの別の部分でそれを使用しようとすると、空です!

静的メンバーとして宣言されているため、何が起こっているのか分かりません。

+0

「データを転送する」必要はありません。 ListeToponymesGeoDbに対する変更は自動的にReadOnlyCollectionに反映されます。 https://msdn.microsoft.com/en-us/library/ms132474(v=vs.110).aspx – WithMetta

+0

roListeToponymesGeoDbが空の場合、ListeToponymesGeoDbは空です。 ListeToponymesGeoDbが正しく挿入されていることを確認してください。 – WithMetta

+0

getterプロパティの 'IReadOnlyCollection 'にキャストするだけです。 – ja72

答えて

0

アイテムの非公開リストを保持し、代わりにIReadOnlyCollectionのプロパティを公開します。

public struct Topo { } 

public class Foo 
{ 
    // Private list of types. This is actual storage of the data. 
    // It is inialized to a new empty list by the constructor. 
    private List<Topo> InnerItems { get; } = new List<Topo>(); 

    // Example on how to modify the list only through this class 
    // Methods have access to `InnerList` 
    public void Add(Topo item) { InnerItems.Add(item); } 

    // Outside of the class only `Items` is exposed 
    // This poperty casts the list as a readonly collection 
    public IReadOnlyCollection<Topo> Items => InnerItems; 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var foo = new Foo(); 

     foo.Add(new Topo()); 

     // foo.Items.Add() doesnt exist. 

     foreach(var item in foo.Items) 
     { 
      Console.WriteLine(item); 
     } 
    } 
} 

また、次の代わりに使用することができます。これはあなたがまた、インデックスによって結果にアクセスすることができます

public IReadOnlyList<Topo> Items => InnerItems; 

。最初の項目はItems[0]のようになります。

関連する問題