2007-04-24

VS2005專案同時使用C#與VB類別程式

步驟1:

在App_Code建立cs與vb資料夾

步驟2:
vb目錄裡面建立vbobj.vb內容如下
Imports Microsoft.VisualBasic
Public Class VBObject
Public Function Hello() As String
Return "Hi...VB"
End Function

End Class
cs目錄裡面建立csobj.cs內容如下
using System;

public class CSharpObject
{
public string Hello()
{
return "Hi...C#";
}
}

步驟3:
Web.config設定


步驟4:

前端頁面叫用
VB用法
'呼叫C#類別
Protected Sub btnCSharp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCSharp.Click
Dim myCSharpObject As New CSharpObject
txtMsg1.Text = myCSharpObject.Hello()
End Sub
'呼叫VB類別
Protected Sub btnVB_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVB.Click
Dim myVBObject As New VBObject
txtMsg2.Text = myVBObject.Hello()
End Sub

CS用法 
//呼叫C#類別
protected void btnCSharp_Click(object sender, EventArgs e)
{
CSharpObject myCSharpObject = new CSharpObject();
txtMsg1.Text = myCSharpObject.Hello();
}
//呼叫VB類別
protected void btnVB_Click(object sender, EventArgs e)
{
VBObject myVBObject = new VBObject();
txtMsg2.Text = myVBObject.Hello();
}


0 comments: