Hello friends,

I am new to C# and try to learn the basics. I am getting error "error CS0052: Inconsistent accessibility: field the 'Model.Notifier' is less accessible than field 'Model.notifyViews'"

Could you please let me know the error I am getting...

using System;
using System.Data;
class Model 
{
	delegate void Notifier(string sender);
	public event Notifier notifyViews;
	public void Change() 
	{
		notifyViews("Model");
	}
}

class View1
{
	public View1(Model m)
	{
		 m.notifyViews += new Notifier(this.Update1);
	}
	
	void Update1(string sender)
	{
		Console.WriteLine(sender + " was changed");
	}
}

class View2
{
	public View2(Model m)
	{
		 m.notifyViews += new Notifier(this.Update2);
	}

	void Update2(string sender)
	{
		Console.WriteLine(sender + " was changed");
	}
}

class Test 
{
	static void Main() 
	{
		Model m = new Model(); 
		new View1(m); 
		new View2(m);
		m.Change();
	}
}

You need to prefix your delegate declaration with an access modifier. If you have a public event that uses a delegate reference that is private to the class then other assemblies can see an event but can't access the delegate's type, thus you get the compilation error. If you have a public event, you need to expose a public delegate for that event. Add public in front of your delegate line:

using System;
using System.Data;

class Model 
{
	public delegate void Notifier(string sender);
	public event Notifier notifyViews;
	public void Change() 
	{
		notifyViews("Model");
	}
}

Likewise if you made the event private then the delegate declaration could also be private:

** This is proof of concept, You don't want this code to answer your question.

using System;
using System.Data;

class Model 
{
	private delegate void Notifier(string sender);
  private event Notifier notifyViews;
	public void Change() 
	{
		notifyViews("Model");
	}
}

Hi Scott,

I added the necessary accessifier/modifier i.e. defined public to the delegate line, still it throws error to me.

Regards
Deven.

Here is the complete code with two other fixes:

using System;
using System.Data;
class Model
{
  public delegate void Notifier(string sender);
  public event Notifier notifyViews;
  public void Change()
  {
    notifyViews("Model");
  }
}

class View1
{
  public View1(Model m)
  {
    m.notifyViews += new Model.Notifier(this.Update1);
  }

  void Update1(string sender)
  {
    Console.WriteLine(sender + " was changed");
  }
}

class View2
{
  public View2(Model m)
  {
    m.notifyViews += new Model.Notifier(this.Update2);
  }

  void Update2(string sender)
  {
    Console.WriteLine(sender + " was changed");
  }
}

class Test
{
  static void Main()
  {
    Model m = new Model();
    new View1(m);
    new View2(m);
    m.Change();
  }
}

Thanks sknake. It worked and the only difference I noticed that I was using the object name along with the delegate and that was causing the problem.

Thanks once again.....Sorry for the late response as saw your note today only.

This question has already been answered. Start a new discussion instead.