Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Ok lets say I wanted to get a few students grades using a inputebox. I need to keep each grade in a variable and allow the user to enter up to 101 grades. Now lets say for what ever reason I need to use a array to do some other stuff later.

Dim myArray(101) as integer

So I would have a index of 0 to 100

How do I use the inputbox to give each index a value (the grade)

 

Thnks for any Help

Hopeless NewBeeeeeee

Mike Bailey

  • *Experts*
Posted

Actually, this will create an array with 102 elements, ranging from

0 to 101. If you wanted 100 elements, you would declare myArray

as

Dim myArray(99) As Integer

 

You can use a For-Each loop to loop through an array, so just

do that, take the user's input, convert it to an Integer, and move

on.

 

Dim myArray(99) As Integer
Dim grade As Integer

For Each grade in myArray
 grade = Convert.ToInt32(InputBox("Grade?")) 
Next

 

Keep in mind this loop doesn't account for when a user enters a

value that is not an integer. You should also create your own

form for inputting values, as InputBox is one of those old VB6

functions that should be avoided in .NET.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

Something like

 

dim max_grade as integer = 0
dim av_grade as integer = 0
dim min_grade as integer = 100

For Loop i = 0 to 100
 if myarray(i) > max_grade then maxgrade = myarray(i)
 if myarray(i) < min_grade then mingrade = myarray(i)
 av_grade = av_grade + myarray(i)
Next i

msgbox ("Max grade = " + str(max_grade))
msgbox ("Min grade = " + str(min_grade))
msgbox ("Average grade = " + str(av_grade / 100))

 

Although personally rather then set it at 100, i would use a variable to hold this number, then have the user enter the correct number into a txtbox, which defaults to 100. That way you cover more possibilities, in the future without rewriting the program.

 

Reading Bucky's earlier reply, you would be better off using for each then for loop - forgot about them!

 

You can also put my if statements in buckys loop, to save running through it again and making it more efficient.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...