如何为VB'If`语句解决'期望语句结束'编译器错误?

时间:2015-04-16 15:16:07

标签: c# vb.net vb.net-2010 c#-to-vb.net

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

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

使用我目前所拥有的代码,我在进行if比较时遇到错误,这就是错误:

  

预期结束语

我该如何解决这个问题?

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

4 个答案:

答案 0 :(得分:3)

我看到两个问题:

1)就像Russ指出的那样,在Then之后需要If语句。在VB中,语法是

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

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

答案 1 :(得分:1)

您的IF声明需要&#34;那么&#34;在它的最后。在线有一些不错的C#到VB.NET转换应用程序(比如这个code converter from Telerik) - 您可以尝试其中一些来帮助您熟悉VB.NET。

答案 2 :(得分:0)

这就是我自己做的事情......

以下代码检查以确保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

答案 3 :(得分:0)

此:

    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