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

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

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
Asked:
jrmcintosh
  • 3
  • 3
  • 2
1 Solution
 
frodomanCommented:
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
 
frodomanCommented:
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
 
Bob LearnedCommented:
Dim largest As Integer = Math.Max(Math.Max(n1, n2), n3)

Bob
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
Jason EvansSenior 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
 
Bob LearnedCommented:
Dog pile!!

Bob
0
 
Jason EvansSenior Software DeveloperCommented:
Who's the dog pile comment for?
0
 
frodomanCommented:
"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
 
Jason EvansSenior 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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