?
Solved

strings compare (numeric).

Posted on 2015-02-16
14
Medium Priority
?
101 Views
Last Modified: 2015-02-21
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.
0
Comment
Question by:donnie91910
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +2
14 Comments
 

Author Comment

by:donnie91910
ID: 40613111
I get this error when I run my code:


Text:    Option Strict On disallows implicit conversions from 'Double' to 'Integer'.
0
 
LVL 27

Assisted Solution

by:MacroShadow
MacroShadow earned 500 total points
ID: 40613125
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
 

Author Comment

by:donnie91910
ID: 40613215
it gives me the error :
'Conversions' is not declared. It may be inaccessible due to its protection level.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:ChloesDad
ID: 40613238
It should be convert.tointeger, however that is .net code not classic VB

In classic VB you would use Val(A) > val(B).
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 total points
ID: 40613416
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
 

Author Comment

by:donnie91910
ID: 40613431
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
 

Author Comment

by:donnie91910
ID: 40613446
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
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 total points
ID: 40613467
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
 

Author Comment

by:donnie91910
ID: 40613475
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40613492
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
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40613576
Add this line to the top of the module containing the code I posted:
Imports Microsoft.VisualBasic.CompilerServices

Open in new window

0
 
LVL 5

Accepted Solution

by:
Chris Watson earned 500 total points
ID: 40614101
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
 

Author Closing Comment

by:donnie91910
ID: 40623865
Thank you.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question