[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1003

Compare numeric values and get largest, vb.net

I want to compare three variables values (they are all numbers). I want the one with the highest value to be returned. How can I do this in vb.net?
0
jrmcintosh
• 3
• 3
• 2
1 Solution

Commented:
public function GetLargestValue(x as integer, y as integer, z as integer)
dim Largest as integer = -9999
if x > Largest then
Largest = x
end if
if y > Largest then
Largest = y
end if
if z > Largest then
Largest = z
end if
return Largest
end sub
0

Commented:
I forgot - you'll need "as integer" appended to the function declaration.

Also change the data type to whatever is appropriate based on the values you're comparing...
0

Commented:
Dim largest As Integer = Math.Max(Math.Max(n1, n2), n3)

Bob
0

Senior Software DeveloperCommented:
Hi there.

You could try the following:

Dim var1 As Integer = 34
Dim var2 As Integer = 345
Dim var3 As Integer = 67

Dim iArray As Integer() = { var1, var2, var3 }

Array.Sort(iArray)

Now, iArray will be (0) = 34, (1) = 67 and (2) = 345. So to get the highest value you return iArray(2).

Jas.
0

Commented:
Dog pile!!

Bob
0

Senior Software DeveloperCommented:
Who's the dog pile comment for?
0

Commented:
"Dog pile" means we've all piled onto this question with answers...

I think Bob is the Big Dog - I like his answer best :-)
0

Senior Software DeveloperCommented:
Ah, I see.

Your solution is the quickest mind, don't know why I thought and array sort would be the most effecient - but you live and learn!

Take care.
Jas.
0

Featured Post

• 3
• 3
• 2
Tackle projects and never again get stuck behind a technical roadblock.