Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

xaml.cs code not working

  • 06-01-2012 6:33pm
    #1
    Registered Users, Registered Users 2 Posts: 1,521 ✭✭✭


    Hey im writing a program and i have an error.

    This is the code:
    public partial class MainWindow : Window
        {
    
            public List<Dept> departments = new List<Dept>();
            public List<Employee> employees = new List<Employee>();
            public List<Manager> managers = new List<Manager>();
    
            public MainWindow()
            {
                InitializeComponent();         
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                
    
    
                departments.Add(new Dept{ Name = "Marketing",}); //creates "Marketing" Department//     
                    {
                      employees.Add(new Employee{ Name="Ben Walker", DOB=new DateTime(1965,2,22), SSClass=SSC.A});
                      employees.Add( new Employee{ Name="Phil Mitchell", DOB=new DateTime(1956,6,12), SSClass=SSC.C});
                      employees.Add (new Employee{ Name="Brad Pitt", DOB=new DateTime(1978,6,02), SSClass=SSC.NG});
                    
                   
                    {
                      managers.Add(  new Manager{ Name="Bill Walkingtons"});
                    }
                    
                departments.Add(new Dept{ Name = "Legal",}); //creates "Legal" Department//
                  {
                        employees.Add(new Employee{ Name="Marshall Eriksen ", DOB=new DateTime(1982,1,14), SSClass=SSC.A});
                        employees.Add(new Employee{ Name="Matthew MmcConaughey", DOB=new DateTime(1972,7,12), SSClass=SSC.C});
                        employees.Add(new Employee{ Name="Lionel Hutz", DOB=new DateTime(1948,12,29), SSClass=SSC.NG});
                        employees.Add(new Employee{ Name="Harvey Dent", DOB=new DateTime(1968,4,17), SSClass=SSC.A});
                    }
                   
                    {
                      managers.Add(  new Manager{ Name="Ben Button"});
                    }
                    }
                
            }
    
    


    Each department contain Employees and a Manager.
    The error is :

    Error 1 Inconsistent accessibility: field type 'System.Collections.Generic.List<******.Dept>' is less accessible than field '********.MainWindow.departments' C:\Users\******\Desktop\*******\*******\MainWindow.xaml.cs 23 27 ********


    Ive tried placing the code for creating the 3 lists under the Window_Loaded but i need the Employee and Manager and Department list to be called upon later outside of the Window_Loaded.

    Any ideas?


Comments

  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    Where have you declared The Dept class? Is it private?


  • Registered Users, Registered Users 2 Posts: 1,521 ✭✭✭mcw92


    Where have you declared The Dept class? Is it private?

    in a class called company:
    enum SSC
        {
            A, C, NG, P
            //end enum
        }
    
    
        class Employee 
        {
            public string Name { get; set; }
            public DateTime DOB { get; set; }
            public SSC SSClass { get; set; }
            public string Area { get; set; }
            public override string ToString()
            {
                return String.Format("{0} ({1} years) \tSSClass:{2} {3}", Name, Math.Round(((DateTime.Now - DOB).Days / 365.25), 0), SSClass.ToString(), Area);
            }
        }
    
        class Dept
        {
            public string Name { get; set; }
            public List<Employee> employees { get; set; }
            public List<Manager> managers { get; set; }
            public override string ToString()
            {
                return String.Format("{0}, {1}", Name);
            }
        }
    
        class Company
        {
            public string Name { get; set; }
        }
    
        class Manager
        {
            public string Name { get; set; }
        }
    
    


    heres the full .cs code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace *********
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
    
            public List<Dept> departments = new List<Dept>();
            public List<Employee> employees = new List<Employee>();
            public List<Manager> managers = new List<Manager>();
    
            public MainWindow()
            {
                InitializeComponent();         
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                
    
    
                departments.Add(new Dept{ Name = "Marketing",}); //creates "Marketing" Department//     
                    {
                      employees.Add(new Employee{ Name="Ben Walker", DOB=new DateTime(1965,2,22), SSClass=SSC.A});
                      employees.Add( new Employee{ Name="Phil Mitchell", DOB=new DateTime(1956,6,12), SSClass=SSC.C});
                      employees.Add (new Employee{ Name="Brad Pitt", DOB=new DateTime(1978,6,02), SSClass=SSC.NG});
                    
                   
                    {
                      managers.Add(  new Manager{ Name="Bill Walkingtons"});
                    }
                    
                departments.Add(new Dept{ Name = "Legal",}); //creates "Legal" Department//
                  {
                        employees.Add(new Employee{ Name="Marshall Eriksen ", DOB=new DateTime(1982,1,14), SSClass=SSC.A});
                        employees.Add(new Employee{ Name="Matthew MmcConaughey", DOB=new DateTime(1972,7,12), SSClass=SSC.C});
                        employees.Add(new Employee{ Name="Lionel Hutz", DOB=new DateTime(1948,12,29), SSClass=SSC.NG});
                        employees.Add(new Employee{ Name="Harvey Dent", DOB=new DateTime(1968,4,17), SSClass=SSC.A});
                    }
                   
                    {
                      managers.Add(  new Manager{ Name="Ben Button"});
                    }
                    }
    
                refreshDeptList();
                refreshEmployeeList();
            }
    
            private void refreshEmployeeList()
            {
                lbxEmployees.Items.Clear();
                foreach (Employee e in employees) lbxEmployees.Items.Add(e);
            }
    
                 private void refreshDeptList()
                 {
                 lbxDept.Items.Clear();
                 foreach (Dept d in departments) lbxDept.Items.Add(d);
                 }
    
                 private void refreshManagerList()
                 {
                     lbxDept.Items.Clear();
                     foreach (Manager m in managers) lbxDept.Items.Add(m);
                 }
    
             [COLOR="SeaGreen"]//    private void lbxDept_SelectionChanged(object sender, SelectionChangedEventArgs e)
             //    {
             //    Dept d = (Dept)((ListBox)sender).SelectedItem;
             //    if (d == null) return; // if no department is selected//
             //    lbxEmployees.Items.Clear(); // clears the employees list//
             //    foreach (Dept d in  lbxEmployees.Items.Add(e));
             //    }
    //DONT MIND THIS BIT!//[/COLOR]
    
    
            private void mnuQuit_Click(object sender, RoutedEventArgs e)
            {
                this.Close();
            }
    
        }
    }
    
    
    

    How the program works is:

    A Company contains 2 Departments, Each department contains Employees and a Manager.


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    In a file called company you mean? Try making them public classes


  • Registered Users, Registered Users 2 Posts: 1,521 ✭✭✭mcw92


    what do you mean? like add public to before each class name?


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    Yeah, they're private by default. How can you instance a private class with a private default constructor unless it's an inner class?

    Edit: turns out they're internal in a namespace by default according to the docs. Well, I dunno then. 2 weeks off has rotted my brain!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,521 ✭✭✭mcw92


    Ok anyways im trying to populate a listbox lbxEmployees with the employees listed.
    refreshEmployeeList();
    }

    private void refreshEmployeeList()
    {
    lbxEmployees.Items.Clear();
    foreach (Employee e in employees) lbxEmployees.Items.Add(e);
    }

    is this right?


  • Registered Users, Registered Users 2 Posts: 11,989 ✭✭✭✭Giblet


    Try AddRange instead of Add for that operation, you won't need a for loop then.


  • Closed Accounts Posts: 22,479 ✭✭✭✭philologos


    It has to do with the visibility of the class. By default this is "internal" in C#. You can't have a private class because that would defeat the point of a class. Internal means that they are accessible within the same namespace.

    Do the namespaces differ between the view (Your XAML and code-behind) from your classes such as Dept? If so you may have to add a using reference to the folder that it is contained in. For example using ProjectName.Folder;


  • Registered Users, Registered Users 2 Posts: 2,494 ✭✭✭kayos


    Everyone has addressed the issue of your error but as your using WPF here are some other tips....

    First biggie would be to read into the MVVM pattern. You will be happy you did when it comes to unit testing.

    Secondly don't use List<T> but rather use ObservableCollection<T> it will become obvious why once you go MVVM.


Advertisement