Why vb code is not executed step by step by using F11? - vb.net

I am new to vb. When I started to work on this new project in vb.net 2010, I put many breakpoints to try to understand the execution order of the project, only to find it in vain.
Step into command F11 should work correctly according to Step Into Property/Function (F11) doesn't work as expected. But I when I pressed F11, I found the code is jumping from one place to another based on breakpoints, not line by line or step by step.
To give an example, please see the code below
Me.tcData.Alignment = TabStrip.TabControl.TabAlignment.Bottom 'line 1-breakpoint
Me.tcData.Dock = System.Windows.Forms.DockStyle.Fill 'line 2
...
Me.tcData.TabsDirection = TabStrip.TabControl.FlowDirection.LeftToRight 'line 3
Public Property Alignment() As TabAlignment 'The property 1 called by line 1
Get
Return m_Alignment
End Get
Set(ByVal value As TabAlignment)
m_Alignment = value
AdjustHeight()
PositionButtons()
For Each t As TabPage In TabPages
t.Alignment = value
Next
End Set
End Property
Public Property TabsDirection() As FlowDirection 'The property 3 -breakpoint
Get
Return m_TabsDirection
End Get
Set(ByVal value As FlowDirection)
m_TabsDirection = value
SelectItem(Nothing)
End Set
End Property
When I press F11 at line 1, it goes to the property 1. After it returns, when I press F11,it goes to property 3 directly, without accessing the code in line 2 and line 3.
I do not understand why the code is NOT executed step by step by using F11. If I put breakpoingts in line 2, then line 2 is executed.
So it seems to me that the showed execution order is based on breakpoints! So if I put breakpoints at different places, the showed execution order would be different! Thus, it is impossible for me to really understand the execution order.
Thanks!

When you tell it to Step Into, it follows the exact code path. So in order to calculate TabStrip.TabControl.TabAlignment.Bottom in line one, it first has to reference TabStrip, then look up the TabControl property, then look up the TabAlignment property, then-- Here's where it jumps to the property 1 label, right? That's because it has to execute the Get section for the TabAlignment property in your code. Once it executes that, it knows what the reference is, and so it returns that back to the previous level of execution, at which point it can look up the Bottom property. Now, it can assign the value to the Me.tcData.Alignment property.
The same goes for line 3: In order to know where it is assigning TabStrip.TabControl.FlowDirection.LeftToRight to, it has to evaluate the Me.tcData.TabsDirection property, which involves executing your code in the Get section of your TabsDirection property.
So, in short, you are seeing the exact execution path that the code is following, including all the "sub-evaluations" that have to be performed to calculate both the "source" and "destination" properties. Depending on the debugger configuration, it might not show this for system code, but if you set the debugger to be as verbose as possible, it will jump to the Get definition for every property you reference, including system code. There are 5 property lookups for System.Windows.Forms.DockStyle.Fill, just to assign it to a local variable.

Related

How to pass params to Excel button macro

I'm trying to find a way to pass params to a macro that's assigned to a button in Excel (2010). I've seen a number of proposed solutions in this thread but none of them work for me - maybe due to my setup?
I have a macro defined in an Add-In file (macros.xlam) that takes no parameters. I have added a button in my *.xlsm file and attached the macro to it. When I click the button macro gets called and it works fine. My macro is called Export and it's inside CSV module so in the button's property I'm calling CSV.Export.
I have added an int parameter to the macro so the definition looks like this now:
Sub Export(a As Integer)
....
End Sub
Now I want to make the button pass 1 as the parameter. I set a breakpoint in first line of the Sub and tried various approaches but none worked:
'CSV.Export 1' - when I click on the button nothing happens, breakpoint doesn't catch. Additionally when I edit the macro again it turns out that what I entered was automatically replaced by 'sheet_name.xlsm'!'CSV.Export 1'. If I leave it at that it still does not work.
'CSV.Export(1)' - when I click on the button nothing happens, breakpoint doesn't catch. This one does not get automatically replaced.
'macros.xlam'!'CSV.Export 1 ' - when I click on the button nothing happens, breakpoint doesn't catch. Upon later edit it turns out that it gets replaced by 'sheet_name.xlsm'!'CSV.Export 1'
'macros.xlam'!'CSV.Export(1)' - when I click on the button nothing happens, breakpoint doesn't catch. Upon later edit it turns out that it gets replaced by 'CSV.Export(1)'
CSV.Export 1 - when trying to close button property window displays an error that the formula contains an error and won't allow to leave it like that
CSV.Export(1) - this return an error saying that "Formula is too complex to be assigned to object".
I want my macro to be customizable (users being able to pass parameters to it). At the same time I want to avoid hacky hard-coded solutions like calling the sub with no params and then always assuming that cell A1 has first parameter, A2 has second and so on.

