Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using string.equals to compare values

Posted on 2014-01-16
4
Medium Priority
?
228 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Integration Management Part 2
Suggested Courses

877 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