Click here to Skip to main content
12,623,027 members (29,195 online)
Rate this:
Please Sign up or sign in to vote.

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.

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
Updated 10-Feb-13 20:30pm
Not clear what's the problem is. Can you create a comprehensive but very small code sample? Use "Improve question".
Zafar248 11-Feb-13 1:34am
Sorry for late reply as it was weekend. I have updated it now
Rate this: bad
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.

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.

No data is hidden. What do you mean?
Zafar248 11-Feb-13 4:34am
It gives me null.
In what line? Please explain the whole scenario. You are missing something really simple, but I cannot see what...
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.
Sergey Alexandrovich Kryukov 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...

Rate this: bad
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<t>[^] should work for objects. For ObservableCollection<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); }

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2016
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