Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › VB.NET - Checking if a number is within a range function
New Posts  All Forums:Forum Nav:

VB.NET - Checking if a number is within a range function

post #1 of 2
Thread Starter 

I'm trying to create a program that will take a user's average on a few tests, and during entry of the tests, create a running average, and in the end, display a message box that will tell the user their name, as well as their final average.


Here is my current code:

Warning: 120 lines (Click to show)

 

 

Public Class Average

    Dim Average As Double

    Dim TestOne As Double

    Dim TestTwo As Double

    Dim TestThree As Double

    Dim TestFour As Double

    Dim TestFive As Double

    Dim NumberofTests As Integer

    Dim Name As String

    Dim TestCheck As Boolean

    Dim FinishedFirst As Boolean

    Dim FinishedSecond As Boolean

    Dim FinishedThird As Boolean

    Dim FinishedFourth As Boolean

    Dim FinishedFifth As Boolean

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

        Name = txtName.Text

        NumberofTests = Val(cboNumberofTests.Text)


        If Name = "" Then

            MessageBox.Show("You've made an invalid name entry!", "Invalid Name Entry", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Else

            Do While NumberofTests >= 2 And NumberofTests <= 5

                Select Case NumberofTests

                    Case Is > 5

                        MessageBox.Show("Only 5 tests maximum can be calculated!", "More Than Five Tests", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    Case Is >= 2

                        Do While FinishedFirst = False

                            TestOne = Val(InputBox("Enter the mark of the first test", "First Test Mark", "", 20, 20))

                            If TestOne <= 0 Or TestOne > 100 Then

                                MessageBox.Show("You've made an invalid entry!", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Error)

                            Else

                                Average = CalculateAverage(TestOne, TestTwo, TestThree, TestFour, TestFive, 1)

                                Call DisplayResult(Average)

                                FinishedFirst = True

                            End If

                        Loop

                        Do While FinishedSecond = False

                            TestTwo = Val(InputBox("Enter the mark of the second test", "Second Test Mark", "", 20, 20))

                            If TestTwo <= 0 Or TestTwo > 100 Then

                                MessageBox.Show("You've made an invalid entry!", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Error)

                            Else

                                Average = CalculateAverage(TestOne, TestTwo, TestThree, TestFour, TestFive, 2)

                                Call DisplayResult(Average)

                                FinishedSecond = True

                                If NumberofTests = 2 Then

                                End If

                            End If

                        Loop

                        If NumberofTests >= 3 Then

                            Do While FinishedThird = False

                                TestThree = Val(InputBox("Enter the mark of the third test", "Third Test Mark", "", 20, 20))

                                If TestThree <= 0 Or TestThree > 100 Then

                                    MessageBox.Show("You've made an invalid entry!", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Error)

                                Else

                                    FinishedThird = True

                                    Average = CalculateAverage(TestOne, TestTwo, TestThree, TestFour, TestFive, 3)

                                    Call DisplayResult(Average)

                                End If

                            Loop

                        End If

                        If NumberofTests >= 4 Then

                            Do While FinishedFourth = False

                                TestFour = Val(InputBox("Enter the mark of the fourth test", "Fourth Test Mark", "", 20, 20))

                                If TestFour <= 0 Or TestFour > 100 Then

                                    MessageBox.Show("You've made an invalid entry!", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Error)

                                Else

                                    Average = CalculateAverage(TestOne, TestTwo, TestThree, TestFour, TestFive, 4)

                                    Call DisplayResult(Average)

                                    FinishedFourth = True

                                End If

                            Loop

                        End If

                        If NumberofTests = 5 Then

                            Do While FinishedFifth = False

                                TestFive = Val(InputBox("Enter the mark of the fifth test", "Fifth Test Mark", "", 20, 20))

                                If TestFive <= 0 Or TestFive > 100 Then

                                    MessageBox.Show("You've made an invalid entry!", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Error)

                                Else

                                    FinishedFifth = True

                                    Average = CalculateAverage(TestOne, TestTwo, TestThree, TestFour, TestFive, 5)

                                    Call DisplayResult(Average)

                                End If

                            Loop

                        End If

                    Case Else

                        MessageBox.Show("2 tests minimum are needed to find an average.", "Less Than Two Tests", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End Select

                If NumberofTests >= 2 And NumberofTests <= 5 Then

                    MessageBox.Show(Name & " had an average of " & lblRunningAverage.Text & "% over " & NumberofTests & " tests.")

                End If

                NumberofTests = 0

            Loop

            End If

    End Sub

    Function CalculateAverage(ByVal TestOne As Double, ByVal TestTwo As Double, ByVal TestThree As Double, ByVal TestFour As Double, ByVal TestFive As Double, ByVal NumberofTests As Integer) As Double

        Return Math.Round((TestOne + TestTwo + TestThree + TestFour + TestFive) / NumberofTests, 1)

    End Function

    Sub DisplayResult(ByVal Average As Double)

        lblRunningAverage.Text = Average.ToString

    End Sub

    Private Sub ResetToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ResetToolStripMenuItem.Click, btnCancel.Click

        txtName.Text = ""

        cboNumberofTests.Text = ""

        lblRunningAverage.Text = ""

    End Sub

    Function CheckValid(ByVal Test As Double) As Boolean

        If Test >= 0 And Test <= 100 Then

            CheckValid = True

        Else

            CheckValid = False

            MessageBox.Show("You've made an invalid entry!", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If

    End Function

    Sub DisplayValid(ByVal CheckValid As Boolean)

        TestCheck = CheckValid

        MessageBox.Show(CheckValid & " is equal to " & TestCheck)

    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click

        Me.Close()

    End Sub

End Class

 

 

I'm currently working on the CheckValid function, and it looks like this:

    Function CheckValid(ByVal Test As Double) As Boolean

        If Test >= 0 And Test <= 100 Then

            CheckValid = True

        Else

            CheckValid = False

            MessageBox.Show("You've made an invalid entry!", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If

    End Function

    Sub DisplayValid(ByVal CheckValid As Boolean)

        TestCheck = CheckValid

        MessageBox.Show(CheckValid & " is equal to " & TestCheck)

 

 

The last line was just me trying to test, to make sure the function was working correctly, but unfortunately I never got that far. :/

 

I'm wondering how I would make a function to check for values and make sure they are <100 but also >0, and then how I'd set up the sub along with it. When I try to Call DisplayValid, by typing

 

Call DisplayValid()

it will only accept a true/false value in the brackets, which isn't what I want. I would like it to calculate a true/false value and then it would use that value in the main program to determine what to do next.

 

Any help would be appreciated with this.

Number Cruncher
(16 items)
 
HTPC
(11 items)
 
750TIMINER2
(15 items)
 
CPUMotherboardGraphicsRAM
i7 2600k ASUS Sabertooth Z77 Sapphire 290 Tri-x 4GB OC CORSAIR Vengeance 16GB 
Hard DriveHard DriveHard DriveOptical Drive
Intel 520 120 GB SSD Seagate Barracuda 1 TB 250GB Samsung 840 EVO SSD LG Blu-Ray Lightscribe Combo Burner 
CoolingOSMonitorPower
Swiftech H320 (CPU only) Windows 7 Ultimate 27" Shimian 1440p Monitor 1000W Corsair PSU 
CaseMouseMouse PadAudio
800D custom painted and fixed up! 15 year old Microsoft Custom Minecraft Mousepad Logitech 2.1 Speakers (wub wubbity wub wub) 
CPUMotherboardGraphicsRAM
i5 3570k MSI Z68 MA-ED55 MSI GTX 750 Ti 6GB Corsair XMP 
Hard DriveOptical DriveCoolingOS
500GB WD Blue ASUS DVD Burner Hyper 212+ Windows 8.1 
PowerCase
Rosewill HIVE 750W Rosewill Blackbone 
CPUMotherboardGraphicsRAM
AMD Sempron 145 Gigabyte 990FXA-UD3 4x MSI GTX 750 Ti 6 GB Corsair XMP 
Hard DriveCoolingOSMonitor
500 GB Seagate Barracuda Stock Windows 7 LG 20" 900p display 
KeyboardPowerCaseMouse
Dell generic Rosewill HIVE 750W Rosewill Thor V2 Dell generic 
Other
4x USB Powered Risers 
  hide details  
Reply
Number Cruncher
(16 items)
 
HTPC
(11 items)
 
750TIMINER2
(15 items)
 
CPUMotherboardGraphicsRAM
i7 2600k ASUS Sabertooth Z77 Sapphire 290 Tri-x 4GB OC CORSAIR Vengeance 16GB 
Hard DriveHard DriveHard DriveOptical Drive
Intel 520 120 GB SSD Seagate Barracuda 1 TB 250GB Samsung 840 EVO SSD LG Blu-Ray Lightscribe Combo Burner 
CoolingOSMonitorPower
Swiftech H320 (CPU only) Windows 7 Ultimate 27" Shimian 1440p Monitor 1000W Corsair PSU 
CaseMouseMouse PadAudio
800D custom painted and fixed up! 15 year old Microsoft Custom Minecraft Mousepad Logitech 2.1 Speakers (wub wubbity wub wub) 
CPUMotherboardGraphicsRAM
i5 3570k MSI Z68 MA-ED55 MSI GTX 750 Ti 6GB Corsair XMP 
Hard DriveOptical DriveCoolingOS
500GB WD Blue ASUS DVD Burner Hyper 212+ Windows 8.1 
PowerCase
Rosewill HIVE 750W Rosewill Blackbone 
CPUMotherboardGraphicsRAM
AMD Sempron 145 Gigabyte 990FXA-UD3 4x MSI GTX 750 Ti 6 GB Corsair XMP 
Hard DriveCoolingOSMonitor
500 GB Seagate Barracuda Stock Windows 7 LG 20" 900p display 
KeyboardPowerCaseMouse
Dell generic Rosewill HIVE 750W Rosewill Thor V2 Dell generic 
Other
4x USB Powered Risers 
  hide details  
Reply
post #2 of 2
Not sure if this is off-base for your need since it doesn't seem that complex, but it should be able to check the range for you.
Code:
Private Function CheckValid(ByVal input As Double) As Boolean
        If (input > 0 And input <= 100) Then Return True
        Return False
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox("Complies: " + CheckValid(Convert.ToDouble(TextBox1.Text)).ToString)
End Sub
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
Waiting on X399
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II B57 @ X4 3.9 Gigabyte 790FXTA-UD5 Sapphire Radeon 290 8 GB G.Skill 2133 
Hard DriveCoolingOSKeyboard
250 GB 840 EVO Noctua NH-D14 Windows 10 Logitech K350 
PowerCaseMouseMouse Pad
Seasonic x750 Corsair 600T Logitech G100s Razer Goliathus Speed 
Audio
Plantronics Gamecom 788 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › VB.NET - Checking if a number is within a range function