我仿照您的提議寫出了下段程式碼
========Class1.vb (Root namespace設定為CrossRawToData)====================
//此部份程式碼已先行加入參考ImcCoreLib.DFile的函式庫插件
Public Class Class1
Public d As Integer
Public thisFile As ImcCoreLib.DFile
Public Function OpenFile() As Integer
thisFile.Open(".\test.RAW", 0)
d = thisFile.DChannels.Item(1).Length
OpenFile = d
End Function
End Clas
//上段的作用,為創造一個類別,其中函數OpenFile()的作用,
為開啟一test.Raw檔案,並將第一欄的列數,回傳給函數叫用者。
========C#部份======================
//此部份已先行將上面vb編程的Dll檔匯入參考
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
CrossRawToData.Class1 CRTD = new CrossRawToData.Class1();
Console.WriteLine("{0}", CRTD.OpenFile);
}
}
}
//以上程式碼,先NEW出一個上面VB所訂的類別,
並叫用該類別的OpenFile函數,並列印於螢幕上。
=======執行後出現以下錯誤
Class1.vb
第五行
thisFile.Open(".\test.RAW", 0) -----> 並未將物件參考設定為物件的執行個體
百思不解。
※ 引述《taba (得登..得登...)》之銘言:
: 最近也在研究C#,沒想到這.NET強調的功能竟然找了好幾本書都沒有,
: 最後在Professional C#的電子書上找到類似的@@ 寫了個C#調用VB的
: DLL的小程式,你參考一下。
: File => New => Project => Visual Basic => Class Library
: 方案總管 => ClassLibrary1 => 右鍵 => Properties =>
: Root Namespace => 改成CrossLangTest
: Class1.vb
: ===========================================================
: Public Class Class1
: Public d() As Integer = {0, 1, 2, 3, 4, 5}
: End Class
: ===========================================================
: 上面的會被編成ClassLibrary1.dll。
: File => New => Project => Visual C# => Console Application
: 方案總管 => ConsoleApplication1 => 右鍵 => Add Reference
: => Browse => 加入剛才的ClassLibrary1.dll
: Program.cs
: ===========================================================
: using System;
: namespace ConsoleApplication1
: {
: class Program
: {
: static void Main(string[] args)
: {
: CrossLangTest.Class1 oVB = new CrossLangTest.Class1();
: foreach (int i in oVB.d)
: {
: Console.WriteLine("{0}", i.ToString());
: }
: }
: }
: }
: ===========================================================
: output:
: ===========================================================
: 0
: 1
: 2
: 3
: 4
: 5
: ===========================================================
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.115.61.67
※ 編輯: Eleganse 來自: 140.115.61.67 (06/18 12:40)
推
06/18 14:28, , 1F
06/18 14:28, 1F
→
06/18 15:01, , 2F
06/18 15:01, 2F
→
06/18 15:01, , 3F
06/18 15:01, 3F
推
06/18 16:09, , 4F
06/18 16:09, 4F
→
06/18 16:11, , 5F
06/18 16:11, 5F
→
06/18 16:15, , 6F
06/18 16:15, 6F
→
06/18 16:16, , 7F
06/18 16:16, 7F
→
06/18 16:18, , 8F
06/18 16:18, 8F
→
06/18 16:24, , 9F
06/18 16:24, 9F
→
06/18 17:06, , 10F
06/18 17:06, 10F
討論串 (同標題文章)