|
-
Sep 27th, 2009, 09:30 PM
#1
Thread Starter
Lively Member
[RESOLVED] How to Find Highest number of all?
What is the right way to find Highest decimal number out of 10 different numbers(or more)? Since I’m Newb, I made this code below, and surprisingly it doesn’t work every time! All numbers are declared as decimals but some times 0.02 is a higher number then 0.11!!!
Code:
If n2 > (n3 And n4 And n5 And n6 And n7 And n8 And n9) Then
TextBox11.Text = n2
End If
If n3 > (n2 And n4 And n5 And n6 And n7 And n8 And n9) Then
TextBox11.Text = n3
End If
If n4 > (n2 And n3 And n5 And n6 And n7 And n8 And n9) Then
TextBox11.Text = n4
End If
If n5 > (n2 And n3 And n4 And n6 And n7 And n8 And n9) Then
TextBox11.Text = n5
End If
If n6 > (n2 And n3 And n4 And n5 And n7 And n8 And n9) Then
TextBox11.Text = n6
End If
If n7 > (n2 And n3 And n4 And n5 And n6 And n8 And n9) Then
TextBox11.Text = n7
End If
If n8 > (n2 And n3 And n4 And n5 And n6 And n7 And n9) Then
TextBox11.Text = n8
End If
If n9 > (n2 And n3 And n4 And n5 And n6 And n7 And n8) Then
TextBox11.Text = n9
End If
VB n00b with stupid questions
Home site keywen.com
-
Sep 27th, 2009, 09:47 PM
#2
Re: How to Find Highest number of all?
There are various ways. Regardless of which way you choose, the first order of business would be to put all the values into an array or collection. The simplest way is to then just call the Maximum method on that list, which is part of LINQ and requires .NET 3.5.
The next simplest method would be to sort the array or collection and then just get the last item.
If this is homework, as I'm guessing it is, then I doubt either of those methods will be acceptable. In that case you should declare a variable to store the maximum and then loop through the list. If the current item is greater than the current maximum you replace the maximum. At the end you'll have the greatest value.
-
Sep 27th, 2009, 09:52 PM
#3
Thread Starter
Lively Member
Re: How to Find Highest number of all?
Thank you for reply; it’s not homework, Learning VB on my own as a hobby.
Never used collection/maximum before,
----
edit, could you please give an example of one off those methods
Last edited by nusaki; Sep 27th, 2009 at 10:00 PM.
VB n00b with stupid questions
Home site keywen.com
-
Sep 27th, 2009, 10:01 PM
#4
Re: How to Find Highest number of all?
For future reference, the reason your original code didn't work is because you cannot combine half conditions like this:
vb.net Code:
If n2 > (n3 And n4 And n5 And n6 And n7 And n8 And n9) Then
That code is valid but it's not doing what you think it is. For what you want it would have to be like this:
vb.net Code:
If n2 > n3 And n2 > n4 And n2 > n5 And n2 > n6 And n2 > n7 And n2 > n8 And n2 > n9) Then
On another note, it would be far more efficient to use AndAlso rather than And. Check out the MSDN documentation for en explanation of why.
-
Sep 27th, 2009, 10:11 PM
#5
Thread Starter
Lively Member
Re: How to Find Highest number of all?
Replace and with andalso, proboly lil better but still getting mistakes.
Is here way to code something like this:
vb Code:
Max=Maximum of {n2,n3,n4,n5,n6,n7,n8,n9}
?
my code so far:
Code:
If n2 > n3 AndAlso n2 > n4 AndAlso n2 > n5 AndAlso n2 > n6 AndAlso n2 > n7 AndAlso n2 > n8 AndAlso n2 > n9 Then
TextBox11.Text = n2
End If
If n3 > n2 AndAlso n3 > n4 AndAlso n3 > n5 AndAlso n3 > n6 AndAlso n3 > n7 AndAlso n3 > n8 AndAlso n3 > n9 Then
TextBox11.Text = n3
End If
If n4 > n2 AndAlso n4 > n3 AndAlso n4 > n5 AndAlso n4 > n6 AndAlso n4 > n7 AndAlso n4 > n8 AndAlso n4 > n9 Then
TextBox11.Text = n4
End If
If n5 > n2 AndAlso n5 > n3 AndAlso n5 > n4 AndAlso n5 > n6 AndAlso n5 > n7 AndAlso n5 > n8 AndAlso n5 > n9 Then
TextBox11.Text = n5
End If
If n6 > n2 AndAlso n6 > n3 AndAlso n6 > n4 AndAlso n6 > n5 AndAlso n6 > n7 AndAlso n6 > n8 AndAlso n6 > n9 Then
TextBox11.Text = n6
End If
If n7 > n2 AndAlso n7 > n3 AndAlso n7 > n4 AndAlso n7 > n5 AndAlso n7 > n6 AndAlso n7 > n8 AndAlso n7 > n9 Then
TextBox11.Text = n7
End If
If n8 > n2 AndAlso n8 > n3 AndAlso n8 > n4 AndAlso n8 > n5 AndAlso n8 > n6 AndAlso n8 > n7 AndAlso n8 > n9 Then
TextBox11.Text = n8
End If
If n9 > n2 AndAlso n9 > n3 AndAlso n9 > n4 AndAlso n9 > n5 AndAlso n9 > n6 AndAlso n9 > n7 AndAlso n9 > n8 Then
TextBox11.Text = n9
End If
what if i had 100 numbers?
Last edited by nusaki; Sep 27th, 2009 at 10:20 PM.
VB n00b with stupid questions
Home site keywen.com
-
Sep 27th, 2009, 10:25 PM
#6
Re: How to Find Highest number of all?
 Originally Posted by nusaki
what if i had 100 numbers?
That's exactly why I said you should put the values into an array or collection. How exactly you create that list depends on where the data is coming from. Once you've got the list though, you simply call its Maximum method, the same way as you call any other method.
So, first things first: how are you going to create the list? Keeping in mind that arrays are fixed-size and a List can grow and shrink dynamically, which is the better choice for your data?
-
Sep 27th, 2009, 10:33 PM
#7
Thread Starter
Lively Member
Re: How to Find Highest number of all?
omg I found my error now that code works fine, its just my numbers were getting wrong values! Thanks jmcilhinney
VB n00b with stupid questions
Home site keywen.com
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|