Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the modifier public is not valid for this item

Tags:

c#

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

like image 860
Pradeep Avatar asked Sep 06 '10 15:09

Pradeep


2 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 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.

like image 123
Darin Dimitrov Avatar answered Nov 11 '22 12:11

Darin Dimitrov


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.

like image 22
Hans Passant Avatar answered Nov 11 '22 14:11

Hans Passant