Public modifier not valid for this item

I get an error

public modifier invalid for this element

this is my code please help me.

using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class First : System.Web.UI.Page,test { protected void Page_Load(object sender, EventArgs e) { Label1.Text = test1("Hi", 1).ToString(); } } public class Base { public int test1(int x) { return x; } public string test1(string x) { return x; } public string test1(string x, int y) { return x + y; } } public interface test { public int test1(int x); public string test1(string x); public string test1(string x, int y); } 

Thank you Pradeep

+4
source share
3 answers

Your interface declaration should look like this:

 public interface test { int test1(int x); string test1(string x); string test1(string x, int y); } 

Access modifiers are invalid in interface declarations :

Interfaces consist of methods, properties, events, indexes, or any combination of these four element types. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static elements. Interface elements are automatically public, and they cannot include any access modifiers.

+18
source

Omit the "public" keyword from interface method declarations. This is not valid; interfaces always have public accessibility.

Defining these methods in a base class is also not enough. Either let the Base class inherit "test" or translate the methods into First.

Declaring a protected Page_Load event handler is also dangerous, it must be closed, because it cannot be redefined, and calling it directly from the class obtained from First is usually an error.

+2
source

All Articles