1

I made a library(dll file) for the follow code, And I make a succesfully Library file for ClassLibrary1.dll under side was the code:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Data;
 using System.Windows.Forms;



 namespace ClassLibrary1
 {
    class A
    {
         protected int s11;

         protected int Lock11;
         protected int Queue11;
         protected int Singal11;

         protected A(int s, int Lock, int Queue, int Singal)
        {
            s11 = s;

            Lock11 = Lock;
            Queue11 = Queue;
            Singal11 = Singal;


        }

        // The constructor obtains the state information.


        protected int StartClient()
        {
            int status;

            status = s11 + Queue11 + Lock11 + Singal11;
             MessageBox.Show("hello!"+ status.ToString());



            return status;


        }
        public int TCall()
        {
            int status;
            status = StartClient();
            return status;
        }

    }


    class B : A
    {

        public B(int s, int Lock, int Queue, int Singal) : base(s, Lock, 
                Queue, Singal)
        {
            int status;
            // Can access protected int but not private int!
            status = TCall();

        }


    }


}

And in my App form , I need to use the class B & TCall(), The question was that I could not use it, what should I do in the follow Form call:

     using System.Collections.Generic;
     using System.Data;
     using System.Drawing;
     using System.Text;
     using System.Windows.Forms;
     using ClassLibrary2;

     namespace MainForm
     {
        public partial class Form1 : Form
        {
            public Form1()
            {
                    InitializeComponent();
             }

            private void button1_Click(object sender, EventArgs e)
            {
                // how could I use class B in this area, since it was 
                 public in ClassLibrary2 ??
                         B cc = new B();    // likewise
            }
        }
     }

Please help me how to use the library call for proteced?

jazb
  • 5,498
  • 6
  • 37
  • 44
Angela
  • 111
  • 2
  • 2
  • 9

3 Answers3

0

You may want to understand default access modifier of C#. In this case, your classes in ClassLibrary1 are internal. If you want to use them in your winform project, make them public or use friend assembly

Leisen Chang
  • 826
  • 1
  • 6
  • 15
0

yes I did it , I include this two command line in my library (DLL) file

 using System.Runtime.CompilerServices;

 [assembly: InternalsVisibleTo("MainForm")]

And in my Form App , as like the follow code:

 using ClassLibrary1;

 namespace MainForm
{ 
    public partial class Form1 : Form
    {
         public Form1()
        {
            InitializeComponent();
        }

    private void button1_Click(object sender, EventArgs e)
    {
        int s = 1;
        int a= 1;
        int b = 1;
        int c = 1;

        // how could I use class B in this area, since it was public in ClassLibrary2 ??
        B cc = new B(s, a, b, c);
    }
}

}

Angela
  • 111
  • 2
  • 2
  • 9
0

You can't use B from your form because B is not public. But, if you make it public, you can do this:

        private void button1_Click(object sender, EventArgs e)
        {
            var cc = new B(1,1,1,1);
        }
Christian Findlay
  • 6,770
  • 5
  • 51
  • 103