views:

25

answers:

1

i have collection of classes in one form and i want to use the same collection in other form. so here is the place where the collection is made

public partial class Window1 : Window {

    string text;
    string[] tmp;
    double procent;
    public ObservableCollection<element> elementi = new ObservableCollection<element>();

and the variable "elementi" is not valid in the new form. so how can i use it?

+1  A: 

It really depends on how the two forms are related.

In general, I would avoid creating an object which needs to be shared between forms in a form. Do that in a separate class.

You could have whatever class instantiates both forms pass it to the constructor for both forms, or if it's meant to be a singleton you could create a static reference to the object somewhere eg:

public class StaticRef {
    static StaticRef() {
        Elementi = new ObservableCollection<element>();
    }
    public static ObservableCollection<element> Elementi {get; set;}
}

Then in both forms you would use StaticRef.Elementi to access your shared object.

Again though, it really depends what you're trying to accomplish what the right way to do it is.

mootinator