【问题标题】:Is there any way to store temporary list values in c# 4.5?有没有办法在 c# 4.5 中存储临时列表值?
【发布时间】:2014-11-06 06:42:35
【问题描述】:

我需要永久存储列表值,并且我需要在另一个 c# 类中获取该列表值。程序类似于 Asp.net 中的 Session。任何人都请分享一些关于这个想法的想法。

这是我的代码:

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)..

任何帮助将不胜感激..

【问题讨论】:

  • 这是一个不好的做法。阅读en.wikipedia.org/wiki/God_object 但是您可以将其存储在静态类或属性中。在 class1 中将 zoneList 设为静态 public,然后在 class2 中像 class1.zoneList 一样访问它,而不在 class2 中创建 class1 实例。
  • 你能再看一遍我编辑的帖子吗?
  • @RajDeInno 不使用属性,而是使用返回对象的静态方法。看我的回答如何执行。

标签: c# asp.net wpf list


【解决方案1】:

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

}

【讨论】:

    【解决方案2】:

    您可以使用 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;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 2015-04-22
      • 1970-01-01
      相关资源
      最近更新 更多