Solved

strings compare (numeric).

Posted on 2015-02-16
14
90 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
  • 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 26

Assisted Solution

by:MacroShadow
MacroShadow earned 125 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
 
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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 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 62

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 26

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 4

Accepted Solution

by:
Chris Watson earned 125 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Retain selection in datagridview 2 23
.net VBA word safemode 1 24
Hide Tab Page 3 20
Saveas need to save a copy 16 14
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now