# Visual Studio hates my code



## mab1376 (Nov 5, 2009)

I'm doing a project for school, basically in VB.net I have to input 2 numbers, list all the numbers between the 2, and check for multiples of 2, below is my code that I can't figure out why Visual Studio won't let me build it, keeps saying " 'If' must end with matching 'End If' "

Any ideas.

```
'checks for multiples of 2 and lists them.
        Dim i As Integer
        ListBox1.Items.Clear()
        getinput()
        For i = M To A' the 2 variables from the sub "getinput"
            If (i Mod 2) = 0 Then
                say(i) 'say is a sub to output text to the listbox.
            Else
            next i
            End If
```

got it working


```
Dim i As Integer
        ListBox1.Items.Clear()
        getinput()
        Do While i <= A
            For i = M To A
                If i Mod 2 = 0 Then
                    say(i)
                End If
            Next i
        Loop
```


----------



## Kreij (Nov 5, 2009)

Your putting your for loop into your conditional if statement. Don't do that.


```
For i = M to A
  if ( i mod 2 = 0) then say(i)
Next i
```


----------



## mab1376 (Nov 5, 2009)

I got it working (edit above in original post)

do you think the logic is overly complicated?


----------



## Kreij (Nov 5, 2009)

You don't need the while loop since the for loop will end when the condition is met (i = A).


----------



## mab1376 (Nov 5, 2009)

thanks, i'll give it a shot.


----------



## Kreij (Nov 5, 2009)

Actually, if you need to list the numbers even if they are not multiples of 2 then you want to add an else statement.

```
for (i = M to A)
  if (i mod 2 = 0) say(i + "is a muliple of 2")
  else say(i)
next i
```


----------



## mab1376 (Nov 5, 2009)

basically what I've done it made multiple check boxes and when all of them are checked, it will only find primes.


```
Private Sub btnMul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMul.Click

        'checks for checked check boxes and executes
        'subs corresponging to the checkbox.
        With Me
            If CheckBoxMul2.Checked Then
                Call multipleof2()
            End If
            If CheckBoxMul3.Checked Then
                Call multipleof3()
            End If
            If CheckBoxMul5.Checked Then
                Call multipleof5()
            End If
            If CheckBoxMul7.Checked Then
                Call multipleof7()
            End If
            If CheckBoxMul11.Checked Then
                Call multipleof11()
            End If
        End With

    End Sub
    Sub multipleof2()

        'checks for multiples of 2 and lists them.
        Dim i As Integer
        ListBox1.Items.Clear()
        getinput()
        For i = M To A
            If i Mod 2 = 0 Then
                say(i)
            End If
        Next i

    End Sub

    Sub multipleof3()

        'checks for multiples of 3 and lists them.
        Dim i As Integer
        ListBox1.Items.Clear()
        getinput()
        For i = M To A
            If i Mod 3 = 0 Then
                say(i)
            End If
        Next i

    End Sub

    Sub multipleof5()

        'checks for multiples of 5 and lists them.
        Dim i As Integer
        ListBox1.Items.Clear()
        getinput()
        For i = M To A
            If i Mod 5 = 0 Then
                say(i)
            End If
        Next i

    End Sub

    Sub multipleof7()

        'checks for multiples of 7 and lists them.
        Dim i As Integer
        ListBox1.Items.Clear()
        getinput()
        For i = M To A
            If i Mod 7 = 0 Then
                say(i)
            End If
        Next i


    End Sub

    Sub multipleof11()

        'checks for multiples of 11 and lists them.
        Dim i As Integer
        ListBox1.Items.Clear()
        getinput()
        For i = M To A
            If i Mod 11 = 0 Then
                say(i)
            End If
        Next i
    End Sub
```


I just tested this code and it works from 0 to 1000


----------



## Kreij (Nov 5, 2009)

You're doing a great job. There is no wrong way to code something. 
What is the end goal of the assignement? To print multiples or find primes?


----------



## mab1376 (Nov 5, 2009)

The print multiples if selected check boxes or inevitably find primes if all are checked.

Multiples of 2,3,5,7,11

Thanks for all the help! 

Oddly enough I find assembly coding easier, mainly because I pay attention more in class lol.


----------



## Kreij (Nov 5, 2009)

Interestingly enough is that to find primes you only need to check for multiples of 2,3 and 5. Everything else comes out in the wash. But if you need to see multiples of 7 and 11, then you do need some extra checking.

Anyway, keep up the good work and you know there will always be someone here on TPU to help you with coding if you need it


----------



## mab1376 (Nov 9, 2009)

I was going through my project again and I realized when multiple check boxes are checked then they run in sequence rather than giving one output, i.e. for multiples of 2, 3, and 5 with a range from 10 to 20. (10, 12, 14, 16, 18, 20, 12, 15, 18, 10, 15, 20) when the output should be only multiples of 2, 3, and 5 in one list.


```
With Me
            If CheckBoxMul2.Checked Then
                Call multipleof2()
            End If
            If CheckBoxMul3.Checked Then
                Call multipleof3()
            End If
            If CheckBoxMul5.Checked Then
                Call multipleof5()
            End If
            If CheckBoxMul7.Checked Then
                Call multipleof7()
            End If
            If CheckBoxMul11.Checked Then
                Call multipleof11()
            End If
        End With
```

Any ideas on how to do this?

an example sub on the computation is:


```
Dim i As Integer
        getinput()
        For i = M To A
            If i Mod 3 = 0 Then
                say(i)
            End If
        Next i
```


----------



## Deleted member 3 (Nov 9, 2009)

If the first number is the same as number you want a multiple of you can just use (assuming 2)


```
for i = number1 to number2 step 2
        msgbox(i)
next
```


----------



## Kreij (Nov 11, 2009)

I think what you may want to do is create a loop in the range of numbers, and then drop through the tests and continue the loop it any condition is met. Have your "multiple-of" methods return a boolean value indicating if the test found a multiple.

```
for (int i = StartValue; i <= EndValue; i++)
{
    if (MultipleOfTwo(i)) 
    {
        say (i + "is a multiple of two");
        continue; // Run next number
    }
    if (MultipleOfThree(i))
    {
        say(i + "is a multiple of three");
        continue;
    }
    ....
}
```


----------



## mab1376 (Nov 12, 2009)

ah, i have what i need to get to working properly.

Thanks!


----------



## Kreij (Nov 12, 2009)

Great. Glad to help.
Post your completed code, so we can take a look at it and make suggestions that may help you in future projects.
Everyone has their own ideas on how to code, and you don't have to take any advice, but you will always learn by reviewing other peoples' code.


----------

