Sunday, July 19, 2009

Adding QB SUBs and FUNCTIONs in PN

One nice thing about using PN for a programming environment is how easy it is to create and use simple tools as you need them. In this article I will walk you through the creation of a programming tool that will allow you to insert a SUB or FUNCTION while working with .BAS files in PN.

Open a new document in PN and type (or copy and paste) the following text into the file:

@ECHO OFF
:About
:: O--------------------------------------------------------------O
:: |subShop.bat helps you create QB SUBs and FUNCTIONs in Crimson |
:: | Editor. Pass in your source file as %1. |
:: | |
:: | This file was created by Jason Bales |
:: | gamerplusplus@gmail.com |
:: | http://gamerpp.blogspot.com/ |
:: O--------------------------------------------------------------O

CLS
SETLOCAL
PROMPT
COLOR 47
TITLE WELCOME TO THE SUB SHOP!!

:: Are we to make a Sub or a Function?
SET /p subOrFunction="Are we to make a Sub or a Function?: "
CLS

:: What is the name of the Sub or Function?
SET /p name="What will we call it?: "
CLS

:: What are we passing in?
SET /p parameters="What are we passing in?: "

:: Simple choice
IF %subOrFunction% == SUB GOTO SUB
IF %subOrFunction% == sub GOTO SUB
IF %subOrFunction% == Sub GOTO SUB
IF %subOrFunction% == SUb GOTO SUB
IF %subOrFunction% == suB GOTO SUB
IF %subOrFunction% == sUB GOTO SUB
IF %subOrFunction% == sUb GOTO SUB

REM Notice you only need to type SUB if you want a SUB.
REM Any other input creates a FUNCTION.

:: Write a FUNCTION
> SCRATCH.TXT ECHO DECLARE FUNCTION %name% (%parameters%)
>>SCRATCH.TXT TYPE %1
>>SCRATCH.TXT ECHO.
>>SCRATCH.TXT ECHO ' ____________________
>>SCRATCH.TXT ECHO FUNCTION %name% (%parameters%)
>>SCRATCH.TXT ECHO ' Write your code here
>>SCRATCH.TXT ECHO %name% = ' Make sure the function returns something
>>SCRATCH.TXT ECHO END FUNCTION

> %1 TYPE SCRATCH.TXT
GOTO FIN

:: Write a SUB
:SUB
> SCRATCH.TXT ECHO DECLARE SUB %name% (%parameters%)
>>SCRATCH.TXT TYPE %1
>>SCRATCH.TXT ECHO.
>>SCRATCH.TXT ECHO ' ____________________
>>SCRATCH.TXT ECHO SUB %name% (%parameters%)
>>SCRATCH.TXT ECHO ' Write your code here
>>SCRATCH.TXT ECHO END SUB

> %1 TYPE SCRATCH.TXT

:FIN
DEL SCRATCH.TXT
ENDLOCAL




Save this as subShop.bat in your QB install folder. This file inserts a SUB or FUNCTION into whatever BASIC code you are working on.

To use it we will have to associate it with a tool in PN.

Press Ctrl+N to open a new document. Save this document anywhere on your computer as BLT.BAS.

Click Tools>Options and then select Tools and click the Add button. Make your new tool look like this:
On the Console I/O tab, make sure the first two checkboxes are NOT checked. Click OK and then OK again.

In your empty document write the line:
PRINT "I love ";

And then select Tools>Add SUB or FUNCTION.

Into the command prompt that appears type SUB
and press Enter.

For the next prompt enter BLT
and press Enter.

At the next prompt type BACON$, LETTUCE$, TOMATO$
and press Enter.

Inside the SUB, in place of the comment line, type this code:
PRINT BACON$; LETTUCE$; TOMATO$;


Your code should now look like this:
After your first PRINT command type the following lines:
CALL BLT("BACON, ", "LETTUCE, ", " and TOMATO ")
PRINT "sandwiches!"


Your complete code should now look like this:

DECLARE SUB BLT (BACON$, LETTUCE$, TOMATO$)

PRINT "I love ";

CALL BLT("BACON, ", "LETTUCE, ", " and TOMATO ")

PRINT "sandwiches!"

' ____________________
SUB BLT (BACON$, LETTUCE$, TOMATO$)
PRINT BACON$; LETTUCE$; TOMATO$;
END SUB


Now execute/interpret your code (F5 if you have been following the tutorials). You should see a prompt like this:
When you press a key to continue, you should see your code in the QB IDE.

Hopefully this tutorial has shown how easy it can be to create custom tools for PN.

No comments: