Click here to Skip to main content
15,868,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

A question if I need to compare two values (as strings) and use the StrComp() function in VB.Net as below:
C#
ResultReadBackTest = StrComp(ValResultPastEquals, Value_Sx_Calculated_String)

What it does is checks the values read back from a device and compares it to what was sent.
The device only deals with strings.

However I can see issues with this method as I have lashed up a quick program to test

VB
Dim ValueA As String
       Dim ValueB As String
       Dim Result As Integer


       ValueA = "0011059096"
       ValueB = "11059096"

       Result = StrComp(ValueA, ValueB)
       MsgBox("Result = " + Result.ToString())

The values I have got from the program I have written are 0011059096 and 11059096
which gives a 1, meaning from the MSDN page Value1 sorts after Value2 ??
I know if I did a substring and got rid of the leading zero's it might work but that could be dangerous. Opions please on the following:
VB
intValA = Val(ValueA)
intValB = Val(ValueB)
MsgBox("Value A = " + intValA.ToString() + " Value B = " + intValB.ToString())
If (intValA = intValB) Then MsgBox("Huzzah")
or to put it cleanly
convert use Val to convert ValueA to an integer and the same for ValueB and do a compare...
Seems a little too easy...

Glenn
(I dislike VB...)
Posted
Comments
CHill60 31-Jul-14 7:06am    
Does your device always return a 10 character padded string? If so you could do something like Dim Value_Sx_Calculated As Integer = 11059096
Value_Sx_Calculated_String = Value_Sx_Calculated.ToString("0000000000")
glennPattonWork3 31-Jul-14 7:21am    
I will try that!

1 solution

If you compare strings, the comparison is always character-by-character, and the result of the comparison is always based on the result of the first different pair of characters found.
So if you compare "1" with "09999999999999" the first string will always be larger.
If you need a numeric comparison, always convert to numeric values and compare those.

BTW: What are yuou doing playing with VB? I though you detested it! :laugh:
 
Share this answer
 
Comments
glennPattonWork3 31-Jul-14 7:41am    
It's for that beast of a test rig (my feeling can't be published on it). I have done the Val of each and tested it, it appears to work (mind you it did that before!)
Glenn

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900