简单的VB代码没有运行

时间:2012-11-20 03:58:31

标签: vb.net mono monodevelop il invalidprogramexception

我在VB.net中做了一个简单的例子,编译并运行它:

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   

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:3)

您的注释循环中不需要End For,而是需要使用Next

它的另一部分是编译器错误,而不是你的错。 mono vb编译器仍然需要一些工作。如果我不得不猜测,我会说这里的问题是,它看到你的方法没有真正的工作,并尝试使用Datetime.Now的两次使用进行错误的优化,或者它使用DateTime的错误重载.Subtract call ...但是,这些只是猜测。

但是,您应该使用System.Diagnostics.Stopwatch类而不是Datetime值。

答案 2 :(得分: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