Brandon Brandon - 4 months ago 33
C# Question

Printing and using Var within a ListObject forloop

I currently have this C# code that has access to table1. I am having an issue however with Var listRowsValue and var lr. How can I print these and subtract 1 off of ListRowsValue and not view "System._ComObject" as an output? How can I resize ListObject to not check another row at the end of the count?

var listRowsValue = xlWorkBook.Worksheets["Sheet1"].ListObjects["table1"].ListRows;

for (int CurrentRow = 0; CurrentRow < listRowsValue.Count; CurrentRow++)
if (xlWorkBook.Worksheets["Sheet1"].ListObjects["table1"].Range[CurrentRow, 2].value == -1)
xlSheet.Cells[5, 5] = "YES!";
var lr = listRowsValue[CurrentRow];
Console.WriteLine("CURRENT ROW: " + lr);//Why does this not work?

//How do I resize the total listRowsValue count here so it doesn't check another row at the end?
//EXAMPLE: ListRowsValue = ListRowsValue - 1;
//CurrentRow = CurrentRow - 1;
xlSheet.Cells[5, 5] = "NO!";

Old VBA code I'm converting from:

For CurrentRow = 1 To Tablesize
If Lo.ListColumns("Column2").DataBodyRange(CurrentRow) = -1 Then
MoveEmUpOne Lo, CurrentRow, Tablesize
Tablesize = Tablesize - 1 'table didn't really get smaller, but number of "real"rows decreased by 1
CurrentRow = CurrentRow - 1 ' check this row again -- stuff from below might need cleard
End If


The usage of VSTO is quite different from regular C# libs because it depends on COM intensively.

lr is a ListRow object.

Use lr.Range.Text to get the text in it.

Console.WriteLine("CURRENT ROW: " + lr.Range.Text);

See Range.Text.