On Exit question - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://staging.qb64phoenix.com/forumdisplay.php?fid=10) +---- Thread: On Exit question (/showthread.php?tid=1713) |
RE: On Exit question - bplus - 06-02-2023 BTW there is no "On Exit" thing in QB64 There is a bunch of On Somethings but Exit is not one of them. _Exit is a Function that tells you mainly the user wants to quit they clicked the top right X button to quit or some key combo that usually lets them escape the app. Opps OK maybe ON _Exit... might work https://qb64phoenix.com/qb64wiki/index.php/ON...GOTO This is poorly worded in my opinion
NumericalExpression evaluates to a number, that codes which line label or line number to goto. But yeah, _Exit returns numbers according to what way the user attempts to quit. Would On _Exit() work better? RE: On Exit question - bplus - 06-02-2023 @NasaCow Search can't find ON EXIT anywhere in your code you posted. This is what I do find: Code: (Select All) 'Disabling the default exit routinue Code: (Select All) ShutDown: Does not look good to set ExitFlag Twice nor putting Shutdown on Timer. When you know the user wants to quit from _Exit then you run the save work code if not saved and quit or just quit if work already saved. I don't see need for Timer sub here. RE: On Exit question - TerryRitchie - 06-02-2023 (06-02-2023, 12:28 PM)Dimster Wrote: Hi Terry - hope I'm not taking this tread down a different road but on that Option_Explicit, do you find it highlights an "undefined variable" error when the variable is just a local loop control type. I don't DIM or DIM SHARED those loop control variables. I'm under the opinion that all variables should be declared. OPTION _EXPLICIT in your code will require this for every single variable. By declaring all variables you get the added benefit of having a detailed listing of your variables followed by a REM statement after each explaining its use: Code: (Select All) DIM lineStart AS Type_Vector2 ' start vector of line (x,y) This is a habit, in my opinion, that every programmer should get in to. It GREATLY helps when debugging your own code or asking someone else to have a look at it for help. The above code snippet contains the variables I use in a function that detects a point on a line. Imagine trying to debug this code without declaring the variables and having a description associated with them. Here is the entire function : Code: (Select All) FUNCTION PointOnLine (TestPoint AS Type_Vector2, __line2D AS Type_Line2D) RE: On Exit question - bplus - 06-02-2023 and ironically Option _Explicit makes no difference in the example Dimster gave us Code: (Select All) 'Option _Explicit ' <<< try this with and without comment, no difference RE: On Exit question - Kernelpanic - 06-02-2023 (06-02-2023, 02:21 PM)bplus Wrote:(06-02-2023, 11:19 AM)Dimster Wrote: When I get a "Duplicate definition" error it's seems to always be related to my misuse of the CONST statement. Somewhere in my program I tried to change the variables value when it supposed to have been fixed. A const variable is just a normal variable. If it is misspelled, the following happens: In QuickBasic (Basic in general), a new variable is (was) initialized with 0 by default until it is assigned a value. That means every typing error leads to a new variable with the value 0. Example of a typo for a const variable (calculate the circumference of a circle): And this is how the result is correct: RE: On Exit question - bplus - 06-02-2023 KP "A const variable is just a normal variable." Then why bother with Const? because it is not just a normal variable. RE: On Exit question - Kernelpanic - 06-02-2023 (06-02-2023, 08:27 PM)bplus Wrote: KP "A const variable is just a normal variable." So that it cannot be accidentally changed in the program. The example above is simple, but in real programs there should be enough reason that certain variables not be can changed in the program (not paying attention!), or by the user. RE: On Exit question - Dimster - 06-02-2023 Quote:and ironically Option _Explicit makes no difference in the example Dimster gave us Not sure what you mean B .... The example with NO Option _Explicit runs fine with no error, but if you place Option _Explicit at the beginning of that code you get an error warning of an undefined variable. Also Kernelpanic ... you mentioned " If it is misspelled, the following happens ".... actual my comments were if it "misused". I suppose that's splitting hairs because I do often misspell an often used variable which I believe you are pointing out Option _Explicit is ideal to catch. But the point I was trying to make, is that there are number of control variables ( ie For x = 1 to 10, or For i = ?? to ??), these control variables I don't think are usually Dimensioned and a program can have a ton of them. Option _Explicit seems to force you to Dimension them. RE: On Exit question - mnrvovrfc - 06-02-2023 (06-02-2023, 09:33 PM)Dimster Wrote: But the point I was trying to make, is that there are number of control variables ( ie For x = 1 to 10, or For i = ?? to ??), these control variables I don't think are usually Dimensioned and a program can have a ton of them. Option _Explicit seems to force you to Dimension them. It doesn't "seem" to force anybody to do anything. Any variable, regardless of its role, has to be dimensioned (declared) if OPTION _EXPLICIT is in place. What if you wanted to use variable "i" or "x" for something else having to do with integers? "DIM" keyword caused a fair amount of confusion because "traditionally" it was used to create arrays. Especially in GW-BASIC there was no way to allocate space for an ordinary variable because INTEGER was pretty much the only scalar type. Could have also declared DOUBLE or SINGLE or even STRING but that wasn't thought of until QuickBASIC and QBasic. On one of the Atari computers, "DIM" was used to allocate string space which added to the confusion. A new keyword should have coined like "GLOBAL". I think some people tried to force the use of COMMON SHARED in QuickBASIC to get away from DIM and leave DIM only to declare arrays. RE: On Exit question - TerryRitchie - 06-02-2023 (06-02-2023, 09:33 PM)Dimster Wrote: But the point I was trying to make, is that there are number of control variables ( ie For x = 1 to 10, or For i = ?? to ??), these control variables I don't think are usually Dimensioned and a program can have a ton of them. Option _Explicit seems to force you to Dimension them. Yes, it does, and that's the point. BASIC, and by extension QB64, allows the creation of variables "out of the blue" and is one of the main reasons professionals in the IT field say programmers that grew up using BASIC have many bad habits and label BASIC as a language to avoid. I don't agree with their thinking since BASIC was designed this way on purpose and creating variables like this is just fine for BASIC. However, I think you'll find that declaring every variable, even those control variables, will make your code much cleaner and easier to read and debug in the long run, not only for others, but especially for you. Besides the benefits I've already pointed out by declaring variables with DIM and STATIC, there is another one. You need to think about the control variables beforehand and name them. I would much rather see this: DIM column AS INTEGER ' text columns on an 80x25 screen DIM row AS INTEGER ' text rows on an 80x25 screen FOR column = 1 TO 80 FOR row = 1 TO 25 .... <code> NEXT row NEXT column than this: FOR i = 1 TO 80 FOR j = 1 TO 25 .... <code> NEXT j NEXT i The first example control variables are not only documented but self-document as well. If you decide to start dabbling with another language it more more than likely require declared variables. BASIC does form a bad habit in this sense. |