C# 自定义类库 方法提示

C# 自定义类库 方法提示,第1张

Class Library(类库)与Windows Form Control Library(控件库)的区别:

两者编译都生成dll文件,不过控件库是特殊的类库。控件库可以编译运行,而类库只能编译,必须在其它可做为”Start up project”的工程中引用后使用。

引用类库的方法:在Solution Explorer中,工程的References上点右键,选择”Add reference”,然后选择browse找到相应的DLL添加

使用自定义控件库的方法:在Toolbox上单击右键,选择“choose items”,d出对话框里点”Browse”,找到控件库添加。添加成功后,会在Toolbox的最下面出现自定义的控件。

创建自己的类库

1、新建工程 ,选择Class Library类型。

2、选择名字空间名。

3、创建自己的公用类。代码如下:

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、测试自己的类库

首先增加对自己的类库的引用,(新建项目,在项目上右键-》添加引用,选择“浏览”标签,然后找到自己的dll文件,加入。)

然后用using包含自己的名字空间,即可使用自己的类了。代码如下:

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"])

        }

    }

}

不明白你说的“显示”是什么意思。

在第一个项目中是可以调用类库中的两个类的。

首先你的类库中的两个类必须用public修饰。然后右键你的项目名-添加引用-项目,把拟建的类库添加加到你原来的项目中去。再在你原来项目的类中编写代码,引入命名空间,

using System

using System.Collections.Generic

using System.Linq

using System.Text

using TestSolution//这个就是新建的类库名称

接下来就很简单了啊

class Test//这个类就是原先项目中的类,注意,他不是建好的类库中的代码

{

a a = new a()//这里不是可以调用么?

b b = new b()//这里不是可以调用么?

}

其实也不是非常明白你的意思,我只是按我的理解去做的,如果不对别见怪,如果对了,希望可以帮到你

第一:对目标类库点击右键,如图:

第二:点击“添加服务引用”如图:

关键是图左下角的“高级”按键。点击如图

第三:在左下角,“添加WEB引用”的按钮点击


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11844490.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存