从另一个类访问一个类的列表 [英] access list of one class from another class

查看:76
本文介绍了从另一个类访问一个类的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的silverlight页面类中有一个成员列表,我在已完成的事件中填入了数据库。现在我创建了另一个类,并希望从那里访问此列表。我试过把它变成一个属性,但它返回null。

任何人都可以帮忙。



谢谢。

 命名空间 Page 
A类
{
public 列出_lst< string> ;;

private Populate()
{
_lst = GetDataFromDB();
}
}

B类:IValueConverter
{
// 现在我想访问_lst的数据,无法从这里进行数据库调用。
}

解决方案

从您的代码示例中,很明显您可以通过 A 的实例使用此列表,因为您将其作为实例字段这个课程是公开的。在这方面,问题解决了。



两件事情都不好:1)让一个场非私人是不好的;你应该更好地使用这个属性; 2)根据需要提供更多访问权限并不好,因此,如果您在同一程序集的不同类型中使用某些类和成员,请使用 internal 而不是 public



-SA


您需要将列表设为 DependencyProperty [ ^ ]最有可能。我只知道WPF,而不是Silverlight。但我可以告诉你 ObservableCollection< t>< / t> [ ^ ]应该适用于对象。对于 ObservableCollection< string>< / string> ,它更难,因为字符串对象没有实现INotifyPropertyChanged。你需要一个围绕字符串对象的包装器。



  public  < span class =code-keyword> class  MyClass 
{
public static DependencyProperty MyListProperty = DependencyProperty.register( MyList typeof (ObservableCollection< object>, typeof (Myclass));

public ObservableCollection< object> MyList
{
get { return (ObservableCollection< object>) this .GetValue(MyListProperty);}
set { this .SetValue(MyListProperty, value );}
}
} < / object > < / object >


Hi,

I have a member list in my silverlight page class, I get it populated in the from the database in the completed event. Now I have created another class and wants to access this list from there. I have tried making it a property but it returns null.
Can anyone please help.

Thanks.

namespace Page
     Class A
     {
         public List _lst<string>;

         private Populate()
         {
            _lst = GetDataFromDB();
         }
     }

     Class B:IValueConverter
     {
          //Now here I want to access the data of _lst, cannot make a db call from here.
     }

解决方案

From your code sample, it is apparent that you can use this list through the instance of A, because you made it an instance field of this class and it is public. In this respect, the problem solved.

Both things are no good: 1) it''s bad to make a field non-private; you should better use the property; 2) it''s not good to provide more access then requires, so, if you use some class and member in a different type of the same assembly, use internal instead of public.

—SA


You need to make your list a DependencyProperty[^] most likely. I have only knowledge of WPF, not Silverlight. But I can tell you that an ObservableCollection<t></t>[^] should work for objects. For ObservableCollection<string></string>, it''s more difficult because string objects doesn''t implement INotifyPropertyChanged. You would need a wrapper around the string object.

public class MyClass
{
  public static DependencyProperty MyListProperty = DependencyProperty.register("MyList", typeof(ObservableCollection<object>, typeof(Myclass));

  public ObservableCollection<object> MyList
  {
    get { return (ObservableCollection<object>)this.GetValue(MyListProperty); }
    set { this.SetValue(MyListProperty, value); }
  }
}</object></object>


这篇关于从另一个类访问一个类的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