array with seperate text boxes??

hulk

New member
Joined
Apr 2, 2006
Messages
2
Programming Experience
Beginner
I have a program that I need to write, and I am not really getting how to get an array to work the way I am thinking. Basically I have 6 text boxes which the user will enter numbers. I need to write an array that takes the values from the 6 boxes, drops the lowest value of the 6 and calculates the remaining 5 of them. I know how to do the array if the numbers were in a list box, but I am not understanding how to go about it since they are in 6 different text boxes.

My code so far is:

Private Sub btntotal_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btntotal.Click
Dim int1, int2, int3, int4, int5, int6, inttotal as integer

int1 = txt1.Text
int2 = txt2.Text
int3 = txt3.Text
int4 = txt4.Text
int5 = txt5.Text
int6 = txt6.Text

txttotal.Text = Str(inttotal)

The only thing my brain keeps coming back to would be how to find the minimum value in a listbox, but I know I am way off:

Dim intMin, intMax, intTotal As Integer
Dim intElement As Integer

intMax = Integer.MinValue
intTotal = 0
For Each intElement In Me.lstList.Items
intTotal += intElement
If intElement < intMin Then
intMin = intElement
End If



I hope someone can understand this as I've been racking my brain for a few hours now, any points in the right direction will be helpful, or if I need to explain better let me know.

thanks
 
to make an array when you declare the integer variable simply put the number of elements the array should be able to hold in parenthesis in your case you'd need to hold 6 values and since arrays are zero based (0 is the 1st element) you'd declare an integer array of 5 (0 through 5) then all you would need to do is call the arrays sort method and do math on the bottom 5 elements

so instead of:
VB.NET:
        Dim int1, int2, int3, int4, int5, int6,  inttotal     as integer
you'd have:
VB.NET:
Dim intValues(5) As Integer
Dim intTotal As Integer

intValues(0) = Cint(txt1.Text)
intValues(1) = Cint(txt2.Text)
etc...

and welcome to the forums
 
Thanks for the input, it helped a lot, I'm still struggling however with subtracting the lowest value. Basically with what i have so far, its only subtracting the last text box value, instead of the lowest total value of each text box. so far i have:

Dim intvalues(5) As Integer
Dim hwtotal As Integer
intvalues(0) = CInt(txt1.Text)
intvalues(1) =
CInt(txt2.Text)
intvalues(2) =
CInt(txt3.Text)
intvalues(3) =
CInt(txt4.Text)
intvalues(4) =
CInt(txt5.Text)
intvalues(5) =
CInt(txt6.Text)

Dim intMin, intTotal As Integer
Dim intelement As Integer
For Each intelement In intvalues
intMin =
Integer.MinValue
intTotal = 0
If intelement < intMin Then
intMin = intelement
End If
Next

dimtotals as integer
inttotal = intvalues(0) + intvalues(1) + intvalues(2) + intvalues(3) + intvalues(4) + intvalues(5) - (intelement)

any help is greatly apreciated, and thank you

 
you're close the problem is that you arnt sorting the array (calling the array's sort method puts the lowest value in the 0 position, so once it's sorted intValues(0) will always be the lowest score)

so once you've called the sort method simply add elements 1 through 5 to total them up

VB.NET:
        Dim intValues(5) As Integer
        Dim intTotal As Integer
        intValues(0) = CInt(txt1.Text)
        intValues(1) = CInt(txt2.Text)
        intValues(2) = CInt(txt3.Text)
        intValues(3) = CInt(txt4.Text)
        intValues(4) = CInt(txt5.Text)
        intValues(5) = CInt(txt6.Text)
        intValues.Sort(intValues)
        For Counter As Integer = 1 To 5
            intTotal += intValues(Counter)
        Next Counter
        MessageBox.Show("Lowest Value: " & intValues(0).ToString & ControlChars.NewLine & "Total: " & intTotal.ToString)
 
Back
Top