In Reply To Comments Made On "Working With A Simple Structure Array In VB.NET"

Olive 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 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.

(Actually, she had left two comments, the second being a copy of her code, but it was incomplete due to the limitations of size in my blog comments box. I’ll update this post with her code if she wants to e-mail it to me for posting.)

There are a few parts of this comment I’d like to respond to more completely than I can in a follow-up comment, so here’s a second blog post on the subject.

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.

I’m fairly sure you mean the array needs global scope, given your description.

A variable has global scope when it is available to all functions, subroutines and variables in the application. I discuss and demonstrate global scope in this blog post.

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.

Technically, you’re correct: a variable can only contain one value, so you couldn’t, for example, assign to a variable every integer from 1 to 100.

There are a number of ways you can achieve the effect of having multiple values assigned to a single variable, but as I’ll demonstrate, almost always, they’re simply ways of making a simple problem far more complicated.

For example, given our previous Structure:

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

We could change intMinGrade to be an array, instead, which contains two integers: The min score and the max score needed for the grade. Then, we could simply compare our input against the same array of Grades structures, seeing whether our grade was less than the max and higher than the min:

Structure Grades
	Public intRange(2) As Integer
	Public strLetterGrade As String
End Structure

'populate array of grades structures
Dim graGradingScale(5) As Grades
graGradingScale(0).intRange(0)= 100
graGradingScale(0).intRange(1)= 91
graGradingScale(0).strLetterGrade = "A"
graGradingScale(1).intRange(0)= 90
graGradingScale(1).intRange(1)= 81
graGradingScale(1).strLetterGrade = "B"
graGradingScale(2).intRange(0)= 80
graGradingScale(2).intRange(1)= 71
graGradingScale(2).strLetterGrade = "C"
graGradingScale(3).intRange(0)= 70
graGradingScale(3).intRange(1)= 61
graGradingScale(3).strLetterGrade = "D"
graGradingScale(4).intRange(0)= 60
graGradingScale(4).intRange(1)= 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.intRange(0) > intSearchFor And tmpGrade.intRange(1) < intSearchFor Then
			strGrade = tmpGrade.strLetterGrade
			Exit For
		End If
	Next
	MsgBox("The grade for your score of " & intSearchFor & " is " & strGrade)
End If

Notice, however, that this has the same net effect of our original solution of simply looking for the lowest grade needed, but contains more code. That is, the array solution is not elegant; it's not as simple as possible, nor does it use as little code as possible.

Notice that we also could have achieved the array solution by assigning the "range" as a string, then splitting the string around a delimiter, then casting the resulting parts to integers and comparing them that way.

For example, using this as our structure:

Structure Grades
	Public strRange As String
	Public strLetterGrade As String
End Structure

And populating each member of the Grades array like this:

graGradingScale(0).strRange = "100,91"
graGradingScale(0).strLetterGrade = "A"

In our For Each loop, we can split the value of strRange into two values using the Split() function. That will give us a two-cell array with the max and min values at cells 0 and 1, respectively; we can then compare our number to the "range":

'declare input and ouput variables
Dim intSearchFor As Integer = Console.ReadLine()
Dim strGrade As String
Dim tmpGrade As Grade
Dim strTemp As String

'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
		strTemp = tmpGrade.strRange.Split(",")
		If strTemp(0) > intSearchFor And strTemp(1) < intSearchFor Then
			strGrade = tmpGrade.strLetterGrade
			Exit For
		End If
	Next
	MsgBox("The grade for your score of " & intSearchFor & " is " & strGrade)
End If

Notice, again, this is not as elegant as simply holding the single, minimum value.

Generally speaking, whenever you're trying to work with a range, it makes more sense to sort your haystack in some sensible order that represents a range, then run the needle through it until you find a match.

Related Posts
  1. Working With A Simple Structure Array In VB.NET (83.7)
  2. Variable Scope Made Simple (29.8)
  3. In Response To Comments About A Simple PHP Calendar (23.4)
  4. Interpreting A C# Class Code Structure (18.4)
  5. Objects (Classes) Explained In Very Simple Terms (12.7)

The numbers inside parentheses are relevance scores. Scoring is based, in order of priority, on title, category, content and tags. The higher the score, the more likely that post relates to this post.

One Comment

  1. OliveOyl says:

    “I’ll update this post with her code if she wants to e-mail it to me for posting.”

    That’s ok. I just wanted to show you the code I used, since you were trying to help me figure out how to do this.

    “the array needs global scope”

    Yes, that’s it.
    :)