strings compare (numeric).

I have started a function, what I need for the function to do is to is to compare two numeric  strings and determine which is larger.  the strings also need to be declared as double .

Here is what I have so far:
Public Function MyFunction(ByVal A As String, ByVal B as String) As String
     ' Dim A as double
     Dim intNumber as double
     If Integer.TryParse(A, intNumber) > Integer.TryParse(B, intNumber) 
         Return A + "is larger than B"
     Else 
        Return B + "is larger than A"
    End if 
  End Function

Open in new window

Thanks.
donnie91910Asked:
Who is Participating?
 
Chris WatsonConnect With a Mentor Software DeveloperCommented:
Have you considered what should happen in the case where the two numbers are equal?

You mention the need for input validation, but don't specify what should occur on invalid input. I have assumed you don't want to raise an exception based on your method parameters being defined as strings and not doubles.

I have modified Fernando's code to accommodate both these things.

Thoughts?

Public Function MyFunction(ByVal A As String, ByVal B As String) As String

    ' Parse / validate input.
    '
    Dim numberA As Double
    Dim numberB As Double

    If Not Double.TryParse(A, numberA) Then
        Return A & " is not a valid number."
    End If

    If Not Double.TryParse(B, numberB) Then
        Return B & " is not a valid number."
    End If

    ' Compare values.
    '
    If numberA > numberB Then
        Return A & " is larger than " & B
    ElseIf numberB > numberA Then
        Return B & " is larger than " & A
    Else
        Return A & " is equal to " & B
    End If

End Function

Open in new window

0
 
donnie91910Author Commented:
I get this error when I run my code:


Text:    Option Strict On disallows implicit conversions from 'Double' to 'Integer'.
0
 
MacroShadowConnect With a Mentor Commented:
How about this:
  Public Function MyFunction(ByVal A As String, ByVal B as String) As String
     If Conversions.ToInteger(A) > Conversions.ToInteger(B) Then
         Return A & " is larger than " & B
     Else
        Return B & " is larger than " & A
    End if
  End Function

Open in new window

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
donnie91910Author Commented:
it gives me the error :
'Conversions' is not declared. It may be inaccessible due to its protection level.
0
 
ChloesDadCommented:
It should be convert.tointeger, however that is .net code not classic VB

In classic VB you would use Val(A) > val(B).
0
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi donnie91910;

I think this may do what your looking for.

Public Function MyFunction(ByVal A As String, ByVal B as String) As String

     '' You need two versions of intNumber one for A and the other for B
     '' Otherwise B will overwrite A
     Dim intNumberA As Double
     Dim intNumberB As Double
     
     If Double.TryParse(A, intNumberA) > Double.TryParse(B, intNumberB) 
         Return A + " is larger than B"
     Else 
        Return B + " is larger than A"
    End if 
    
End Function

Open in new window

0
 
donnie91910Author Commented:
I received the message:
Your code returned '-3 is larger than A'
Given parameters: 39, -3, expected a value of 'A is larger than B.'
0
 
donnie91910Author Commented:
so basically this is what I am trying to do:
Create a function that compares two numeric strings and determines which is larger.  then i need to make sure that my code includes input validation.
0
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi donnie91910;

Actually the code was returning the correct answer. It was testing if A was greater then B and the answer was False and select the correct path in the If statement. I modified your code to return the Max of the two values.

Public Function MyFunction(ByVal A As String, ByVal B as String) As String

     '' You need two versions of intNumber one for A and the other for B
     '' Otherwise B will overwrite A
     Dim intNumberA As Double
     Dim intNumberB As Double
     
     '' Convert values from the string
     Double.TryParse(A, intNumberA)
     Double.TryParse(B, intNumberB)
    
     '' Use the Math.Max Library function to find the larger of the two
     Dim maxValue = Math.Max(intNumberA, intNumberb)
     
     '' See which was the larger and return the correct message
     If maxValue = intNumberA
        Return A + " is larger than B"
     Else
        Return B + " is larger than A"
     End If
    
End Function

Open in new window

0
 
donnie91910Author Commented:
I get the following errors:
Line:    29
Text:    Option Strict On requires all variable declarations to have an 'As' clause.

Line:    29
Text:    'Math' is not declared. It may be inaccessible due to its protection level.

Line:    32
Text:    Option Strict On disallows operands of type Object for operator '='. Use the 'Is' operator to test for object identity.
0
 
Fernando SotoRetiredCommented:
Change this line of code by add As Double

Dim maxValue = Math.Max(intNumberA, intNumberb)

To this, which is caused by having Option Strict On. The Math functions is located in the System namespace and should already have an reference to it so I added it to the line below in case you do not have it set in the file.

Dim maxValue As Double = System.Math.Max(intNumberA, intNumberb)

There is no line 32 in the code I posted Please post the relevant code.
0
 
MacroShadowCommented:
Add this line to the top of the module containing the code I posted:
Imports Microsoft.VisualBasic.CompilerServices

Open in new window

0
 
donnie91910Author Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.