创建并引用库文件
一、用C#创建并编写库文件
打开 Visual Studio 2015并新建类库文件并命名为average,程序编写完成后点击运行,生成库类文件。
代码如下(数字之间以空格分开,求取平均数):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace average
{
public class Class1
{
public double AVER(string inputString)
{
IList<string> input = inputString.Split(' ');
IList<double> input2 = new List<double>();
double temp = 0;
for (int i = 0; i < input.Count; i++)
{
if (!double.TryParse(input[i], out temp))
{
break;
}
else
{
input2.Add(temp);
}
}
Calc(input2);
return Calc(input2);
}
private static double Calc(IList<double> input)
{
double temp = 0;
foreach (double var in input)
{
temp += var;
}
double average = temp / input.Count;
return average;
}
}
}
二、库文件的引用
1.创建添加控件WindowsFormsApplication文件并添加控件
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button aver;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Button array;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.NumericUpDown MAX;
private System.Windows.Forms.NumericUpDown MIN;
private System.Windows.Forms.Label label3;
2.库文件的引用方法一
打开库文件的average→average→bin→Debug复制average.dll文件,并将库文件放在新建WindowsFormsApplication文件的 求平均数→求平均数→bin→Debug中,此时我们可以通过输入代码对average库进行引用。
代码如下(示例):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;//引用库文件
namespace 求平均数
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 创建数组
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void array_Click(object sender, EventArgs e)
{
try
{
richTextBox1.Clear();
Random rd = new Random();
int stu = Convert.ToInt32(textBox1.Text);//创建数组的数量
int max = Convert.ToInt32(MAX.Value);//最大值
int min = Convert.ToInt32(MIN.Value);//最小值
for (int i = 0; i < stu; i++)
{
richTextBox1.Text += rd.Next(min, max).ToString() + " ";//创建数组
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());//显示问题
}
}
/// <summary>
/// 取平均数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void aver_Click(object sender, EventArgs e)
{
average.Class1 averAge = new average.Class1();//实例化
double ad = averAge.AVER(richTextBox1.Text);
richTextBox2.Text = ad.ToString();
}
}
}
3.库文件的引用方法一
新建WindowsFormsApplication,右击引用→添加引用→浏览→average.dll,添加
using average;
代码如下(示例):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using average;
namespace 求平均数
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 创建数组
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void array_Click(object sender, EventArgs e)
{
try
{
richTextBox1.Clear();
Random rd = new Random();
int stu = Convert.ToInt32(textBox1.Text);//创建数组的数量
int max = Convert.ToInt32(MAX.Value);//最大值
int min = Convert.ToInt32(MIN.Value);//最小值
for (int i = 0; i < stu; i++)
{
richTextBox1.Text += rd.Next(min, max).ToString() + " ";//创建数组
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());//显示问题
}
}
/// <summary>
/// 取平均数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void aver_Click(object sender, EventArgs e)
{
Class1 averAge = new Class1();
double ad = averAge.AVER(richTextBox1.Text);
richTextBox2.Text = ad.ToString();
}
}
}