vb.net 如何解决 VB 'If` 语句的“预期语句结束”编译器错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29679044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:02:28  来源:igfitidea点击:

How do I solve an 'End of statement expected' compiler error for a VB 'If` statement?

c#vb.netvb.net-2010c#-to-vb.net

提问by Laziale

I'm not too good in VB.net, mostly working with C#, and I have the following foreachloop:

我不太擅长 VB.net,主要使用 C#,我有以下foreach循环:

Dim pSources() As Integer = {}
pSources = SCCC.GetSources(SysCompany, SysUser, ccHeaderId)

Try

    For Each intSelect As Integer In pSources 

        For Each li As ListItem In chkSources.Items 

            If Convert.ToInt32(li.Value) Equals(intSelect)
                li.Selected = True
            End If

        Next

    Next

Catch ex As Exception

End Try

I would like to check for each item in the pSourcesarray of Integer, to find the appropriate value in the list of checkboxes and check the checkbox if the value match.

我想检查 的pSources数组中的每个项目Integer,以在复选框列表中找到适当的值,如果值匹配则选中复选框。

With the code I have at this moment I'm getting error on the line where I do the if comparison, and this is the error:

使用我目前拥有的代码,我在进行 if 比较的行中出现错误,这是错误:

End of statement expected

预期语句结束

How can I fix this?

我怎样才能解决这个问题?

Or maybe better, how I can use a LINQ statement which will check for the value and then check the checkboxes if the value is contained in the pSourcesarray?

或者也许更好,我如何使用 LINQ 语句来检查该值,然后检查该值是否包含在pSources数组中的复选框?

采纳答案by ???ěxě?

Here's what I would do myself...

这是我自己会做的...

The below code checks to make sure pSourcesis something and also has something in it. The Integer.TryParsewill not throw an exception if it can't parse and will short circuit before trying to do a comparison...

下面的代码检查以确保pSources有东西并且里面也有东西。在Integer.TryParse试图做一个比较之前将不会抛出一个异常,如果它不能解析,并会短路...

 Dim pSources As New List(Of Integer)
 Dim intNumber As Integer = 0
 pSources = SCCC.GetSources(SysCompany, SysUser, ccHeaderId)

 Try
    If pSources IsNot Nothing AndAlso pSources.Count > 0 Then
      For Each intSelect In pSources 
       For Each li As ListItem In chkSources.Items 
        If Integer.TryParse(li.Value.ToString, intNumber) AndAlso (intNumber = intSelect) Then
            li.Selected = True
        End If
       Next
      Next 
    End If

 Catch ex As Exception
  'Handle your exception...
 End Try

回答by mclark1129

Two problems that I see:

我看到的两个问题:

1) Like Russ points out, you need a Thenstatement after If. In VB, the syntax is

1) 就像 Russ 指出的那样,您需要ThenIf. 在VB中,语法是

If <boolean statement> Then
    <Some Code>
End If

2) I do not see a .joining Equalsin your boolean statement. This is just invalid syntax. Like was suggested in the comments, you can use the =operator here for more clarity. If you still want to use Equalsthen add a .between Converter.ToInt32(li.Value)and Equals. Your final code should be below:

2)我在你的布尔语句中没有看到.加入Equals。这只是无效的语法。就像评论中建议的那样,您可以在=此处使用运算符以更清晰。如果您仍然想使用,请在和之间Equals添加一个。您的最终代码应如下所示:.Converter.ToInt32(li.Value)Equals

Dim pSources() As Integer = {}
pSources = SCCC.GetSources(SysCompany, SysUser, ccHeaderId)

Try

    For Each intSelect As Integer In pSources 

        For Each li As ListItem In chkSources.Items 

            If Convert.ToInt32(li.Value).Equals(intSelect) Then
                li.Selected = True
            End If

        Next

    Next

Catch ex As Exception

End Try

回答by Russ

Your IF statement requires a "THEN" at the end of it. There are some decent C# to VB.NET conversion applications online (such as this code converter from Telerik)--you might try some of those to help you gain familiarity with VB.NET.

你的 IF 语句要求在它的末尾有一个“THEN”。网上有一些不错的 C# 到 VB.NET 转换应用程序(例如来自 Telerik 的这个代码转换器)——您可以尝试其中的一些来帮助您熟悉 VB.NET。

回答by Craig Johnson

This:

这个:

    Dim pSources = SCCC.GetSources(SysCompany, SysUser, ccHeaderId)

    Dim val = 0
    For l = 0 To chkSources.Items.Count - 1
        chkSources.SetSelected(l, Integer.TryParse(chkSources.Items(l).ToString, val) AndAlso pSources.Contains(val))
    Next