Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObservableCollection.SetItem() is inaccessible due to its protection level

Tags:

c#

.net

wpf

I'm creating a CRUD application through the WPF Application to learn the basics. However, I am running into a 'inaccessible due to its protection level' when I call SetItem() to change an element within the ObservableCollection.

Things that I have tried but did not solve the problem:

  1. Clean and rebuild
  2. Changing all protection levels to public as well as internal

Does anyone have any ideas or other suggestions that may help? Thanks in advance!

Error 1 'System.Collections.ObjectModel.Collection.SetItem(int, WpfApplication1.User)' is inaccessible due to its protection level C:\Users\sliu\Documents\Visual Studio 2013\Projects\WpfApplication1\WpfApplication1\MainWindow.xaml.cs 70 27 WpfApplication1

Where the code is failing:

private void onKeyDownHandler(object sender, System.Windows.Input.KeyEventArgs e)
{
    BindingExpression binding = addUser.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty);
    binding.UpdateSource();

    User temp_user;

    if ((e.Key == Key.Return) && !addUser.Text.Equals(""))
    {

        if (modify)
        {
            **//THIS IS WHERE THE PROBLEM IS
            users.SetItem(index, new User() { Name = addUser.Text });**
        }
        else
        {
            users.Add(new User() { Name = addUser.Text });
        }
    }
}

Entire code:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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.ComponentModel;
using System.Windows.Forms;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<User> users = new ObservableCollection<User>();
        public bool modify = false;
        public int index = -1;

        public MainWindow()
        {
            InitializeComponent();

            users.Add(new User() { Name = "John Doe" });
            users.Add(new User() { Name = "Jane Doe" });

            lbUsers.ItemsSource = users;
        }

        private void btnAddUser_Click(object sender, RoutedEventArgs e)
        {
            users.Add(new User() { Name = "New user" });
        }

        private void btnModify_Click(object sender, RoutedEventArgs e)
        {
            if (lbUsers.SelectedItem != null)
            {
                addUser.Text = (lbUsers.SelectedItem as User).Name;
                modify = true;
                index = users.IndexOf(lbUsers.SelectedItem as User);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (lbUsers.SelectedItem != null)
                users.Remove(lbUsers.SelectedItem as User);
        }

        private void onKeyDownHandler(object sender, System.Windows.Input.KeyEventArgs e)
        {
            BindingExpression binding = addUser.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty);
        binding.UpdateSource();

            User temp_user;

            if ((e.Key == Key.Return) && !addUser.Text.Equals(""))
            {

                if (modify)
                {
                    **//THIS IS WHERE THE PROBLEM IS
                    users.SetItem(index, new User() { Name = addUser.Text });**
                }
                else
                {
                    users.Add(new User() { Name = addUser.Text });
                }
            }
        }
    }

    public class User : INotifyPropertyChanged
    {
        public string name;
        public string Name
        {
            get { return this.name; }
            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    this.NotifyPropertyChanged("Name");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}
like image 799
susieloo_ Avatar asked Aug 20 '14 16:08

susieloo_


2 Answers

ObservableCollection<T>.SetItem is a protected method, meaning its only available to derived types.

You have two choices:

  1. use Insert (Note this will shift the elements down by 1, not replace the value originally placed in the specified index):

    users.Insert(0, item);
    
  2. Use an indexer:

    users[index] = new User { /*....*/ };
    
like image 141
Yuval Itzchakov Avatar answered Nov 08 '22 08:11

Yuval Itzchakov


Use the indexer instead.

users[index] = new User() { Name = addUser.Text };
like image 24
Daniel A. White Avatar answered Nov 08 '22 09:11

Daniel A. White