title: collection is inaccessible due to its protection level error wpf c#
body: <p>Hi I am quite new to wpf and c#. I have written a simple WPF app but it doesn't work because of the error <code>collection is inaccessible due to its protection level error</code>. I was wondering what am I doing wrong? </p>
  
  <p>Here is my code:</p>
  
  <p>It is only a dummy program and there is quite a lot left to do in it like creating another window to add and edit collection items, but I cant get past the hurdle of the protection level error. Can anyone help me fix it? </p>
  
  <p>Main window c#: </p>
  
  <pre><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;
  using System.Windows.Threading;
  using System.Collections.ObjectModel;
  using System.IO;
  
  namespace cars
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          ObservableCollection&lt;Cars&gt; cars;
          Random ran = new Random(Environment.TickCount);
  
  public MainWindow()
  {
      InitializeComponent();
  }
  
  //start of window loaded
  private void Window_Loaded(object sender, RoutedEventArgs e)
  {
      //clock############################################
      DateTime.Now.ToLongTimeString();                //#
      DispatcherTimer clock = new DispatcherTimer();  //#
      clock.Interval = new TimeSpan(0, 0, 1);         //#
      clock.Tick += clock_Tick;                       //#
      clock.Start();                                  //#
      //#################################################
  
      //creating new observable collection for my cars
      cars = new ObservableCollection&lt;Cars&gt;();
  
      // adding 3 cars in to collection ########################################
      cars.Add(new Cars(001, Type.Coupe, "Porsche", Fuel.Petrol, "Red"));    //#
      cars.Add(new Cars(002, Type.Coupe, "Ferrari", Fuel.Petrol, "Blue"));   //#
      cars.Add(new Cars(003, Type.Coupe, "McLaren", Fuel.Petrol, "Yellow")); //#
      //########################################################################
  
      //displaing collection in lable
      lbxCars.ItemsSource = cars;
  
      //start randomly picking car from collection 
      PickRandomCar();
  
  }//end of window loaded
  
  //pick random car method ##############################
  private void PickRandomCar()                        //#
  {                                                   //#
      Cars randCars = cars[ran.Next(cars.Count)];     //#
      lblRandom.Content = randCars;                   //#
  }                                                   //#
  //#####################################################
  
  //method to display time in text box ##################
  void clock_Tick(object sender, EventArgs e)         //#
  {                                                   //#
      tbxClock.Text = DateTime.Now.ToLongTimeString();//#
  }                                                   //#
  //#####################################################
  
  //methodfor adding cars in to cars collection ###################
  private void btnAddCar_Click(object sender, RoutedEventArgs e)//#
  {                                                             //#
      AddCars AddCars = new AddCars();
      AddCars.Owner = this;
      AddCars.ShowDialog();
  }                                                             //#
  //###############################################################
  
  //method to edit cars in cars collection##########################
  private void btnEdidCar_Click(object sender, RoutedEventArgs e)//#
  {                                                              //#
  
  }                                                              //#
  //################################################################
  
  private void btnDeleteCar_Click(object sender, RoutedEventArgs e)
  {
  
  }
  
  private void btnRentCar_Click(object sender, RoutedEventArgs e)
  {
  
  }
  
  private void btnReturnCar_Click(object sender, RoutedEventArgs e)
  {
  
  }
  
  private void tbxFilterName_TextChanged(object sender, TextChangedEventArgs e)
  {
  
  }
  
  private void tbxFilterNumber_TextChanged(object sender, TextChangedEventArgs e)
  {
  
  }
  
  private void tbxFilterName_GotFocus(object sender, RoutedEventArgs e)
  {
  
  }
  
  private void tbxFilterNumber_GotFocus(object sender, RoutedEventArgs e)
  {
  
  }
  
  private void lbxMembers_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
  
  }
  }
  </code></pre>
  
  <p>Main Page xaml:</p>
  
  <pre><code>&lt;Window x:Class="cars.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="CARS" Height="420" Width="650"
          Loaded="Window_Loaded"&gt;
      &lt;Window.Resources&gt;
              &lt;Style x:Key="btnStyle" TargetType="Button"&gt;
              &lt;Setter Property="Margin" Value="5"/&gt;
              &lt;Setter Property="FontSize" Value="14"/&gt;
          &lt;/Style&gt;
  
          &lt;Style x:Key="txtblkStyle" TargetType="TextBlock"&gt;
              &lt;Setter Property="Margin" Value="5"/&gt;
              &lt;Setter Property="FontSize" Value="15"/&gt;
              &lt;Setter Property="VerticalAlignment" Value="Top"/&gt;
              &lt;Setter Property="Background" Value="White"/&gt;
          &lt;/Style&gt;
  
          &lt;Style x:Key="txtbxStyle" TargetType="TextBox"&gt;
              &lt;Setter Property="Width" Value="200"/&gt;
              &lt;Setter Property="Margin" Value="5"/&gt;
              &lt;Setter Property="FontSize" Value="15"/&gt;
              &lt;Setter Property="VerticalAlignment" Value="Center"/&gt;
          &lt;/Style&gt;
  
          &lt;Style x:Key="lblMember" TargetType="Label"&gt;
              &lt;Setter Property="Width" Value="100"/&gt;
              &lt;Setter Property="Margin" Value="5"/&gt;
              &lt;Setter Property="FontSize" Value="15"/&gt;
              &lt;Setter Property="VerticalAlignment" Value="Center"/&gt;
              &lt;Setter Property="HorizontalAlignment" Value="Left"/&gt;
          &lt;/Style&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid Background="Goldenrod"&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition/&gt;
              &lt;ColumnDefinition/&gt;
              &lt;ColumnDefinition/&gt;
              &lt;ColumnDefinition/&gt;
              &lt;ColumnDefinition/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="0.6*" /&gt;
              &lt;RowDefinition Height="1.5*"/&gt;
              &lt;RowDefinition Height="0.7*"/&gt;
              &lt;RowDefinition Height="0.7*"/&gt;
              &lt;RowDefinition Height="0.7*"/&gt;
              &lt;RowDefinition Height="0.7*"/&gt;
              &lt;RowDefinition Height="0.7*"/&gt;
              &lt;RowDefinition Height="1*"/&gt;
              &lt;RowDefinition Height="0.7*"/&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;TextBox Name="tbxFilterName" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Left" Style="{StaticResource txtbxStyle}" TextChanged="tbxFilterName_TextChanged"  GotFocus="tbxFilterName_GotFocus" /&gt;
          &lt;TextBox Name="tbxFilterNumber" Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="3" HorizontalAlignment="Right" Style="{StaticResource txtbxStyle}" TextChanged="tbxFilterNumber_TextChanged"  GotFocus="tbxFilterNumber_GotFocus" /&gt;
          &lt;!--buttons--&gt;
          &lt;Button x:Name="btnAddCar" CommandParameter="add" Content="Add Car"   Grid.Row="8"  Grid.Column="0" Style="{StaticResource btnStyle}" Click="btnAddCar_Click"/&gt;
          &lt;Button x:Name="btnEdidCar" Content="Edit Car"   Grid.Row="8" Grid.Column="1" Style="{StaticResource btnStyle}" Click="btnEdidCar_Click"/&gt;
          &lt;Button x:Name="btnDeleteCar" Content="Delete Car" Grid.Row="8" Grid.Column="2" Style="{StaticResource btnStyle}" Click="btnDeleteCar_Click"/&gt;
          &lt;Button x:Name="btnRentCar" Content="Rent Car" Grid.Row="8" Grid.Column="3" Style="{StaticResource btnStyle}" Click="btnRentCar_Click"/&gt;
          &lt;Button x:Name="btnReturnCar" Content="Return Car" Grid.Row="8" Grid.Column="4" Style="{StaticResource btnStyle}" Click="btnReturnCar_Click"/&gt;
          &lt;!--labels--&gt;
          &lt;Label Name="lblNumber" Content="Number: " Grid.Row="2" Grid.Column="3" Style="{StaticResource lblMember}"  /&gt;
          &lt;Label Name="lblName" Content="Type: " Grid.Row="3" Grid.Column="3" Style="{StaticResource lblMember}" Margin="5,0,0,5" VerticalAlignment="Bottom"  /&gt;
          &lt;Label Name="lblPhone" Content="Name: " Grid.Row="4" Grid.Column="3" Style="{StaticResource lblMember}"  /&gt;
          &lt;Label Name="lblAddress" Content="Fuel Type: " Grid.Row="5" Grid.Column="3" Style="{StaticResource lblMember}"  /&gt;
          &lt;Label Name="lblRentals" Content="Colour: " VerticalAlignment="Top" Grid.Row="6" Grid.Column="3" Style="{StaticResource lblMember}" Margin="5,10,0,0" Grid.RowSpan="2"  /&gt;
          &lt;!--textblocks--&gt;
          &lt;TextBlock Name="txtblkNumber" Grid.Column="4" Grid.Row="2" Style="{StaticResource txtblkStyle}" &gt;&lt;/TextBlock&gt;
          &lt;TextBlock Name="txtblkName" Grid.Column="4" Grid.Row="3" Style="{StaticResource txtblkStyle}" &gt;&lt;/TextBlock&gt;
          &lt;TextBlock Name="txtblkPhone" Grid.Column="4" Grid.Row="4" Style="{StaticResource txtblkStyle}" &gt;&lt;/TextBlock&gt;
          &lt;TextBlock Name="txtblkAddress" Grid.Column="4" Grid.Row="5" Height="33" Style="{StaticResource txtblkStyle}" Margin="5,5,5,0" /&gt;
          &lt;TextBlock Name="txtblkRental" TextWrapping="Wrap" Height="30" Grid.Column="4" Grid.Row="6" Style="{StaticResource txtblkStyle}" Margin="0,10,15,0" Grid.RowSpan="2" /&gt;
          &lt;!--Style="{StaticResource txtblkStyle}"--&gt;
          &lt;ListBox Name="lbxCars" Grid.Column="0" Grid.Row="2" Grid.RowSpan="5" Grid.ColumnSpan="3" Background="Aqua" SelectionChanged="lbxMembers_SelectionChanged" &gt;&lt;/ListBox&gt;
          &lt;Image Name="imgUser" Grid.Column="3" Grid.Row="1" Stretch="Uniform" &gt;&lt;/Image&gt;
          &lt;TextBox x:Name="tbxClock" Grid.ColumnSpan="2" Grid.Column="3" HorizontalAlignment="Left"  Grid.Row="1" TextWrapping="Wrap"  VerticalAlignment="Top" Width="120"/&gt;
          &lt;Label x:Name="lblRandom" Content="" Grid.Column="2" HorizontalAlignment="Left" Margin="5,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="119" Height="60"/&gt;
  
  
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Class I used:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.IO;
  
  namespace cars
  {
  //enum for fuel types
  public enum Fuel
  {
      Petrol, Diesel, Electric, Other
  }
  
  //enum for car types
  public enum Type
  {
      Cabriolet, Coupe, Saloon, Hatchback, Estate, MPV, SUV, Other
  }
  //start class
  class Cars
  { 
      //foelds and properties
      public int Number { get; set; }
      public Type Type { get; set; }
      public string Name { get; set; }
      public Fuel FuelType { get; set; }
      public string Colour { get; set; }
  
      // working constructor
      public Cars(int number, Type type, string name, Fuel fuel, string colour)
      {
          Number = number;
          Type = type;
          Name = name;
          FuelType = fuel;
          Colour = colour;
      }
  
      // to string method
      public override string ToString()
      {
          return string.Format(" Number: {0}.\n Type: {1}.\n Name: {2}.\n Fuel type: {3}.\n Colour: {4}. ", Number, Type, Name, FuelType, Colour);
      }
  
      //method to find text file with cars
      public static string GetCarsFile()
      {
          string debug = Directory.GetCurrentDirectory();
          string bin = Directory.GetParent(debug).ToString();
  
          return Directory.GetParent(bin) + "\\cars.txt";
      }
  }
  }
  </code></pre>