Problem with Constructors

拟墨画扇 提交于 2019-12-13 03:39:21

问题


****Just below is my winform client inst with one parameter constructor within the class.****

    private void button1_Click(object sender, EventArgs e)
    {

        string s1 = textBox1.Text;
        int x1 =  Convert.ToInt32(s1);
        int X= x1;        
       ExternalTest ob =  new ExternalTest(X);                 
       string s2 = Convert.ToString(ob.Y);              
        ob.Y = 0;
       textBox2.Text = s2;

And below this is my class that i added to the project The code below is an added class within the assembly. If i tried to make it a class library and and add addreference - it will not build.

class ExternalTest       
{      
            private int _x;
       //     protected new int x
       //     {
       //        get { return _x; }
       //        set  {_x = value ;}
       //     }
             private int y;
             public  int Y 
            {                 
              get {return y =  Mult(_x); }
              set { }
            }        
                internal   int Mult(int _x)
              {                       
              y = _x + 51;   
               return  y;         
               }    

            public ExternalTest(int X)
           {
               _x =  X;             

           }            

      }
}

回答1:


Your class is not public by default. You must add public to the definition of the class when you're using it in an external library, or the WinForms client will not be able to see it.

EG:

public class ExternalObj
{
    // ... 
}

Based on the fact that you are getting a compile error only when this class is in an external library, and the numerous times I've forgotten to add public when I've needed it myself, I'm thinking this is probably the issue.



来源:https://stackoverflow.com/questions/3189302/problem-with-constructors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!