Visual Basic +Visual Studio 2017 Pro Help please

Good morning, I am currently working through every problem in the Visual Studio book to learn Visual Basic language. My program should take a intFrom to intTo number and then display only the even numbers between the entered numbers, such as I enter 1 to 9 and should display 2, 4, 6 ,8 but my loop is processing it 9 times instead. Please help me figure out what I am doing wrong. this is a modified code from the original project I created. Code is below.

’ Name: Numbers Project
’ Purpose: Display a list of numbers using various loops.
’ Programmer: Lisa Kordich on Aug. 20, 2019

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain
Private Sub btnForNext_Click(sender As Object, e As EventArgs) Handles btnForNext.Click
’ Display a list of numbers.

    Dim intFrom As Integer
    Dim intTo As Integer

    Integer.TryParse(txtFrom.Text, intFrom)
    Integer.TryParse(txtTo.Text, intTo)
    lstNumbers.Items.Clear()

    ' intNum is the counter 
    For intNum As Integer = intFrom To intTo
        ' To display even numbers between intFrom and intTo

        If intFrom Mod 2 = 0 Then
            intFrom += 2
            'lstNumbers.Items.Add(intFrom)
        Else intFrom += 1 'intFrom Mod 2 = 1 Then
        End If
        lstNumbers.Items.Add(intFrom)

        'lstNumbers.Items.Add(intNum) ' Adds numbers in the lstNumbers  box

    Next intNum ' Adds 1 to the intNum counter

End Sub

Private Sub btnDoPretest_Click(sender As Object, e As EventArgs) Handles btnDoPretest.Click
    ' Display a list of numbers.

    Dim intFrom As Integer
    Dim intTo As Integer

    Integer.TryParse(txtFrom.Text, intFrom)
    Integer.TryParse(txtTo.Text, intTo)
    lstNumbers.Items.Clear()
    Do While intFrom <= intTo
        lstNumbers.Items.Add(intFrom)
        intFrom += 1
    Loop
End Sub

Private Sub btnDoPosttest_Click(sender As Object, e As EventArgs) Handles btnDoPosttest.Click
    ' Display a list of numbers.

    Dim intFrom As Integer
    Dim intTo As Integer

    Integer.TryParse(txtFrom.Text, intFrom)
    Integer.TryParse(txtTo.Text, intTo)
    lstNumbers.Items.Clear()

    Do
        If intFrom <= intTo Then
            lstNumbers.Items.Add(intFrom)
            intFrom += 1
        End If

    Loop While intFrom <= intTo

End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

Private Sub txtFrom_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtFrom.KeyPress
    ' Accept only numbers and the Backspace key.

    If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
        e.Handled = True
    End If
End Sub

Private Sub txtTo_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtTo.KeyPress
    ' Accept only numbers and the Backspace key.

    If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
        e.Handled = True
    End If
End Sub

Private Sub txtFrom_Enter(sender As Object, e As EventArgs) Handles txtFrom.Enter
    txtFrom.SelectAll()
End Sub

Private Sub txtTo_Enter(sender As Object, e As EventArgs) Handles txtTo.Enter
    txtTo.SelectAll()
End Sub

Private Sub txtFrom_TextChanged(sender As Object, e As EventArgs) Handles txtFrom.TextChanged
    lstNumbers.Items.Clear()
End Sub

Private Sub txtTo_TextChanged(sender As Object, e As EventArgs) Handles txtTo.TextChanged
    lstNumbers.Items.Clear()
End Sub

End Class

Hi @kordich

Codecademy doesn’t currently offer a course on Visual Basic, so that might limit the amount of help you’ll be able to get here.

Since you’ve asked, though, I presume you’re struggling with this bit of the code:

Private Sub btnForNext_Click(sender As Object, e As EventArgs) Handles btnForNext.Click
    ' Display a list of numbers
    Dim intFrom As Integer
    Dim intTo As Integer

    Integer.TryParse(txtFrom.Text, intFrom)
    Integer.TryParse(txtTo.Text, intTo)
    lstNumbers.Items.Clear()

    ' intNum is the counter 
    For intNum As Integer = intFrom To intTo
        ' To display even numbers between intFrom and intTo

        If intFrom Mod 2 = 0 Then
            intFrom += 2
            'lstNumbers.Items.Add(intFrom)
        Else intFrom += 1 'intFrom Mod 2 = 1 Then
        End If
        lstNumbers.Items.Add(intFrom)

        'lstNumbers.Items.Add(intNum) ' Adds numbers in the lstNumbers  box

    Next intNum ' Adds 1 to the intNum counter

