Mike Bailey Posted February 27, 2004 Posted February 27, 2004 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 Quote
*Experts* Bucky Posted February 27, 2004 *Experts* Posted February 27, 2004 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Mike Bailey Posted February 28, 2004 Author Posted February 28, 2004 Thanks for the help. I don't know what I would do with out this group. Quote
Mike Bailey Posted February 28, 2004 Author Posted February 28, 2004 One last ? how would I compare all grades in the array? like if I wanted to see what the highest grade entered was? Quote
Jay1b Posted February 28, 2004 Posted February 28, 2004 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. Quote
Administrators PlausiblyDamp Posted February 28, 2004 Administrators Posted February 28, 2004 You could also do somethnig like Array.Sort(myarray) max_grade = myarray(100) min_grade = myarray(0) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.