【问题标题】:Declaration Expected vb.net声明预期 vb.net
【发布时间】:2020-01-20 20:25:12
【问题描述】:

我是编码新手,我真的不知道为什么我会收到一条消息,上面写着 “预期声明” 在底部一行的 greenCount 部分这段代码。该代码几乎解释了我想要什么,但无论如何我有三个变量,totalCountredCountgreenCount

greenCount 的值应该是totalCount 减去redCount。 有人可以帮忙吗?

Public Class Form1

Dim totalCount As Integer = 44
Dim redCount As Integer
Dim greenCount As Integer greenCount = totalCount - redCount

【问题讨论】:

    标签: vb.net visual-studio


    【解决方案1】:

    编译器期望 greenCount = totalCount - redcount 在 Sub 或 Function 中。那就是它认为缺失的“声明”。

    您需要更多类似的东西来让编译器满意...

    Module Program
        Sub Main(args As String())
            Dim f As New Form1
            Call f.ChangeGreenCount(42)
        End Sub
    End Module
    
    
    Public Class Form1
    
        Dim totalCount As Integer = 44
        Dim greenCount As Integer
    
        Public Sub ChangeGreenCount(redCount As Integer)
            greenCount = totalCount - redCount
        End Sub
    End Class
    

    【讨论】:

      【解决方案2】:

      有三种可能的方法来纠正这个错误,你会遇到第一种。

      • 将非声明性语句移至过程主体。
      • 以适当的声明关键字开始声明。
      • 确保声明关键字没有拼写错误。

      也许你需要一个教程来学习vb.net,同时编码,请参考official document

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-09
        • 2018-06-12
        • 1970-01-01
        • 2022-11-14
        • 1970-01-01
        相关资源
        最近更新 更多