Using Msgbox as a debugging tool


#1

I haven’t had much success with the Chrome debugger so I have gone back to my old way of doing it in vb6. However, unless there is some way I am not aware of, I can only put 1 msgbox at a time and only 1 variable. The program does stop at the msgbox but doesn’t continue after closing it. This makes the process very slow. Is there another method or way around the 1 at a time thing. In my case when I insert the word “debugger” into my code, I can’t tell if it stopped there or not. This would be ideal so I can use F8, etc. to trace through the code. I will keep trying but would like some advice or answer on my above dilemma. Thanks for any help. I am converting vb6 code to NSB. Incidentally, how would the following vb6 code be converted?
For lngnumber = Val(strPoint) - (intCnt - 1) To Val(strPoint)
Whatever I try doesn’t work.


#2

The Chrome debugger (Dev Tools) is an excellent resource but does have a learning curve. Placing console.log(yourVariableHere) in your code will display info in the console. Debugger statements should halt code execution as expected if the debugger is open (F12 on Windows). Some info on the Chrome debugger can be found in the AppStudio docs at Using the Chrome Debugger.

Though MsgBox statements display a modal dialog, code execution is not halted. Only the first dialog will display. See NSB.MsgBox for more info.

There is a way to daisy-chain your NSB.MsgBox statements together, but it requires placing them at the end of a function and then calling a response function to run when the OK button is clicked/tapped. Code execution would essentially be halted until a response is made, allowing inspection of the code in the debugger. Debugger statements might be more practical, however. The daisy chaining would look kind of look like this:

Sub Main()
  MsgBox FirstResponse,"First."
  console.log("first")
End Sub

Function FirstResponse()
  MsgBox SecondResponse,"Second."
  console.log("second")
End Function

Function SecondResponse()
  MsgBox ThirdResponse,"Third."
  console.log("third")
End Function

Function ThirdResponse()
  MsgBox "Third."
  console.log("fourth")
End Function

Try something like:

Dim lngnumber
Dim strPoint = "5"
Dim intCnt = 3

For lngnumber = (CLng(strPoint) - (intCnt - 1)) To CLng(strPoint)
  console.log(lngnumber)
Next

Also take a look at Conversions in the docs.

Kind regards,
Doug


#3

Doug,
Thank you so much for your quick reply. I will certainly take another look at the chrome debugger. Does the chrome debugger have to be open when I insert the log commands in the main code? I have read the instructions so I was disappointed when it didn’t work right. i am not the sharpest tack in the box.

I am using For lngnumber = Number(strpointno-(intcounter - 1)) To Number(strpointno)
I thought that “Number” converted strings to numbers but maybe I should use what you did “Clng” or does it make a difference?
I wouldn’t be able to give values to strpoint and intcnt ahead of running the app since they are not determined until a previous routine executes but there was no way you could have known that. When I do assign the numbers tor testing, most of it works.
May I place global variables as parameters to a sub? I have found that I have to initialize some variables to 0 in a dim before using them for the first time…
You may be hearing from me again but I really, really do thank you for what you shared so far.
Jim Schuchert


#4

Sometimes a video or tutorial can help too. Maybe do a search for JavaScript console.log how-to or similar search for how to use the debugger statement. Setting a breakpoint in the chrome debugger (click on a line number similar to how it is done in VB) will halt code execution too.

To answer your questions:

  1. You would place the console.log() or debugger statements in your code within AppStudio at design time (as my example shows), then see their effect when deploying the project to the local browser (or elsewhere). When the app runs, open the Dev Tools window, then refresh the page and go from there.

  2. Number() works great too, but be sure you feed it integers if for a For/Next statement. CLng() converts to a long integer. The Conversion doc page details conversion options. Your For statement above needs to be modified slightly to appear like this:

For lngnumber = (Number(strpointno-(intcounter - 1))) To Number(strpointno)

The extra parans are needed due to an apparent translator issue within AppStudio. If George is reading this, maybe he can confirm whether that’s true. Your syntax “should” work, but it didn’t here for me either.

  1. Re: declaring vars, my apologies; I was attempting to provide a complete example. Your own code would assign variables as required, often not in Dim statements like that.

  2. Sure, global variables can be used in Subs/Functions too.

Good luck in your learning, and ask away to the group anytime.

Kind regards,
Doug


#5

Yes, I see the translation problem as well - we’ll get it fixed.

Thank you!

George Henne
NS BASIC Corporation
http://www.nsbasic.com


#6

Use alert to stop execution until OKed:

alert("the variable name is: " & varName)

John


#7

John,

The “alert” command works great and helps me examine multiple variables rather than 1 at a time. Thank you so much.

Jim


#8

Doug,
This is embarrassing but you are trying to get through to an 82 year old brain that seems to have been locked at present.

Here is what I am doing:
Insert “debugger” at a place in my ide code after which the guts of the routine are executed.
Start the app in the desktop browser and run it to completion. It has created values used in the next sub routine.which is where the debugger statement is found.
Open the dev tools. The dev tools window has covered the original program window.
No useful information in the dev tools console but a replica of the code (in JS) appears when I tab to "Sources)
Refresh the window.
Use F8 to trace through the code.following debugger.
Look at code after debugger and hover over variables with no indication of their values.
Obviously I am lost or have missed something that is critical…

