Working With A Simple Structure Array In VB.NET

Recently asked in Yahoo! Answers:

In Visual Basic.NET how do you assign a grading scale to an array of structure variables?

I want to assign a grading scale to an array of structure variables. HOW do you assign a range of values, such as 0 to 299, to a variable within a structure? (In Visual Basic.NET)

Example:
‘declare array of structure variables
Dim graGradingScale(4) As Grades
Dim intSearchfor As Integer
Dim intX As Integer = 0

‘assign grading scale to the array
graGradingScale(0).intPoints = 0 to 299
graGradingScale(0).strGrades = “F”

The part that needs to be changed is “0 to 299″ . How do you assign this range of values to the variable, so that if the user enters a 7, for example, the grade that will match that number will be “F”? I have the rest of the code done correctly, except this one part.

Here is the Grades structure:

Structure Grades
Public intPoints As Integer
Public strGrades As String
End Structure

As I understand the question, what this questioner wants is to create a five-cell array — each cell corresponding to a letter grade of A, B, C, D or F — and assign both the letter grade and the range of numbers needed to obtain that grade into a Structure (struct, for you C-language folks).

Then, she wants to be able to run an input value through the array, to see what letter grade the person should get.

Well, that’s pretty straightforward, but we need to make a couple minor modifications to the questioner’s methodology to make this work as desired. So let’s get to it.

The Grades Structure

Our structure will retain its two properties. However, rather than having a range of numbers the Structure will use to determine a grade, instead we’ll use a single number that is the lowest score needed for the grade.

For example, let’s assume this grading structure:

Letter Grade Max Score Min Score
A 100 91
B 90 81
C 80 71
D 70 61
F 60 0

Let’s go ahead and set up our structure now:

Structure Grades
Public intMinGrade As Integer
Public strLetterGrade As String
End Structure

All we need now is to create the array of Grades structures and populate them with the lowest grade needed for the given letter grade, and that letter. (I’ll explain why shortly.)

Dim graGradingScale(5) As Grades
graGradingScale(0).intMinGrade = 91
graGradingScale(0).strLetterGrade = "A"
graGradingScale(1).intMinGrade = 81
graGradingScale(1).strLetterGrade = "B"
graGradingScale(2).intMinGrade = 71
graGradingScale(2).strLetterGrade = "C"
graGradingScale(3).intMinGrade = 61
graGradingScale(3).strLetterGrade = "D"
graGradingScale(4).intMinGrade = 0
graGradingScale(4).strLetterGrade = "F"

The Algorithm: Find The Lowest Applicable Score

We already know we’re going to go through each of these grade structures in an array, trying to match some input score against the numeric grade value. And the natural way to iterate through all the items in an array is with a For loop.

Since that’s the case, so long as we put our array together so that the letter grades are in descending order — that is, that A is at cell 0, and F at cell 4 — then as we go through each item in the array, all we really need to know is whether the input score is higher than the minimum score required for a given letter grade.

In other words, suppose we have an input score of 77. We know that is a C.

How do we know that? Because to get a B, you’d need a score of at least 81, and to get a C, you’d need at least a 71. Since 77 is less than 81 but more than 71, a score of 77 is equal to a letter grade of C.

The For Loop That Does The Work

Again, since our grades are in descending order, all we need to do is use a For loop to go through all the grades in the array, finding the one with a minimum score that’s less than our input score.

Once we have that, we know we’ve found the right letter grade to apply. So, we assign the proper grade to a string variable and show that to the user.

First, we’ll make sure the input score is in range — that is, it’s less than or equal to the maximum possible grade (100) but more than or equal to the minimum possible grade (0).

Then, we’ll run the input score through a For loop that goes through all the cells in the Grades array; as soon as we find a matching letter score, we Exit the For loop.

Dim intSearchFor As Integer = Console.ReadLine()
Dim strGrade As String
Dim I As Integer
 
If intSearchFor > 100 Or intSearchFor < 0 Then
	MsgBox("Input is out of range!")