End Sub

Your problem is, I think, a misunderstanding of how the For...Next structure works in VB. Here’s the docs on the subject.

VB only evaluates For intNum As Integer = intFrom To intTo once, when the loop begins. These determine the values which the loop will iterate over, and are fixed once the loop starts. You can change them from inside the loop, as you have done, but they will not affect the iteration values.

At the start of each iteration, VB increments the counter which is intNum in your code and compares it to the end condition which is intTo. If the value is the same or less, the loop runs.

Your loop will run 9 times because there are 9 valid values of intNum between 1 and 9.

It looks like you’re trying to jump ahead by two when you encounter the first even number, but you’re incrementing intFrom which won’t affect anything because intFrom is essentially pointless once you’re in the loop as already mentioned.

From a procedural point of view, your coding is wrong. You can fix it, like so:

Private Sub btnForNext_Click(sender As Object, e As EventArgs) Handles btnForNext.Click
    ' Display a list of numbers
    Dim intFrom As Integer
    Dim intTo As Integer

    Integer.TryParse(txtFrom.Text, intFrom)
    Integer.TryParse(txtTo.Text, intTo)
    lstNumbers.Items.Clear()

    ' intNum is the counter 
    For intNum As Integer = intFrom To intTo

        If intNum Mod 2 = 0 Then ' intNum is the current value of the counter, so if it's even...
            lstNumbers.Items.Add(intNum) ' we add it to the list! if it's not, we can ignore it and do nothign
        End If

    Next intNum ' Adds 1 to the intNum counter to check the next number.

End Sub

Hope that helps. :slight_smile:

4 Likes

Thank you so much for your help. I have been trying to learn this myself to get caught up for the VB 2 class because I took VB 1 3.5 years ago so I am trying to run through the new book by doing each example and every exercise but this one had me lost. The tutors could not help either.

1 Like

No problem, though I have to say that

is a little concerning…

I totally agree. When I asked for help they told me that they were only assigned number 1 through 3 of each chapter. That is all the for beginner stuff. I shook my head in disbelief as I am taking this class with one goal of a specific program I am going to try to tackle and want to know the language. Doing number 1 to 3 is not going to give me knowledge. PS. I changed code to what you put and it did not work for me. I did get it to print correctly on the odd numbers but the even numbers is still giving me one extra. I have decided to move on and come back to it later in hopes that later I will have a better understanding.

It’s possible I made a mistake, as I haven’t done anything with VB in a considerable time.

I didn’t want to make a WinForms app, so I’ve created a console application to see if I could do what you’re trying to achieve. I arrived at the following:

Imports System

Module Program

    Sub TestFunction(iFrom As Integer, iTo As Integer)
        Dim StartNum As Integer = iFrom
        Dim EndNum As Integer = iTo

        Dim ListOfEvens As List(Of Integer) = New List(Of Integer)
        Dim ListOfOdds As List(Of Integer) = New List(Of Integer)

        For CurNum As Integer = StartNum To EndNum
            'Console.WriteLine(CurNum) ' Output CurNum for monitoring, if required
            If CurNum Mod 2 = 0 Then
                ListOfEvens.Add(CurNum)
            ElseIf CurNum Mod 2 = 1 Then
                ListOfOdds.Add(CurNum)
            End If
        Next CurNum

        Console.WriteLine("Here are the even numbers:")

        For Each Item As Integer In ListOfEvens
            Console.Write(Item)
            Console.Write(", ")
        Next Item

        Console.WriteLine("")
        Console.WriteLine("Here are the odd numbers:")

        For Each Item As Integer In ListOfOdds
            Console.Write(Item)
            Console.Write(", ")
        Next Item

    End Sub
    Sub Main(args As String())
        Console.WriteLine("--- Number Cruncher ---")

        TestFunction(1, 9)

    End Sub
End Module

I get the following console output:

--- Number Cruncher ---
Here are the even numbers:
2, 4, 6, 8,
Here are the odd numbers:
1, 3, 5, 7, 9,

One would assume that, if they knew enough of the language to teach it formally, they’d be able to help regardless of whether they were “assigned” that section or not.

2 Likes

Thank you. I will try to apply this later. I have moved on to the rest in the chapter. They may only assign 1 through 3 but I want to learn so I am doing all that time will permit.

In re: One would assume… I agree. I am very doubtful that the teacher can even help. when I was taking C++ one and two I had to go to a different teacher at a different school just to get past the class because my teacher was unable to help me. It’s a head scratcher for sure.

I will come back later and let you know how I panned out. Thank you for your help.