Issue comparing two strings in IF activity

Hello!

I am trying to compare two strings in an IF activity condition

I am copy pasting a cell in excel, I put it into a variable. I then want to check if the value equals “EEL ftgnr”, and if it doesn’t I’ll press key down, copy paste and continue til I do.

Everything works except for the fact that no matter how I do, I can’t get the if activity to properly compare the strings.

I’ve even tried manually setting values such as numbers and text so that it doesn’t fetch anything from Excel, and I still cannot compare the strings in the if activity condition?

I’ve tried variable1=variable2 and variable1 is variable2 but can’t get it to work :frowning:

Any advice?

I attached the code here: Main.xaml (11.1 KB)

Regards, Emil

Change it to cell_content = eel_ftgnr

Using the ‘Is’ and ‘=’ operators have two separate functions.

= is determining if the values of the variables are equal, in your case TRUE

‘is’ determines if the two variables (objects) refer to the same object, in your case FALSE

2 Likes

Hi @guemi,
I think you can’t just compare two strings from variable. If statement requires math calculations which are not possible to do on words. Instead of this you could use some bool. Look at this code:

It returns output (true or false) and based on calculation like this you could is it for If activity like If “calculation” returs true then something…

1 Like

@guemi

Try like this:

varString1.Trim.ToString.Equals(varString2.Trim)

9 Likes

Hey Emil,

@Ronan is correct in that the is keyword performs reference comparison, meaning that two objects with the same value will still return false when compared with is, since they are stored in different locations in memory and altering the value of one will not affect the other.

I would take this one step further and eliminate the IF statement block entirely. You should just assign the value of eel_ftgnr_hittat to the result of cell_content = eel_ftgnr.

@lakshman Also has a good point in trimming the beginning and end of any whitespace which may cause the equality to fail despite having the same content with extra spaces.

2 Likes

In addition to the great comments by others, keep in mind that just checking string1 = string2 will only work if it matches the case. If you don’t care whether it has upper/lowercase, then you should instead compare the two strings using String.Equals to do so and use the StringComparison options to ignore case. Therefore it’d look like this in your if condition: String.Equals(variable1,variable2,StringComparison.InvariantCultureIgnoreCase)

2 Likes

@guemi - If you’re copying from Excel with a CTL-C, you should know that Excel, for whatever reason, adds a CrLf at the end of of the line. This may be the issue, and you’l need to remove that - something like cell_content.replace(vbcrlf,“”), as I’m not sure where you eel_ftgnr variable is being populated

Otherwise variable1=varibale2 is a valid VB Boolean expression.

1 Like

@Pablito The If condition is simply a VB (C#) expression that resolves to a Boolean value.
variable1=variable2 is a valid VB expression that resolves to True/False

2 Likes

Hello, thanks for all the fast replies!

I did try cell_content=eel_ftgnr but I am not sure if i used spaces around =.

Would that be the problem?

Check this condition for data : var1.Equals(var2)
If condition true then write steps in Then step and else write steps for false condition

No. cell_content=eel_ftgnr cell_content =eel_ftgnr cell_content= eel_ftgnr cell_content = eel_ftgnr all work the same.

@guemi Ok, I did a PoC (Proof of Concept) on my theory, and I think this proves my hypothesis. For cell_content you’re getting from Excel Read Cell Activity. That pulls just the text. But then you down arrow and use CTL+C then assign a variable the contents of the clipboard.

I used your code (modified since I didnt have your Excel file) But captured variable contents in a wrapper - observe the results: (first is cell_content, then is eel_ftgnr
If Closed
<DATA>test</DATA>
Write Line Executing
Write Line Closed
<DATA>test
</DATA>
Write Line Executing
Write Line Closed

image

So, like @lakshman mentioned, we need a Trim to clear whitespace:
image

So, now when run - SUCESS:
image

Something is off here. To try the comparsion in the if activity I manually set the variables to the same thing, like this:

And I commented out any function setting these variable names, so the robot should ALWAYS hit true.

bild

Yet, for some reason, it returns false.

What the hell am I doing wrong?

Entire code here: Main.xaml (10.4 KB)

@guemi - You’re still assigning a variable here:

2 Likes

Jesus I’m stupid… Thanks for the extra pair of eyes buddy.
(moderated)

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.