0

我在 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   
4

3 回答 3

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
于 2012-11-20T10:45:45.393 回答
3

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

它的另一部分是编译器错误,而不是你的错。单声道 vb 编译器仍然需要一些工作。如果我不得不猜测,我会说这里的问题要么是它看到你的方法没有真正的工作,并试图对你对 Datetime.Now 的两次使用进行糟糕的优化,要么它对 DateTime 使用了错误的重载.减去电话......但同样,这些只是猜测。

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

于 2012-11-20T04:42:21.867 回答
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
于 2012-11-20T04:36:14.190 回答