Solved

using string.equals to compare values

Posted on 2014-01-16
4
225 Views
Last Modified: 2014-01-23
I am using string.equual to compare two values (old vs new)

                    If String.Equals(myHistoryRecipeRecord.MeltPointLow, DirectCast(dvRecipeItem.Rows(5).Cells(1).Controls(0), TextBox).Text) Then
                        strRefineRecipeRecord.Append("MeltPointLow: " & myHistoryRecipeRecord.MeltPointLow & ", ")
                    Else
                        strRefineRecipeRecord.Append("MeltPointLow: Old= " & myHistoryRecipeRecord.MeltPointLow &
                                                     ", New= " & DirectCast(dvRecipeItem.Rows(5).Cells(1).Controls(0), TextBox).Text & ", ")
                    End If

Open in new window


The problem I am having is that this field: myHistoryRecipeRecord.MeltPointLow
contains a decimal value like: 100D

and DirectCast(dvRecipeItem.Rows(5).Cells(1).Controls(0), TextBox).Text)
contains: 100.00000000000000000

Which one of the values is easier to convert to type, so that the comparision code executes more accurately?

please help if possible with example.

Thank you
0
Comment
Question by:metropia
[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
  • 2
  • 2
4 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 39787815
Is there any particualr why you are comparing them as strings instead of using decimals?

You can use Decimal.Equals Method which it is more accurate in your case.
0
 

Author Comment

by:metropia
ID: 39788589
I tried that but even using Decimal.Equals, it always falls to the else part of the statement.

If Decimal.Equals(CType(myHistoryRecipeRecord.MeltPointLow, String), DirectCast(dvRecipeItem.Rows(5).Cells(1).Controls(0), TextBox).Text) Then 
    strRefineRecipeRecord.Append("MeltPointLow: " & myHistoryRecipeRecord.MeltPointLow & ", ")
Else
    strRefineRecipeRecord.Append("MeltPointLow: Old= " & myHistoryRecipeRecord.MeltPointLow & ", New= " & DirectCast(dvRecipeItem.Rows(5).Cells(1).Controls(0), TextBox).Text & ", ")
End If

Open in new window


The values that are comparing are:

myHistoryRecipeRecord.MeltPointLow contains: 100D
DirectCast(dvRecipeItem.Rows(5).Cells(1).Controls(0), TextBox).Text) contains: 100.00

This should be true when it runs true the If statement, because both mean the same, but the are formatted different.

Any ideas to work around this problem are welcome.

Thank you very much.
0
 

Author Comment

by:metropia
ID: 39788689
the fields that come with strRefineRecipeRecord are for a record obtained from the entity model. so in the beginning of the sub this is how i get it:

Dim myContext As New OLTPEntities
Dim myHistoryRecipeRecord = myContext.RefineRecipe.Where("it.Id=" & Me.dvRecipeItem.DataKey(0)).FirstOrDefault()

Then i access the field like:

myHistoryRecipeRecord.MeltPointLow

can this be formatted? I think it taks the format from the db
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 39792796
Ah, regarding "I tried that but even using Decimal.Equals", it is easy to solve if you use for the if logic the following condition:
if (Math.Abs(low - text) <= margin)

where low = decimal representation of myHistoryRecipeRecord.MeltPointLow
 text= decimal representation of textbox contents
margin = any demical value e.g. 0.01

Thus no need to to change your db.
0

Featured Post

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!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 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