views:

80

answers:

1

Good Morning,
I have a rather odd thing happening with an Excel WorkSheet that I am creating through .Net. I load up an existing workbook

Dim _xlApp As New Excel.Application
Dim _xlWorkbook As Excel.WokBook = _xlApp.Workbooks.Open(_templateFileName)

set it to be visible before populating it (for debugging purposes)

_xlApp.Visible = True

I then go on to populate the existing template.

Private Sub GenerateOceanFreightGrid()

        Dim columnCount As Integer = 4 ' _standardColumnCount
        Dim columnInsertPosition As Integer = 5

        If MyBooleanValue() Then
            _xlSheet.Columns(4).Insert()
            _xlSheet.Cells(7, 4) = "My Header Value"
            columnInsertPosition += 1
            columnCount += 1
        End If

When i set a breakpoint on the "Private Sub Gen.." line, and step through the code line by line, it inserts a new column for each variable declared, so in this case it will insert two columns before it even hits the "If MyBooleanValue()..." line (I can watch it actually happen due to the Excel app being visible). However, if i dont set a breakpoint and just let the code run straight through this doesnt happen.
There is no multi threading happening or anything else that i suspect would have any impact on the code. My obvious work around is to not set the breakpoint, i was just wondering if anyone has seen this happen before and for what reason? Thanks

+1  A: 

I suspect this is due to some property that the debugger is evaluating in order to be helpful - but it's an evil property with the side-effect of adding a column.

I assume you don't have any Watch variables which could be doing this explicitly?

Jon Skeet
Thanks Jon, but no, i don't have any Watch Variables set. It's very odd though, becuase if i dont initialize the variables in the above example, it only inserts one rogue column, but if i do (as in the example) it adds 3 columns. I can't quite fathom why its adding a column per variable initialized and why it only happens when stepping through the code. Very weird.
Ben
also turn off implicit property evaluation in Debugger Settings. Debugger evaluates ToString()
Sergey Mirvoda
Thanks @Sergey and @Jon, i turned off Implicit Property Evaluation and now it does the "right thing". Cheers.
Ben
@Ben this feature completely sucks. And beat me more than once. Also breakpoints does not hits on prop eval (VS6 debugger can break on watch window evals).
Sergey Mirvoda
@Sergey: Why would this be happening if Ben does not have any Watch windows? I don't see how this is possible. Can you (or anyone else) explain?
Mike Rosenblum
@Mike simply hover mouse over any object property and debugger immediately invokes ToString method over returned property result. it is called Implicit Property Evaluation and yes it causes side effects. that is another reason for CQS.
Sergey Mirvoda
Yes, ok, I knew that -- and am aware of the possible side effects. I didn't realize that Ben was hovering. in general, though, should one be creating properties that have side effects? In this case, it looks like the debugger must be interpreting the line '_xlSheet.Columns(4).Insert()' as a property instead of as a method and, therefore, executing it?? It doesn't sound possible, and, yet, I don't see what other line could be doing this.
Mike Rosenblum
it is a _visual basic_
Sergey Mirvoda
@Sergey: "It is a visual basic" LOL. :-) That said, I still don't see the problem, unless VB.NET interprets methods that return values as well as properties? Here it is interpreting the Insert() method, which, has a pretty obvious side effect! (To someone that can read English, but not to the compiler.) Ugh, what a disaster. I guess it is doing so because, the Insert() method does not follow CQS and actually returns a result. Ugh...
Mike Rosenblum
@Mike not sure but in my opinion bug is in Columns property Ben checked Columns count via hovering mouse over it, viola - side effects, but why only one column... only BillG knows)) as for visual basic, it looks weird for c# programmers parametrized property columns looks like method http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.columns.aspx
Sergey Mirvoda
@Sergey: Yes, I was a long-time VBA/VB6/VB.NET guy before moving to C#, so I definitely get the paremetrized properties. Bill doesn't state what he's hovered over, though. Actually, he says that it happens *before* the 'If MyBooleanValue() Then' line that he shows above. In don't see how the 'Columns' property could have a side effect of inserting -- it truly is just a property that returns a Range object, but takes no action in the worksheet. If it is not the 'Insert()' method (which returns a value, so it's a 'function') then it must be some other member that would achieve an insertion.
Mike Rosenblum
@Mike yes I also VB5..VB.NET programmer )), and also checked Columns property, moreover now I think side effect is in Ben's code, not in interop code. also for me the main question is How Ben perform checks of column's count.
Sergey Mirvoda
@Sergey: "moreover now I think side effect is in Ben's code". I think you are right. Ben must have a property that inserts as a side effect, I guess. It should be a method instead.
Mike Rosenblum