[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Strange runtime error in VB

Posted on 2011-09-27
7
Medium Priority
?
154 Views
Last Modified: 2012-05-12
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
Comment
Question by:dicecomputers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36711164
In line 17, your Ors should be Ands.
0
 

Author Comment

by:dicecomputers
ID: 36711165
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
 

Author Comment

by:dicecomputers
ID: 36711173
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 40
ID: 36711259
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 36711277
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36711286
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
 

Author Closing Comment

by:dicecomputers
ID: 36711352
Thanks, the else was the problem. Also, thanks for the shortcut, I didn't know about that!
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question