C # protected keyword related issue

I have a product.aspx class

public partial class _Products : Product
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Product p1 = new Product();
        p1.m1(); ///here I am facing problem y intelligence system not allowing me access the product class method m1();   
    }
   new virtual int m1() 
    {
        return 10;   
    }
}

and in my appcode i have class product.cs

public class Product
{
    public Product()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    protected void m1() 
    { 

    }
    public void m2() 
    { 

    }

Task: p1.m1 (); here I ran into a problem with my intelligence system that did not allow me to access the m1 () product class method;

+3
source share
4 answers

If you create an instance of a new product, you cannot get a protected element from another class to it, even if it is obtained from the product

+2
source

You are not reporting your problem, but I expect you to mean:

(base class)

protected abstract int m1();

(sub-class)

protected override int m1() {
    return 10;
}

; ; . Page_Load , , mehods ("this").

+2

m1() Product (parent) void, m1() _Products (child) int. . , , m1() ( ) m1() .

+1

, , , , defind,

 public class P : Product
{
    public P()
    {
        m1();
    }
}


public class Product
{
    public Product()
    {        //        // TODO: Add constructor logic here        
    }
    protected void m1() { }
    public void m2() { }



}
+1

Source: https://habr.com/ru/post/1777274/


All Articles