Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
C#
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.
     }
Posted
Updated 10-Feb-13 19:30pm
v3
Comments
Sergey Alexandrovich Kryukov 8-Feb-13 13:28pm    
Not clear what's the problem is. Can you create a comprehensive but very small code sample? Use "Improve question".
—SA
Zafar248 11-Feb-13 1:34am    
Sorry for late reply as it was weekend. I have updated it now

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
 
Share this answer
 
Comments
Zafar248 11-Feb-13 2:55am    
I have also tired what you have suggested as making a property. I am able to access the list this way but not the data and I need the data to which I am applying the converter to be replaced by data in the list.

Thanks
Sergey Alexandrovich Kryukov 11-Feb-13 3:00am    
No data is hidden. What do you mean?
—SA
Zafar248 11-Feb-13 4:34am    
It gives me null.
Sergey Alexandrovich Kryukov 11-Feb-13 4:42am    
In what line? Please explain the whole scenario. You are missing something really simple, but I cannot see what...
—SA
Zafar248 11-Feb-13 5:18am    
In Class B I use this Code

A objA = new A();
List<string> lst = new List<string>;
lst = A._lst;

here objA._lst does not have any data.
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.

C#
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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900