How to check a previous value in a variable in a for loop?

Hello,

I am looping through a datatable in the following manner:

I want to create variable that houses the previous value of PHYID, (basically the value before the current value it finds in the regular loop) to use as a comparison later.

How would I do this in code?

Hi @ajeffers,

Define an index inside the loop.Then you can assign as follows.

dtResult.rows(index-1)(“ColumnName”).ToString

Regards,
MY

What would I set the index as? 0?

It will start from default 0. You create that index in the for, it will increase as the loop continues. When you stop according to a condition, -1 of this index will bring you the previous row.

Code worked fine but then it threw an error :frowning:

“Assign: There is no row at position -1.”

What would I do to bypass the initial header?

By adding a condition, you can say don’t do this assign operation in case index = 0.

1 Like

Thank you! This seems to work :slight_smile:

1 Like

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