# Compare current row with previous row

**URL:** <https://forum.uipath.com/t/compare-current-row-with-previous-row/520489>\
**Category:** Studio\
**Tags:** uiautomation\
**Created:** [February 24, 2023, 3:11am UTC](https://forum.uipath.com/t/compare-current-row-with-previous-row/520489 "2023-02-24T03:11:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![flyingdragon](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/flyingdragon/32/287509_2.png) [@flyingdragon](https://forum.uipath.com/u/flyingdragon)\
**Post date:** [February 24, 2023, 3:11am UTC](https://forum.uipath.com/t/compare-current-row-with-previous-row/520489/1 "2023-02-24T03:11:21Z")

</div>

Hi,

I am using for each loop and if function. I am trying to compare if current row is the same with previous row using:

dt.rows.indexof(row)\>0 and row(“Company Name”).ToString = dt(dT.Rows.IndexOf(row) - 1).item(“Company Name”).ToString

however, i encounter error “If: Object reference not set to an instance of an object.” and i do not know why.

Pls help, thank you

---

<div class="post-metadata">

**Author:** ![Yoichi](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/yoichi/32/523754_2.png) [@Yoichi](https://forum.uipath.com/u/Yoichi)\
**Post date:** [February 24, 2023, 3:14am UTC](https://forum.uipath.com/t/compare-current-row-with-previous-row/520489/2 "2023-02-24T03:14:56Z")

</div>

Hi,

> [@flyingdragon](#):
>
> dt.rows.indexof(row)\>0 and row(“Company Name”).ToString = dt(dT.Rows.IndexOf(row) - 1).item(“Company Name”).ToString

Can you try AndAlso instead of And as the following?

```
dt.rows.indexof(row)>0 AndAlso row("Company Name").ToString = dt(dT.Rows.IndexOf(row) - 1).item("Company Name").ToString

```

Regards,

---

<div class="post-metadata">

**Author:** ![flyingdragon](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/flyingdragon/32/287509_2.png) [@flyingdragon](https://forum.uipath.com/u/flyingdragon)\
**Post date:** [February 24, 2023, 3:18am UTC](https://forum.uipath.com/t/compare-current-row-with-previous-row/520489/3 "2023-02-24T03:18:42Z")

</div>

This works! but may i understand why we should be using AndAlso instead of And?

---

<div class="post-metadata">

**Author:** ![Yoichi](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/yoichi/32/523754_2.png) [@Yoichi](https://forum.uipath.com/u/Yoichi)\
**Post date:** [February 24, 2023, 3:24am UTC](https://forum.uipath.com/t/compare-current-row-with-previous-row/520489/4 "2023-02-24T03:24:46Z")

</div>

Hi,

It’s because `AndAlso` evaluate the expression as short-circuiting. (And `And` is non-shortcircuiting) Please see the following in details.

> [@When we use And, Andalso, or, orelse](https://forum.uipath.com/t/when-we-use-and-andalso-or-orelse/493570/4):
>
> Hi, Why u use andalso? what is difference if i use and AndAlso is short-circuiting logic. If the first expression returns false, behind expressions are never evaluated. In most case, it’s better than And because of performance. Is it possible to do multiple condition using Else If? so i dont need to add if acitivity one by one Yes, we can use Else if as the following. Hope this helps you. Regards,

> **[AndAlso Operator - Visual Basic](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/andalso-operator#remarks)**
>
> Learn more about: AndAlso Operator (Visual Basic)

Regards,

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/system/32/182466_2.png) [@system](https://forum.uipath.com/u/system)\
**Post date:** [February 27, 2023, 3:25am UTC](https://forum.uipath.com/t/compare-current-row-with-previous-row/520489/5 "2023-02-27T03:25:34Z")

</div>

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