檔案 -> 專案 -> 類別庫(如圖)
程式碼範例如下:
範例一是測試的範例
範例二是偵錯的範例,除了藍色的部份要記得加入一個windows form
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace dllfunction
{
public class Class1
{
//範例一,數字相加輸出數字
public int add(int a, int b)
{
int sum = a + b;
return sum;
}
//範例二,字串轉數字後相加後輸出字串
public string addstr(string strA, string strB)
{
int retult = 0;
try
{
retult = int.Parse(strA) + int.Parse(strB);
}
catch(Exception ex)
{
MessageBox.Show("" + ex.ToString() + " ");
}
return "" + retult;
}
//
}
}
程式寫好後建置方案
建置 -> 建置方案
接下來把dll檔放到要引用的專案目錄下
專案 -> 加入參考 -> 瀏覽(引用dll檔)
照以下綠色段落的方式動態引用
以下是引用的程式碼:
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 dllfunction;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//
private void button1_Click(object sender, EventArgs e)
{
dllfunction.Class1 encode = new dllfunction.Class1();
button1.Text = "1+2=" + encode.add(1, 2);
}
//
private void button2_Click(object sender, EventArgs e)
{
dllfunction.Class1 encode = new dllfunction.Class1();
button2.Text = "2+3=" + encode.addstr("2", "3");
}
//
private void button3_Click(object sender, EventArgs e)
{
dllfunction.Class1 encode = new dllfunction.Class1();
button3.Text = "3+a=" + encode.addstr("2", "a");
}
//
}
}
然後是輸出結果,
第一個例子:產出int數字,
第二個例子:產出string格式的數字
第三個例子:產生錯誤訊息後輸出0字串 (這是除錯的範例)
本文大致提供建置與除錯dll的方式,就介紹到這邊
沒有留言:
張貼留言