Else
	For I = 0 To 4
		If graGradingScale(I).intMinGrade < intSearchFor Then
			strGrade = graGradingScale(I).strLetterGrade
			Exit For
		End If
	Next
	MsgBox("The grade for your score of " & intSearchFor & " is " & strGrade)
End If

An alternative — and the better practice for iterating all the items in an array — is to use a For Each statement. I only mention it second because many classes will not introduce For Each statements until some time after you’ve been working with arrays. If you have been instructed on how to use For Each statements, you should use this rather than a simple For loop.

Dim intSearchFor As Integer = Console.ReadLine()
Dim strGrade As String
Dim tmpGrade As Grade
 
If intSearchFor > 100 Or intSearchFor < 0 Then
	MsgBox("Input is out of range!")
Else
	For Each tmpGrade In graGradingScale
		If tmpGrade.intMinGrade < intSearchFor Then
			strGrade = tmpGrade.strLetterGrade
			Exit For
		End If
	Next
	MsgBox("The grade for your score of " & intSearchFor & " is " & strGrade)
End If

So, tying all the code together, this will accomplish what I think the questioner wants:

'declare structure
Structure Grades
Public intMinGrade As Integer
Public strLetterGrade As String
End Structure
 
'populate array of grades structures
Dim graGradingScale(5) As Grades
graGradingScale(0).intMinGrade = 91
graGradingScale(0).strLetterGrade = "A"
graGradingScale(1).intMinGrade = 81
graGradingScale(1).strLetterGrade = "B"
graGradingScale(2).intMinGrade = 71
graGradingScale(2).strLetterGrade = "C"
graGradingScale(3).intMinGrade = 61
graGradingScale(3).strLetterGrade = "D"
graGradingScale(4).intMinGrade = 0
graGradingScale(4).strLetterGrade = "F"
 
'declare input and ouput variables
Dim intSearchFor As Integer = Console.ReadLine()
Dim strGrade As String
Dim tmpGrade As Grade
 
'check input for range
If intSearchFor > 100 Or intSearchFor < 0 Then
	MsgBox("Input is out of range!")
Else
	'match input score to letter grade
	For Each tmpGrade In graGradingScale
		If tmpGrade.intMinGrade < intSearchFor Then
			strGrade = tmpGrade.strLetterGrade
			Exit For
		End If
	Next
	MsgBox("The grade for your score of " & intSearchFor & " is " & strGrade)
End If
Share This »
  • Digg
  • Yahoo! Buzz
  • Technorati
  • del.icio.us
  • Propeller
  • StumbleUpon
  • Reddit
  • Mixx
  • Twitter / Twit This
  • Pownce
  • Fark
  • Slashdot
  • NewsVine
  • BlinkList
  • Netvouz
  • Furl
  • Mister Wong
  • DZone
  • Ma.gnolia
  • Simpy
  • blogmarks
  • Blue Dot
  • Spurl
  • Sphinn
  • DotNetKicks
  • MySpace
  • Facebook
  • LinkedIn
  • Google Bookmarks
  • Yahoo! MyWeb
  • Windows Live Favorites

2 Comments

  1. OliveOyl:

    Thank you!

    I really appreciate all of the time and effort, and thought that has obviously gone into this answer.

    I had already figured out the answer to the problem, and it is the same as what you have said here.
    This is really cool, the way this has been answered!

    The other thing I was doing wrong with this problem is that the array must be declared in the public area of the form (I am not sure of the correct name for that area).

    The instructor’s requirement was that the array should only be populated one time, when the form loads. That means that the display click event needs to be able to access it, so it has to be declared in the public area.

    Thanks again! Hopefully this information will help others who are learning VB.NET.

    p.s. I am almost certain that it is impossible to “assign a range of values, such as 0 to 299, to a variable ” as I tried to do.

    I know that a scalar variable can hold ONLY one value at a time.

  2. dougv.com | The Web home of Doug Vanderweide » Blog Archive » In Reply To Comments Made On “Working With A Simple Structure Array In VB.NET”:

    [...] Oyl left the following comment in response to my blog post, “Working With A Simple Structure Array In VB.NET,” which responds to her Yahoo! Answers question: Thank [...]

Leave a comment