Unbound DataGridView add row with checkbox error

This is a Windows forms program. I have an unbound DataGridView control with four columns; Feature ID as string, Parts as string, Flats as string and ShowOnEst as boolean. The columns were defined in the form designer and the boolean column is, indeed, set to be a checkbox with tristate set to false.
The user selects a product from a list box and this code is executed to populate the grid
grdFeat.Rows.Clear()
For i = 0 To curProduct.lstFeatures.Count - 1
With (curProduct.lstFeatures(i))
grdFeat.Rows.Add(.FeatureId, .Parts, .Flats, .ShowOnEst)
End With
Next
If AllowUserToAddRows is false, everthing is good. Setting AllowUserToAddRows to true, however, results in the following error:
FormatException
Value '' cannot be converted to type 'Boolean'.
The error is NOT triggered in my code. It's displayed in a new tab titled "No Source Available".
It seems pretty clear that the process that adds the new editing row is trying to set the checkbox to a string value, or perhaps NULL. I'm very new (a few months) to vb.net, so I suspect I'm overlooking some simple setting somewhere, but after several hours of trying to find it, I'm starting to feel a little foolish here.
I'd give you a list of things I've tried already, but it's a long one. :) I even tried to go around the issue by adding a new row manually, .add("","","",False), but that too gives the same error, even though I'm telling it what to put in the checkbox.
What am I missing? Also, can someone point me to an explanation of how that new editing row thingy works?
From what you explained, I have no real explanation for the problem.
A workaround may be to set AllowUserToAddRows to false before populating the DataGridView and set it to true after.
Found it. Hope it helps someone else...
The rows being added are uncommitted rows. To solve it, capture the row added index, set the current cell and notify that the added row is dirty:
grdFeat.Rows.Clear()
For i = 0 To curProduct.lstFeatures.Count - 1
With (curProduct.lstFeatures(i))
Dim p = grdFeat.Rows.Add(.FeatureId, .Parts, .Flats, .ShowOnEst)
End With
grdFeat.CurrentCell = grdFeat.Rows(p).Cells(0)
grdFeat.NotifyCurrentCellDirty(True)
Next
Well, with further experimentation, this isn't a total fix. The problem is still there, but at least now handling the DataError event allows me to do something with it in code. Without these lines, the error occurs before the DataError event is triggered.
Still puzzled.

Update and use a Variable in an expression in SSIS

