Jump to content

Line Number in message box display


Recommended Posts

I couldn't find anything on this in the help files or by searching the forum, so I'll ask.

Is there a way to automatically have the message box display the line number that called it? 

I use the msgboxes a lot in debugging and I have started putting the line number as the title of the msgbox:

EX:

Msgbox (0,"-57-","The value for $stringinput is not as expected == " & $stringinput)

which stops the script at line 57 and tells me why.

Unfortuantely, as I add lines above line 57, this message box can easliy get moved. If I paste in a lot of stuff, it can be moved down 100 lines, or more.  Of course, if this happens and I don't change the title of the msgbox, I start looking at line 57 to figure out why the variable isn't as expected and I can have trouble when it isn't where I expect it.

Of course, I can just search for "-57-" and I can find it, but if there was a way for the msgbox function to automatically display the line number, it would be helpful.

Thanks,

redpicker

Link to comment
Share on other sites

Try macro @ScriptLineNumber.

According to the docu: "Line number of the currently executed script line. Useful for debug statements specially when a function is call you can pass the caller line number. (Not significant in complied script)"

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

To automatically get the line number you can set @ScriptLineNumber as a default value for a optional function parameter, then the function will know when it was called from.

Example:

Local $stringinput = "test abc"
_MsgBox("The value for $stringinput is not as expected == " & $stringinput)
_MsgBox("The value for $stringinput is not as expected == " & $stringinput)

_MsgBox("The value for $stringinput is not as expected == " & $stringinput)

Func _MsgBox($sText, $sTitle = @ScriptLineNumber)
    MsgBox(0, "-" & $sTitle & "-", $sText)
EndFunc
Link to comment
Share on other sites

That's great!  I am not surprised that hadn't run across that before; I really don't understand much of this stuff.  I hack away and have figured out a lot, but there is so much that I don't know that I am amazed that I have been able to do what I can do.

I would really like to figure out how to use the debugger, but I feel that it is over my head.  I know it really isn't, but I just haven't had the time to figure it out.  Maybe Pincopanco's link will do the trick.  I'll give it a look.

Thanks, again!

Link to comment
Share on other sites

  • 2 weeks later...

My defalut mesg box for years has been defined in the abbreviations file as

mb=MsgBox(0, @ScriptName,  'Line ' & @ScriptLineNumber & @CRLF & '---' & $| & '---')
mbb=MsgBox(0, @ScriptName,  'Line ' & @ScriptLineNumber & @CRLF & '--- | ---')

recently I added.

mbe=MsgBox(0, @ScriptName,  'Line ' & @ScriptLineNumber & @CRLF & @CRLF & 'error ---' & @error & '---' & @CRLF & 'extented ---' & @extended & '---' & @CRLF & @CRLF & 'will exit')|nExit
 

type the key and hit space.  Instant custom mesgbox with cursor placed where you need it next, to type the var in one case or a text note in the other.   The dashes are for easy detection of spaces at the start or end of your var if it happens to be a string.  Downside is it can be hard to tell at a glance if it was a neg number.   ----1---   and ---1---   look pretty close.  3 dashes and a neg are close to 3 dashes in a mesg box.

I had a thought this afternoon.  I wonder if I can stick @compiled in there somehow and removed the end result of  "line -1" that a compiled version gives on the mesgbox while leaving it there for the script vers to still give actual line numbers.  But I have not played yet with that idea.  I want to keep it to one line.  I often use "mb" or "mbb" as quick tests and then the editors hotkey Ctrl Q to comment and uncomment the mesgbox lines as needed.  Hence my desire to keep it to a single line.

Be aware that @scriptname can't tell if something is in an include you wrote or the calling script.  Includes get counted as the reg script and returns that name, not its own.  Found that out the hard way when I was using some vars that where defined in both.  Last one wins in that case.   lol

I need to stop getting lazy with the global vars in cases like that.

Link to comment
Share on other sites

ah.

mb=MsgBox(0, @ScriptName, StringReplace('Line ' & @ScriptLineNumber,'Line -1','') & @CRLF & '---' & $| & '---')

does the trick.

Hmm.  Now I miss the familar -1.   I think I'll leave it as it was.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...