有没有办法在c#4.5中存储临时列表值?

时间:2014-11-06 06:42:36

标签: c# asp.net wpf list

我需要存储列表值无常,我需要在另一个c#类中获取该列表值。该过程类似于Asp.net中的Session。请有人就这个想法分享一些想法。

这是我的代码:

的Class1.cs:

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

我需要将这两个列表值存储在本地的某个位置,我需要将这两个列表值转换为另一个类..尽管如此,我宁愿不去作为构造函数。我需要在本地存储这两个破坏值..

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&amp;班级以外的CurrentSelectedInventories ..

的Class1.cs:

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

任何帮助将不胜感激..

2 个答案:

答案 0 :(得分:2)

静态属性不能访问非静态字段,zoneListGlobal和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; }
    }

}

答案 1 :(得分: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;
    }

}