Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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.
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 8-Feb-13 0:28am
Edited 10-Feb-13 20:30pm
v3
Comments
Sergey Alexandrovich Kryukov at 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 at 11-Feb-13 1:34am
   
Sorry for late reply as it was weekend. I have updated it now
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Zafar248 at 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 at 11-Feb-13 3:00am
   
No data is hidden. What do you mean?
—SA
Zafar248 at 11-Feb-13 4:34am
   
It gives me null.
Sergey Alexandrovich Kryukov at 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 at 11-Feb-13 5:18am
   
In Class B I use this Code
 
A objA = new A();
List lst = new List;
lst = A._lst;
 
here objA._lst does not have any data.
Sergey Alexandrovich Kryukov at 11-Feb-13 11:45am
   
This code is gibberish!
 
You create a new list, instantiate.
Then you assign its to A._lst, which is null because you never instantiated it. Old value is discarded...
What do you want then?!
 
You have no clue about programming, at the moment. It's to early for you to ask questions here. You are just doing try-end-error, without understanding what you are doing.
But you should understand every single letter your write.
 
So, grab a book/manual and try to start from the very beginning. Learn what is a type and instance (object). Static and instance members. Initialization, construction, assignment, expressions... from the very beginning...
 
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^] should work for objects. For ObservableCollection, 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>
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.150326.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100