2014-11-06 110 views
3

我需要存儲列表值非常規,我需要在另一個c#類中獲取該列表值。程序類似於Asp.net中的Session。任何人請分享關於這個想法的一些想法。有沒有辦法在c#4.5中存儲臨時列表值?

這裏是我的代碼:

的Class1.cs:

ObservableCollection<SampleEntityList> zoneList = new ObservableCollection<SampleEntityList>(); 
    ObservableCollection<SampleEntityList> ztowList = new ObservableCollection<SampleEntityList>(); 

我需要存儲這2列出本地重視一些地方,我需要得到這兩個列表值到另一個類..儘管如此,我寧願不去作爲構造函數。我需要在本地保存這兩破敗值..

class2.cs:

我已盡力了此代碼:

設置了新的靜態類來設置屬性。更重要的是,我不能給類以外的屬性訪問..

這裏是我的代碼:

static class GlobalTempStorageVariable 
{ 

    ObservableCollection<SampleEntityList> zoneListGlobal 
            = new ObservableCollection<SampleEntityList>(); 
    ObservableCollection<SampleEntityList> ztowListGlobal 
            = new ObservableCollection<SampleEntityList>(); 


    public static ObservableCollection<SampleEntityList> CurrentListOfInventories 
    { 
     get { return zoneListGlobal; } 
     set { zoneListGlobal = value; } 
    } 

    public static ObservableCollection<SampleEntityList> CurrentSelectedInventories 
    { 
     get { return ztwoListGlobal; } 
     set { ztwoListGlobal= value; } 
    } 

    } 

但這種代碼是行不通的。另外我無法訪問CurrentListOfInventories類的外部& CurrentSelectedInventories ..

的Class1.cs:

GlobalTempStorageVariable. ???(its not showing the property).. 

任何幫助,將不勝感激..

+0

這是一個糟糕的做法。閱讀http://en.wikipedia.org/wiki/God_object然而,你可以將它存儲在靜態類或屬性中。使zoneList靜態在class1中公開,然後像class2中的class1.zoneList那樣訪問它,而無需在class2中創建class1實例。 – 2014-11-06 06:46:51

+0

你能否再次閱讀我編輯過的帖子? – 2014-11-06 09:12:59

+0

@RajDeInno不使用屬性,而是返回對象的靜態方法。看看我的答案如何執行它。 – 2014-11-06 15:19:14

回答

2

Static屬性無法訪問非靜態字段zoneList Global和ztowListGlobal也應該是靜態的,以便可以通過它們的屬性訪問:

static class GlobalTempStorageVariable 
{ 
    static ObservableCollection<SampleEntityList> zoneListGlobal 
            = new ObservableCollection<SampleEntityList>(); 

    static ObservableCollection<SampleEntityList> ztowListGlobal 
            = new ObservableCollection<SampleEntityList>(); 

    public static ObservableCollection<SampleEntityList> CurrentListOfInventories 
    { 
     get { return zoneListGlobal; } 
     set { zoneListGlobal = value; } 
    } 

    public static ObservableCollection<SampleEntityList> CurrentSelectedInventories 
    { 
     get { return ztowListGlobal; } 
     set { ztowListGlobal = value; } 
    } 

} 
0

你可以用2種靜態方法建立自己的靜態類返回您想要的對象,如下例所示:

public static class GlobalTempStorageVariable 
{ 

    ObservableCollection<SampleEntityList> zoneListGlobal 
            = new ObservableCollection<SampleEntityList>(); 
    ObservableCollection<SampleEntityList> ztowListGlobal 
            = new ObservableCollection<SampleEntityList>(); 


    public static ObservableCollection<SampleEntityList> GetCurrentListOfInventories() 
    { 
     return zoneListGlobal; 
    } 

    public static ObservableCollection<SampleEntityList> GetCurrentSelectedInventories() 
    { 
     return ztowListGlobal; 
    } 

} 
相關問題