I am trying to use a variable that I am updating in a loop inside an expression in SSIS. I am going to trying to be as clear as possible. Before to arrive to the last Data Flow Task (where I have a ODBC source), I am getting some values inside the variable (Return) that I am going to use in the [ODBC Source].[SqlCommand], with the second Script Task I could checked that the variable is been updating in the way I need. The problem is that in the Data Flow the expression is taking into account this variable but with the Default value that I have choosen ('').
In the first Script task I am updating the variable:
Dts.Variables["Return"].Value = Dts.Variables["Return"].Value + identif;
So, I do not know if I missed a previous indication or what could be happening with the execution of this control flow
The Variables window will not show you the value of the variable at the end of package execution. You need to set breakpoints and use the local window to "catch" the value of the variable before and after it changes.
Breakpoints Example
I have a package with a single script task.
The code inside is this...
new string strReturn = "NewValue";
public void Main()
{
// TODO: Add your code here
Dts.Variables["Return"].Value = strReturn;
Dts.TaskResult = (int)ScriptResults.Success;
}
The code sets the my SSIS variable equal to NewValue. To catch this variable changing values I will set breakpoints. To set breakpoints, right-click the Script Task and select Edit Breakpoints.
Set a Pre and PostExecute breakpoint. Save and then execute the package. When it reaches the breakpoint, it will pause. The first pause is PreExecute event, just before the script is about to execute. During this pause, you can enter the name of your variable into the locals window.
You will see that the value has not changed yet. Press the continue button to move to the next breakpoint, PostExecute. Now, the Script Task has been executed. Check the local window and you should see that the value has changed!
Notice that throughout each of these the top left Variables window never shows an updated value. It always shows '' . You must use breakpoints to see a variable value changing. Hope this helps you to debug your package!

Dataset is empty while it is not empty at first

I have the following piece of code
_Foo = String.Concat(_Foo, " Var = '", _varValue, "' ")
_Bar = DsInvoice.viewInvoice.Select(_Foo, _Order)
If _Bar.Count > 0 Then
When I place the debug lines, the Dataset is actually returning rows, and _Bar has a count of 116. Yet when it is at the 3rd line (the if statement) _Bar is empty and the count returns 0. Without any reason I just lost my data.
Let me know if you need more information (I'm actually a PHP programmer and I had to fix a legacy bit of code :(. I lack VB experience to give more background information on this code.
_Bar has no rows because there is no value equal to the _varValue in the "Column" (a.k.a "Property") in the datatable.
*Even if the Property is numeric I doubt wrapping it with single quotes will matter. *
Tip: F8 (VB.Net) or F11 (C#) to step the code control (Yellow Line) onto the second line of code. Then hover your mouse over the viewInvoice word on the second line shown in your question, you'll see the ObjectBrowser Tooltip appear and in it will be a Magnifying Glass with a dropdown. Click it (the dropdown) and select the DataTable View to help you debug this problem.
You can change the Select(...) criteria in run/debug time and then (by dragging the yellow arrow in the line number margin up a line) see the effect of the different Select criteria to work out what is going wrong.

Showing the updates being performed by macro

So I have seen all the ways to hide the screen updating in excel, what I'm looking for is a data problem, so I WANT to see the screen update. Is there a way to code the screen to update during the macro slowly? I tried the following:
'//////adding screen updating to watch what's happening
Application.ScreenUpdating = True
Application.Wait Now + TimeValue("00:00:01")
'/////
and all I got was screen flicker.
Thanks for being excellent unto me.
What you're after is a better method of debugging your errors. ScreenUpdating property of the Application is not what you want :)
Instead, set a breakpoint in your code using F9, at or before where you suspect the error is happening. If necessary, you can put this on your first line of code (if you literally have no idea where the error happens).
Here is a breakpoint, it's the red/maroon highlighted line:
Then, using the F8 key, you can step through the code line by line.
You can add as many breakpoints as you want. Pressing F5 will execute the code only up to the next breakpoint, then you would have to F8 or F5 to resume execution.
Some additional things to help you debug
Use the Locals window.
This shows you the vairables in scope, and you can click on them to view their properties/etc. As you can see, I have a shitload of variables in a fairly complicated application. I've found the immeidates window is very helpful for Object variables and arrays. Here is what it looks like when I expand an array variable, so that I can see all its contents:
Use the Immediate window to query variables or execute statements outside of your code, this is equivalent to Debug.Print statements. For example, let's check to see if the value of some variable is "9", you can:
?someVar = 9 and then press enter. It will show you True or False. You could query the value directly by ?someVar and it would print the value.
In this screenshot, I am checking the value of a boolean, and it shows me that the value is False. I could also verify this in the Locals window.
The screen updating must be set only once, at the start of the code.
But the Wait you will need to put inside your loop, or spread between your code lines.