Solved

strings compare (numeric).

Posted on 2015-02-16
14
92 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 27

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

16 Experts available now in Live!

Get 1:1 Help Now