• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 156
  • Last Modified:

Strange runtime error in VB

One of my guys is in an intro to VB class at school right now and he wrote this code to let a user enter a month number and output the name of the month and the number of days in the month. I looked over it and can't find a problem but when you enter a valid month number it still considers it an invalid number. It must be in the "If/Then" statement, but not sure where.
Thanks
Option Strict On
Option Explicit On

Module Module1

    Sub Main()

        Dim ErMonth As String = Nothing
        Dim Month As Integer = Nothing
        Dim NMonth As Integer = Nothing
        Dim AMonth As String = Nothing
        Dim DMonth As Integer = Nothing
        Dim ErYear As String = Nothing
        Dim Year As Integer = Nothing

        Try
            Do While Not IsNumeric(ErMonth) Or CInt(ErMonth) >= 1 Or CInt(ErMonth) <= 12
                Console.WriteLine("Please enter a numeric value for a month")
                ErMonth = Console.ReadLine
                If Not IsNumeric(ErMonth) Or CInt(ErMonth) >= 1 Or CInt(ErMonth) <= 12 Then
                    Console.WriteLine("Please enter a numeric value only!")
                Else
                    Month = CInt(Console.ReadLine)
                End If
            Loop
        Catch ex As Exception
        End Try

        Try
            Do While Not IsNumeric(ErYear)
                Console.WriteLine("Please enter a year")
                ErYear = Console.ReadLine
                If Not IsNumeric(ErYear) Then
                    Console.WriteLine("Please enter a valid year!")
                Else
                    Year = CInt(Console.ReadLine)
                End If
            Loop
        Catch ex As Exception
        End Try

        Select Case Month
            Case 1
                AMonth = "January"
                DMonth = 31
            Case 2
                AMonth = "February"
                If Date.IsLeapYear(Year) Then
                    DMonth = 29
                Else
                    DMonth = 28
                End If
            Case 3
                AMonth = "March"
                DMonth = 31
            Case 4
                AMonth = "April"
                DMonth = 30
            Case 5
                AMonth = "May"
                DMonth = 31
            Case 6
                AMonth = "June"
                DMonth = 30
            Case 7
                AMonth = "July"
                DMonth = 31
            Case 8
                AMonth = "August"
                DMonth = 31
            Case 9
                AMonth = "September"
                DMonth = 30
            Case 10
                AMonth = "October"
                DMonth = 31
            Case 11
                AMonth = "Novermber"
                DMonth = 30
            Case 12
                AMonth = "December"
                DMonth = 31
        End Select


    End Sub

Open in new window

0
dicecomputers
Asked:
dicecomputers
  • 3
  • 3
1 Solution
 
käµfm³d 👽Commented:
In line 17, your Ors should be Ands.
0
 
dicecomputersAuthor Commented:
Sorry, I put in an old version of the code. Here is the final version
Option Strict On
Option Explicit On

Module Module1

    Sub Main()

        Dim ErMonth As String = Nothing
        Dim Month As Integer = Nothing
        Dim NMonth As Integer = Nothing
        Dim AMonth As String = Nothing
        Dim DMonth As Integer = Nothing
        Dim ErYear As String = Nothing
        Dim Year As Integer = Nothing

        Try
            Do While Not IsNumeric(ErMonth) Or CInt(ErMonth) >= 1 Or CInt(ErMonth) <= 12
                Console.WriteLine("Please enter a numeric value for a month")
                ErMonth = Console.ReadLine
                If Not IsNumeric(ErMonth) Or CInt(ErMonth) >= 1 Or CInt(ErMonth) <= 12 Then
                    Console.WriteLine("Please enter a numeric value only!")
                Else
                    Month = CInt(ErMonth)
                End If
            Loop
        Catch ex As Exception
        End Try

        Try
            Do While Not IsNumeric(ErYear)
                Console.WriteLine("Please enter a year")
                ErYear = Console.ReadLine
                If Not IsNumeric(ErYear) Then
                    Console.WriteLine("Please enter a valid year!")
                Else
                    Year = CInt(ErYear)
                End If
            Loop
        Catch ex As Exception
        End Try

        Select Case Month
            Case 1
                AMonth = "January"
                DMonth = 31
            Case 2
                AMonth = "February"
                If Date.IsLeapYear(Year) Then
                    DMonth = 29
                Else
                    DMonth = 28
                End If
            Case 3
                AMonth = "March"
                DMonth = 31
            Case 4
                AMonth = "April"
                DMonth = 30
            Case 5
                AMonth = "May"
                DMonth = 31
            Case 6
                AMonth = "June"
                DMonth = 30
            Case 7
                AMonth = "July"
                DMonth = 31
            Case 8
                AMonth = "August"
                DMonth = 31
            Case 9
                AMonth = "September"
                DMonth = 30
            Case 10
                AMonth = "October"
                DMonth = 31
            Case 11
                AMonth = "Novermber"
                DMonth = 30
            Case 12
                AMonth = "December"
                DMonth = 31
        End Select

        Console.WriteLine("The month is: " & AMonth & vbCrLf & "The number of days in this month are: " & DMonth)

    End Sub