Briefly, I run an angle-distance traverse and check it for closure using the “compassrule” method. That is where I am hung up. The vb version works but I can’t get the appstudio one to work. I have the results from the VB and want to compare them with appstudio which is why I am asking all these questions.
If I am not making sense, it’s because I haven’t had my nap yet…will chick back later.


#9

The values of the variables should show when you hover over them in the Sources tab.

You can also switch to the Console tab and enter the variable you would like to see the value of - it will display.


#10

Hello again guys,

I have isolated my problem but haven’t corrected it yet but now will take the time to figure it out. Also, I suddenly began getting weird results to simple math but I think it’s because I haven’t cleared my cache for a while. My thanks again to you for the time and effort you expended to help. I still haven’t gotten the chrome debugger to work for me but that is on my list.

JIm


#11

No worries, everyone had to pick these things up before they could use the tools. Also, like the alert() tool, the console.log tool can handle multiple variables by separating them with commas console.log(varA, varB, varC).

Give this a try to test the debugger statement:

  1. Insert a debugger statement in your code
  2. Launch the app locally
  3. Open Dev Tools (F12)
  4. Resize the Dev Tools window so as not to cover your app. There options to move it to the side of the browswer window, the bottom, or to pop it out are display in a separate window.
  5. Refresh the browser window and interact with the app until the debugger statement is encountered, then the code should halt execution there and highlight that line of code in the debugger window.
  6. At this point you should be able to hover over variables to see their values, plus there is a scope window in Dev Tools that displays some values. Using F8 should work through your code one line at a time, but not necessarily your code only. The included AppStudio code also responds to F8 use.

As an example, I’ve opened the HelloWorld sample project and placed a debugger statement just before the MsgBox code. Then deployed the app and clicked on the button while the Dev Tools window was open. You can see the desired results. I’ve also circled the various areas that may be of interest to you as you work through what’s there.

Now that I’ve written these things out I realize they may or may not be helpful depending on your browser setup but take a look and see if anything is useful.

Kind regards,
Doug


#12

Thank you, Doug. I will do it in the morning and report back during

the day sometime. I thought variables were not case sensitive but today one would not reveal its value until I changed one of the letters. I was using “alert” and maybe that’s why.


#13

Doug,

The “Hello World” or the other samples will not open in my browser although my code does. Also, F12 does not open the dev tools window. I have to get to it the long way but I will continue to use your instructions and see if I can streamline my debugging efforts. Thank you so much for the time you are spending.

Jim


#14

Copy that, Jim. I hope you’re able to make some headway with it.

Kind regards,Doug


#15

What happens when you try to open HelloWorld?


#16

The translation problem is fixed in the next build.


#17

Nothing. Code remains but nothing in chrome browser.


#18

The object of this app is to effect a closure on an angle-distance traverse by applying the “compass rule” adjustment. This sub uses the total length of the traverse and adjusts each leg by a formula. This will change the lengths of each leg and the coordinates of each point. Each point with its coordinates is then stored in a database. The beginning and ending points would theoretically have the same coordinates or very close to it.
The VB6 code I am converting works perfectly for this task but so far the converted appstudio code has not. After many hours of head scratching and much appreciated help from Doug and George, I think I have narrowed the problem down to this:
The formula adds latitudes (lat) and departures (dep) to the previous coordinates to get new ones. Critical lines of my code state:
lat=lat + (dist*multiplier)
dblnorth2=dblnorth2+lat
This is what happens…say dblnorth2=2084.123 and lat=-3.123
I assumed that when lat is added to dblnorth2 the result would now be 2081.000 but it displays (using alert or msgbox) as 2084.123-3.123 and is stored like that in the database. Obviously that is wrong. Can anyone see where the code is screwy, my logic is flawed or it is a bug. I even thought somehow I might have typed everything as strings so I prefaced the code with "Number"to convert it to numbers but it didn’t help. I certainly hope I have overlooked something and it is a simple fix. I would be happy to post the entire code of the sub if anyone is interested but I believe the 2 lines above are where the problem lies. Sorry to take up so much space and your time.

The “Storecoord” code is as follows:
Sub storecoord()
nnext=UBound(coord)+1 'actual size of array
If strdes="" Then strdes=“None"
coord[nnext]=strpointno & “,” & FormatNumber(dblnorth,5,True,False,False) & _
”," & FormatNumber(dbleast,5,True,False,False) & “,” & strdes & vbCRLF
localStorage.data02 = JSON.stringify(coord)
End Sub

Thanks so much for any advice or solution. I will check back in the morning.

Jim Schuchert


#19

You ned to convert each item to a number:
dblnorth2=dblnorth2+lat
becomes
dblnorth2=Number(dblnorth2)+Number(lat)

Helen


#20

To add to what Helen mentions, FormatNumber() is for display purposes. In order to add thousand separators, leading zeros, etc. to a number, the function converts any input into a string, and then concatenates them. Save your number formatting tasks until all the math is completed.

Kind regards,
Doug