Solved

Strange runtime error in VB

Posted on 2011-09-27
7
145 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
  • 3
  • 3
7 Comments
 
LVL 74

Expert Comment

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

Author Comment

by:dicecomputers
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
Thanks, the else was the problem. Also, thanks for the shortcut, I didn't know about that!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now