Help finding a highest value integer

jaysoul

Member
Joined
Nov 23, 2005
Messages
7
Programming Experience
Beginner
Hi,

I am very new to vb.net but am enjoying getting to grips with it.
I have only been using vb.net for 2 months now.

I'm sure what I need to do is very simple but just need to know how to do it.

If I have 10-15 text boxes or combo boxes all containing integers.

How can I write a peice of code to pull out the highest integer from the boxes and also the lowest integer.

Help much appreciated.

Kind Regards

Jay :D
 
Hi Kulrom... thanks for the reply.

Yeah, you went far to fast and far too deep there mate! :eek:

You need to go step by step and explain the code involved I'm afraid.

How do I get my integers into an array and then how do I pull out the first and last numbers?

Kind Regards

Jay
 
I would try something like the following.. It loops through all the controls on the form and if it is a combobox or a textbox it trys to convert the text to an integer value and then checks it against the current highest and lowest numbers...If you know that the data in the textboxes/comboboxes will be numeric you can remove the try..catch blocks.

Dim ctrl As Control
Dim tempNumber As Integer
Dim biggestNumber As Integer
Dim lowestNumber As Integer

'seed the lowest number with the highest
'integer value avaiable
lowestNumber = Integer.MaxValue

'seed the biggest number with the lowest
'integer value available
biggestNumber = Integer.MinValue

'loop through all the controls in the current form
For Each ctrl In Me.Controls
'if it is a textbox
If TypeOf (ctrl) Is TextBox Then
Try
'get the number from the textbox
tempNumber = Convert.ToInt32(ctrl.Text)

'check if it is the biggest number
If tempNumber > biggestNumber Then
biggestNumber = tempNumber
End If

'check if it is the smallest number
If tempNumber < lowestNumber Then
lowestNumber = tempNumber
End If
Catch ex As Exception
End Try
End If

'check the comboboxes
If TypeOf (ctrl) Is ComboBox Then
Try
'get number from combobox
tempNumber = Convert.ToInt32(ctrl.Text)

'check if it is the biggest number
If tempNumber > biggestNumber Then
biggestNumber = tempNumber
End If

'check if it is the smallest number
If tempNumber < lowestNumber Then
lowestNumber = tempNumber
End If
Catch ex As Exception

End Try
End If
Next

MsgBox("Largest Number = " & biggestNumber & _
"; Lowest Number = " & _
lowestNumber, MsgBoxStyle.Information, "Biggest " & _
"and Lowest Number")

...
Hope this is helpful
 
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myValues [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2]()
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls.Count - 1
[/SIZE][SIZE=2][COLOR=#0000ff]If [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls(i).GetType [/SIZE][SIZE=2][COLOR=#0000ff]Is [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2](TextBox) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ReDim [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Preserve[/COLOR][/SIZE][SIZE=2] myValues(i)
myValues(i) += [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls(i).Text)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]Array.Sort(myValues)
MessageBox.Show("The highest value is : " & myValues(myValues.Length - 1))
Regards ;)
[/SIZE]
 
Note that when using Redim with the Preserve keyword, the old elements are preserved as copies in the new array. This can lead to performance loss in some instances which is why I use ArrayList whenever possible.
 
If you're really new into vb then i understand some things these guys say don't make anny sense to you, maybe try max() and min() command or play arround with the math. function.

+sorry if i use some words wrong btw, i tend to mix some things :eek:
 
Back
Top