C # custom class libraries and Custom Controls

Source: Internet
Author: User

 

Differences between Class Library and Windows Form Control Library:

Both of them generate dll files, but the control library is a special class library. The control library can be compiled and run, while the class library can only be compiled. It must be referenced in other projects that can be used as "Start up project.

Method to reference the Class Library: In Solution Explorer, right-click the References of the project, select "Add reference", and select browse to find the corresponding DLL to Add

To use the custom control library, right-click the Toolbox and select "choose items". In the displayed dialog box, click "Browse" to add the control library. After the Toolbox is added, a custom control is displayed at the bottom of the Toolbox.

Create your own class library

See http://hi.baidu.com/hongyueer/blog/item/ce3b39a618133e92d143582d.html

1. Create a project and select the Class Library type.

2. Select the namespace name.

3. Create your own public classes. The Code is as follows:

Using System;

Using System. Collections. Generic;

Using System. Text;

 

Namespace Chapter09

{

Public class Box

{

Private string [] names = {"length", "width", "height "};

Private double [] dimensions = new double [3];

Public Box (double length, double width, double height)

{

Dimensions [0] = length;

Dimensions [1] = width;

Dimensions [2] = height;

}

Public double this [int index]

{

Get

{

If (index <0) | (index> = dimensions. Length ))

Return-1;

Else

Return dimensions [index];

}

Set

{

If (index> = 0 & index <dimensions. Length)

Dimensions [index] = value;

}

}

 

Public double this [string name]

{

Get

{

Int I = 0;

While (I <names. Length) & (name. ToLower ()! = Names [I])

I ++;

Return (I = names. Length )? -1: dimensions [I];

}

Set

{

Int I = 0;

While (I <names. Length) & (name. ToLower ()! = Names [I])

I ++;

If (I! = Names. Length)

Dimensions [I] = value;

}

}

}

}

5. Test your own class library

First, add a reference to your class library. (create a project, right-click the project and add a reference, select the "Browse" tab, find your dll file, and add it .)

Then, use using to include your namespace and you can use your class. The Code is as follows:

Using System;

Using System. Collections. Generic;

Using System. Text;

Using Chapter09;

 

Namespace Test_classLib

{

Class Program

{

Static void Main (string [] args)

{

Box box = new Box (20, 30, 40 );

 

Console. WriteLine ("Created a box :");

Console. WriteLine ("box [0] = {0}", box [0]);

Console. WriteLine ("box [1] = {0}", box [1]);

Console. WriteLine ("box [0] = {0}", box ["length"]);

Console. WriteLine ("box [\" width \ "] = {0}", box ["width"]);

}

}

}

Note: The use of the C # indexer is also demonstrated in this example.

 

From Shine's holy heaven-Min Chen 〃

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.