Pradeep
Pradeep

Reputation: 2729

the modifier public is not valid for this item

I am getting error

the modifier public is not valid for this item

this 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);
    }

Thanks, Pradeep

Upvotes: 7

Views: 26332

Answers (2)

Hans Passant
Hans Passant

Reputation: 942050

Omit the "public" keyword from your interface method declarations. That's not valid, interfaces always have public accessibility.

Defining these methods in the Base class isn't good enough either. Either let the Base class inherit "test" or move the methods to First.

Declaring the Page_Load event handler protected is fishy too, it ought to be private since overriding it isn't possible and calling it directly from a class derived from First would normally be a mistake.

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1039238

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 not valid on interface declarations:

Interfaces consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static members. Interfaces members are automatically public, and they cannot include any access modifiers.

Upvotes: 23

Related Questions