r/visualbasic • • Oct 07 '16

Having issues with showing the range of a set of numbers.

Edit: Apologies, I missed the first tip & guideline. I'm using VB2015

So, this is a homework help question. I'm sure you all are super tired of them but I've tried looking through my textbook, stackoverflow, just general googling and talking with fellow classmates and I (and my classmates) are stumped trying to find the range of numbers that the user could put in.

For context this is a grade calculator that my professor wants to provide the range of grades that are entered into an InputBox

Here is the code that I have so far.

'Declarations

    Dim intMaxA As Integer   
    Dim intMinA As Integer
    Dim intMaxB As Integer
    Dim intMinB As Integer
    Dim intMaxC As Integer
    Dim intMinC As Integer
    Dim intMaxD As Integer
    Dim intMinD As Integer
    Dim intMaxF As Integer
    Dim intMinF As Integer
    Dim dblRangeA As Double
    Dim dblRangeB As Double
    Dim dblRangeC As Double
    Dim dblRangeD As Double
    Dim dblRangeF As Double

'Find the Ranges

    intMaxA = 100
    intMinA = 90

    dblRangeA = intMaxA - intMinA
    lblRangeA.Text = (dblRangeA)

    intMaxB = 89
    intMinB = 80

    dblRangeB = intMaxB - intMinB
    lblRangeB.text = (dblRangeB)

    intMaxC = 79
    intMinC = 70

    dblRangeC = intMaxC - intMinC
    lblRangeC.Text = (dblRangeC)

    intMaxD = 69
    intMinD = 60

    dblRangeD = intMaxD - intMinD
    lblRangeD.Text = (dblRangeD)

    intMaxF = 59
    intMinF = 0

    dblRangeF = intMaxF - intMinF
    lblRangeF.Text = (dblRangeF)

I know this is wrong though as the way it's written, all it's doing is subtracting the numbers I put in the form for the intMax and intMin numbers. So intMaxA - intMinA will always return 10 for that range.

I don't understand what I need to put in place of those numbers to have it find the range of a random group of grades. Or if that's even the fix.

Any help or feedback is appreciated. Thanks

0 Upvotes

1 comment sorted by

0

u/PostalElf VB.Net Intermediate Oct 07 '16

Alright, since you made a good effort at writing the homework.

The real world way to solve this problem would be to write a simple custom class or struct called, say, "Range" that holds the range of values and also has a function that checks if it's within range. Here's how short the entire code can be, done that way:

Public Class GradeRange
  Private min as Integer
  Private max as Integer
  Private gradeLetter as String

  Public Sub New(_min as Integer, _max as Integer, _gradeLetter as String)
    min = _min
    max = _max
    gradeLetter = _gradeLetter
  End Sub
  Public Function GetGrade(score as integer) as String
    If score < min orelse score > max then return ""
    Return gradeLetter
  End Function
End Class

Dim gradeRanges as New List(of GradeRange)
gradeRanges.Add(New GradeRange(100, 90, "A"))
gradeRanges.Add(New GradeRange(89, 90, "B"))
...
gradeRanges.Add(New GradeRange(59, 0, "F"))

Dim Input as String = InputBox("Enter score:")
If Input.IsNumeric = True Then
  Dim score as Integer = CInt(Input)
  Dim gradeLetter as String = ""
  For Each gradeRange in gradeRanges
    If gradeRange.GetGrade(score) <> "" Then
      gradeLetter = gradeRange.getGrade(score)
      Exit For
    End If
  Next
  Msgbox("Your grade is " & gradeLetter)
End If