看板 C_Sharp 關於我們 聯絡資訊
我仿照您的提議寫出了下段程式碼 ========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)
taba:thisFile new了嗎? 06/18 14:28
Eleganse:New Class1應該就會創出就有變數成員不是嗎? 06/18 15:01
Eleganse:還是VB的寫法是不同的,因為我的VB算是剛接觸 06/18 15:01
taba:會創出成員變數阿,不過你沒寫任何constructor編譯器會自動提 06/18 16:09
taba:供default constructor,它會把d設為0,把thisFile設為null 06/18 16:11
Eleganse:我竟然把VB也要建構函數這件事忘了 感謝提醒:) 06/18 16:15
taba:thisFile要new一下才會指向一個真正的物件,然後才能對它操作 06/18 16:16
Eleganse:Public ... As....這些碼在vb中不是new的意思嗎? 06/18 16:18
taba:c#:Class1 o = new class1(); vb.net:Dim o As New Class1() 06/18 16:24
Eleganse:感謝taba,我試了2天終於成功了:) 真的是實用的技術 06/18 17:06