简体   繁体   中英

C# Empty object property List from another class

So, I have problem with objects in list. The list is in Student class, named ListaWybranychPrzedmiotow , I initialize and fill it with objects in DeansOffice class.

When i try to show them in MessageBoxes in DeansOffice class, its normally show it, but when i try to show same MessageBoxes from MainWindow class, the same List is empty, and no objects were shown in MessageBoxes.

What am I doing wrong?

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        GetDataFromDb();
        StudentsDataGrid.ItemsSource = Student._ListaStudentow;
    }

    private void AddButton_Click(object sender, RoutedEventArgs e)
    {
        var wnd = new DeansOffice();
        wnd.ShowDialog();

        if (wnd.NewStudent != null)
        {
            var NewStudent = wnd.NewStudent;
            foreach (Subject s in NewStudent.ListaWybranychPrzedmiotow)
                MessageBox.Show(s.Name);   //HERE they don't appear
        }
    }
}
public partial class DeansOffice : Window
{
    public Student NewStudent { get; set; }
    public List StudentSubjects = new List();

    public DeansOffice()
    {
        InitializeComponent();
        StudiaComboBox.ItemsSource = Studies._StudiesList;
        PrzedmiotyListBox.ItemsSource = Subject._SubjectList;
    }

    private void AddButton_Click(object sender, RoutedEventArgs e)
    {
        var tmpNazwisko = NazwiskoTextBox.Text;
        var tmpImie = ImieTextBox.Text;
        var tmpNrIndeksu = NrIndeksuTextBox.Text;
        var StudentSubjects = Subject._SubjectList.Where(p => p.IsChecked == true);
        var AreSubjectsSelected = StudentSubjects.Any();

        if (tmpImie != "" && tmpNazwisko != "" && tmpNrIndeksu != "" && StudiaComboBox.SelectedItem != null && AreSubjectsSelected)
        {
            var match = Regex.Match(tmpNrIndeksu, "^s[0-9]{4,5}$");
            var tmpStudia = ((Studies)StudiaComboBox.SelectedItem).Id;
            if (match.Success)
            {
                NewStudent = new Student
                {
                    Imie = tmpImie,
                    Nazwisko = tmpNazwisko,
                    NrIndeksu = tmpNrIndeksu,
                    IdStudia = tmpStudia,
                    ListaWybranychPrzedmiotow = StudentSubjects
                }; 
                foreach (Subject s in NewStudent.ListaWybranychPrzedmiotow)
                    MessageBox.Show(s.Name);   //HERE CheckBoxes appear
                Close();
            }
        }
    }
}

As @Blorgbeard said. Changing ListaWybranychPrzedmiotow = StudentSubjects to ListaWybranychPrzedmiotow = StudentSubjects.ToList() in creating new Student object in DeansOffice class, works.

Thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM