2012-11-20 123 views
0

我做了VB.net中一個簡單的例子,它編譯並運行它:雖然它編譯罰款簡單的VB代碼沒有運行

Public Class Application 
    Sub calc1() 
     Dim sq as Integer 
     'uncommenting this loop keeps it from compiling for some reason 
     'For i as Integer = 1 to 1000 
     ' sq = i*i 
     'End For 
     Console.WriteLine("calculated squares") 
    End Sub 

    Public Shared Sub Main() 
     Dim startTime as DateTime 
     Dim endTime as DateTime 
     System.Console.WriteLine("Hello world!") 
     startTime = Now 
     calc1() 
     endTime = Now 
     Console.WriteLine(endTime.Subtract(startTime).TotalSeconds.ToString("0.0000")) 

    End Sub 
End Class 

,它提供了運行時一個奇怪的錯誤:

Unhandled Exception: System.InvalidProgramException: Invalid IL code in ThreadTest.Application:Main(): IL_0018: ldarg.0 


[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidProgramException: Invalid IL code in ThreadTest.Application:Main(): IL_0018: ldarg.0 
+0

當你取消對'For'循環有什麼編譯錯誤? – Ryan

+0

很多錯誤,第一個錯誤是'End For'。它表示預期的「Sub」,預期的結束語句,預期的「結束」,預期的語句結束。說後的所有行無效作爲標識符。 – NoBugs

+5

噢,它不是'End For',它是'Next'。 'while ... End While','Do ... Loop','For ... Next','For Each ... Next'。 – Ryan

回答

1
Main function is required in file like as below : 

    Public Class Application 
      Public Shared Sub calc1() 
       Dim sq As Integer 
       'uncommenting this loop keeps it from compiling for some reason 
       'For i as Integer = 1 to 1000 
       ' sq = i*i 
       'End For 
       Console.WriteLine("calculated squares") 
      End Sub 
     End Class 

     Sub Main() 
      Dim startTime As DateTime 
      Dim endTime As DateTime 
      System.Console.WriteLine("Hello world!") 
      startTime = Now 
      Application.calc1() 
      endTime = Now 
      Console.WriteLine(endTime.Subtract(startTime).TotalSeconds.ToString("0.0000")) 
      Dim inputFromConsole As String 
      Dim outputToConsole As String 
      Console.WriteLine("Type in a sentence and hit Enter:") 
      inputFromConsole = Console.ReadLine() 
      Console.WriteLine(outputToConsole) 


     End Sub 
3

而不是End For在您的評論循環中,您需要使用Next

其他部分是編譯器錯誤,而不是你的錯。 mono vb編譯器仍然需要一些工作。如果我不得不猜測,我會說這裏的問題要麼是它看到你的方法沒有真正的工作,並試圖對Datetime.Now的兩種用法做出不好的優化,或者它使用DateTime的錯誤重載。減去呼叫...但是,這些只是猜測。

但是,您應該做的是使用System.Diagnostics.Stopwatch類而不是日期時間值。

4

運行時錯誤是因爲您正在從共享函數(Main)調用實例函數(calc1)。

較新版本的VBNC(Mono的VB編譯器)的爲您提供了這個錯誤:

test.vb (16,15) : error VBNC30369: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. 

該修正代碼工作:

Public Class Application 
    Shared Sub calc1() 
     Dim sq as Integer 
     For i as Integer = 1 to 1000 
      sq = i*i 
     Next 
     Console.WriteLine("calculated squares") 
    End Sub 

    Public Shared Sub Main() 
     Dim startTime as DateTime 
     Dim endTime as DateTime 
     System.Console.WriteLine("Hello world!") 
     startTime = Now 
     calc1() 
     endTime = Now 
     Console.WriteLine(endTime.Subtract(startTime).TotalSeconds.ToString("0.0000")) 
    End Sub 
End Class 
+0

好抓!它應該是一個'模塊'... – Ryan

+0

那麼,'共享'相當於靜態?我想知道那是爲了什麼。 .NET中的Class和Module有什麼區別? (我對Java更熟悉,我很少完成VB.NET) – NoBugs

+0

是的,Shared與C#/ Java中的靜態相同。 VB中的模塊是一個所有成員都是共享的類(與C#中的靜態類相同)。 –