Solved

using string.equals to compare values

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

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

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
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…

943 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

19 Experts available now in Live!

Get 1:1 Help Now