【问题标题】:Simple VB code is not running简单的 VB 代码没有运行
【发布时间】:2012-11-20 03:58:31
【问题描述】:

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

【问题讨论】:

  • 取消注释For循环时出现什么编译错误?
  • 很多错误,第一个错误在End For。它说预期Sub,预期语句结束,预期End,预期语句结束。后面的所有行都说 Not valid as identifier.
  • 哦,不是End For,是Next。 While...End While, Do...Loop, For...Next, For Each...Next.
  • 谢谢,运行的时候还是报错。
  • 参数可能有问题,你传入的是Console.WriteLine(endTime.Subtract(startTime).TotalSeconds.ToString("0.0000"))

标签: vb.net mono monodevelop il invalidprogramexception


【解决方案1】:

运行时错误是因为您从共享函数 (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

【讨论】:

  • 那么,shared 等价于静态?我想知道那是为了什么。 .NET 中的类和模块有什么区别? (我对Java比较熟悉,VB.NET很少用过)
  • 是的,Shared 与 C#/Java 中的 static 相同。 VB 中的模块是所有成员共享的类(与 C# 中的静态类相同)。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:
    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
    

    【讨论】:

      猜你喜欢
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 2021-07-20
      • 2014-08-31
      • 2014-12-02
      • 1970-01-01
      • 2017-12-19
      相关资源
      最近更新 更多