Solved

using string.equals to compare values

Posted on 2014-01-16
4
205 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 35

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 35

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

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.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
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…

757 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

25 Experts available now in Live!

Get 1:1 Help Now