Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
hi, please help me with this one.
i have a list in one class and i want to access that list from another class to add another value to that list.


C++
namespace ReportsUIScreens
{
    public partial class Screen_2 : UserControl
    {
        public Screen_2()
        {
        `   InitializeComponent();
        }
        private void viewFilterHistory(object sender, System.Windows.RoutedEventArgs e)
        {
            FilterHistory history = new FilterHistory();
            history.Show();
        }

        private void saveFilterHistory(object sender, System.Windows.RoutedEventArgs e)
        {
            SaveFilterHistory history = new SaveFilterHistory(hist);
            history.Show();
        }
    }
}

//FilterHistory.xaml.cs
namespace ReportsUIScreens
{
    public partial class FilterHistory : ChildWindow
    {
        private List<filterHistory> hist {get;set;}
        public FilterHistory()
        {
            InitializeComponent();
            history();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            ...
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            ...
        }

        public class filterHistory
        {
            public string FilterName { get; set; }
            public DateTime Date { get; set; }
        }

        public void history()
        {
            List<filterHistory> hist = new List<filterHistory>()
            {
                new filterHistory(){FilterName="Filter1", Date=new DateTime(2010, 12, 20)},
                new filterHistory(){FilterName="Filter2", Date=new DateTime(2011, 04, 20)}
            };
            this.HistoryList.ItemsSource=hist;
        }

    }
}

//SaveFilterHistory.xaml.cs
namespace ReportsUIScreens
{
    public partial class SaveFilterHistory : ChildWindow
    {
        private List<filterHistory> histList;
        public SaveFilterHistory(List<filterHistory> histListLink)
        {
            InitializeComponent();
            histList = histListLink;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            //here's where i wanna add anew filterHistory
            histList.Add(new filterHistory(){FilterName="Filter1", Date=new DateTime(2011, 04, 30)});
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

//FilterHistory.cs
namespace ReportsUIScreens
{
    public class filterHistory
    {
        public string FilterName { get; set; }
        public DateTime Date { get; set; }
    }
}
Posted
Updated 29-Apr-11 23:48pm
v2

Hi,

you must declare the List as a property. To access the list from an other class you must give an instance from your List to the other class.


The classes where you must give an instance from your List to access it:
C#
//FilterHistory.xaml.cs
namespace ReportsUIScreens
{
    public partial class FilterHistory : ChildWindow
    {
        private List<filterHistory> histList;
        public FilterHistory(List<filterHistory> histListLink)
        {
            InitializeComponent();  
            histList = histListLink;
            this.HistoryList.ItemsSource=histList;          
        }
 
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            ...
        }
 
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            ...
        }
 
    }
}
 
//SaveFilterHistory.xaml.cs
namespace ReportsUIScreens
{
    public partial class SaveFilterHistory : ChildWindow
    {
        private List<filterHistory> histList;
        public SaveFilterHistory(List<filterHistory> histListLink)
        {
            InitializeComponent();
            histList = histListLink;
        }
 
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            //here's where i wanna add anew filterHistory
            histList.Add(new filterHistory(){FilterName="Filter1", Date=new DateTime(2011, 04, 30)});
            this.DialogResult = true;
        }
 
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}


The class with the List:
C#
namespace ReportsUIScreens
{
    public partial class Screen_2 : UserControl
    {
        private List<filterHistory> hist {get;set;}
        public Screen_2()
        {
            InitializeComponent();
            history();
        }
        private void viewFilterHistory(object sender, System.Windows.RoutedEventArgs e)
        {
            FilterHistory history = new FilterHistory(hist);
            history.Show();
        }
        private void saveFilterHistory(object sender, System.Windows.RoutedEventArgs e)
        {
            SaveFilterHistory history = new SaveFilterHistory(hist);
            history.Show();
        }
        public void history()
        {
            hist = new List<filterHistory>()
            {
                new filterHistory(){FilterName="Filter1", Date=new DateTime(2010, 12, 20)},
                new filterHistory(){FilterName="Filter2", Date=new DateTime(2011, 04, 20)}
            };
        }
    }
}



you must pull out the filterHistory class out of your partial class and paste it simply in the namespace or in a separate .cs file
C#
public class filterHistory
{
   public string FilterName { get; set; }
   public DateTime Date { get; set; }
}



Hope it helps
 
Share this answer
 
v7
Comments
Member 7838027 30-Apr-11 4:37am    
thanks for the help... but an error occured at SaveFilterHistory class at these lines
private List<filterhistory> histList; and
public SaveFilterHistory(List<filterhistory> histListLink)
it says:

the type or namespace name filterHistory could not be found(are you missing a using directive or an assembly reference?)
DrNimnull 30-Apr-11 4:46am    
Of course, you must write the h as capital letter (filterhistory -> filterHistory) and move the declaration of the List into the class. It must be slipped. sorry i´ll correct my solution.


And you shouldn´t copy the xml Tags at the end...I dont know how i remove them from my answer.
Member 7838027 30-Apr-11 5:12am    
the constructor call where you instanced the SaveFilterHistory class is at another .cs file... so it would not be able to identify hist if i do this:
SaveFilterHistory history = new SaveFilterHistory(hist);
now the error says: the name hist doesn't exist in the current context

i need your help... please
DrNimnull 30-Apr-11 5:22am    
Then please post your whole code. However, it is the same principle. You must ensure that the class with the list or only list is known in the other class.
Member 7838027 30-Apr-11 5:49am    
i already updated my question and showed my whole code...please help me... thank you soooo much :)
Hi,,

Declare a property with type "List"


for eg:
public List<filterhistory> List
{
get;set;
}


u can assign values into this property and access this list property from another class...
 
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