End Module

Open in new window

0
 
dicecomputersAuthor Commented:
If I change the or's to and's, it will only trigger when all cases are true. I need it to trigger if any of the cases are true
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Jacques Bourgeois (James Burger)PresidentCommented:
Tell you guy that he should learn to exploit the framework. Whenever he wants to do something trivial, there is a way in the system to do it fast, easy and effitiently
Dim month As Integer = 10 'Pick any month
Dim year As Integer = 2011          'Pick any year

Console.WriteLine(New Date(1, month, 1).ToString("MMMM"))
Console.WriteLine(Date.DaysInMonth(Today.Year, month))
Console.WriteLine("The month is: " & New Date(1, month, 1).ToString("MMMM")& vbCrLf & "The number of days in this month are: " & Date.DaysInMonth(Today.Year, month))

Open in new window

0
 
käµfm³d 👽Commented:
Sorry, I was incorrect. I looked it over a bit closer and there are a couple of issues:

1)  Unless you are starting your months at 2, in line 17, use OrElse for its short-circuiting effect, and check that the value is less than 1 and greater than 12, not what you are currently doing.

2) Remove the If block because your loop will cause the user to have to enter values twice for the month each time the check fails.

3) For the year, same as #2.

Here is what I might do:

Option Strict On
Option Explicit On

Module Module1

    Sub Main()

        Dim ErMonth As String = Nothing
        Dim Month As Integer = Nothing
        Dim NMonth As Integer = Nothing
        Dim AMonth As String = Nothing
        Dim DMonth As Integer = Nothing
        Dim ErYear As String = Nothing
        Dim Year As Integer = Nothing

        Dim errMsg As String = "Please enter a numeric value for a month"

        Try
            Do While Not IsNumeric(ErMonth) OrElse CInt(ErMonth) < 1 OrElse CInt(ErMonth) > 12
                Console.WriteLine(errMsg)
                ErMonth = Console.ReadLine
                errMsg = "Please enter a numeric value only!"
            Loop

            Month = CInt(ErMonth)
        Catch ex As Exception
        End Try

        Try
            errMsg = "Please enter a year"

            Do While Not IsNumeric(ErYear)
                Console.WriteLine(errMsg)
                ErYear = Console.ReadLine
                errMsg = "Please enter a valid year!"
            Loop

            Year = CInt(ErYear)
        Catch ex As Exception
        End Try

        Select Case Month
            Case 1
                AMonth = "January"
                DMonth = 31
            Case 2
                AMonth = "February"
                If Date.IsLeapYear(Year) Then
                    DMonth = 29
                Else
                    DMonth = 28
                End If
            Case 3
                AMonth = "March"
                DMonth = 31
            Case 4
                AMonth = "April"
                DMonth = 30
            Case 5
                AMonth = "May"
                DMonth = 31
            Case 6
                AMonth = "June"
                DMonth = 30
            Case 7
                AMonth = "July"
                DMonth = 31
            Case 8
                AMonth = "August"
                DMonth = 31
            Case 9
                AMonth = "September"
                DMonth = 30
            Case 10
                AMonth = "October"
                DMonth = 31
            Case 11
                AMonth = "Novermber"
                DMonth = 30
            Case 12
                AMonth = "December"
                DMonth = 31
        End Select

        Console.WriteLine("The month is: " & AMonth & vbCrLf & "The number of days in this month are: " & DMonth)

        Console.ReadKey()
    End Sub

End Module

Open in new window

0
 
käµfm³d 👽Commented:
Unless you are starting your months at 2, in line 17, use OrElse for its short-circuiting effect, and check that the value is less than 1 and greater than 12, not what you are currently doing.

I think I should rephrase #1:

In line 17, use OrElse for its short-circuiting effect, and, unless you are starting your months at 2, check that the value is less than 1 and greater than 12, not what you are currently doing.
0
 
dicecomputersAuthor Commented:
Thanks, the else was the problem. Also, thanks for the shortcut, I didn't know about that!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now