Solved

using string.equals to compare values

Posted on 2014-01-16
4
220 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
  • 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

685 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