Laziale
Laziale

Reputation: 8225

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

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

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 pSources array of Integer, to find the appropriate value in the list of checkboxes and check the checkbox if the value match.

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:

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 pSources array?

Upvotes: 0

Views: 12057

Answers (4)

Craig Johnson
Craig Johnson

Reputation: 754

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

Upvotes: 0

Trevor
Trevor

Reputation: 8004

Here's what I would do myself...

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

 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

Upvotes: 1

mclark1129
mclark1129

Reputation: 7592

Two problems that I see:

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

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

2) I do not see a . joining Equals in 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 Equals then add a . between Converter.ToInt32(li.Value) and Equals. Your final code should be below:

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

Upvotes: 3

Russ
Russ

Reputation: 4163

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.

Upvotes: 1

Related Questions