The Elements of an NSB/App Studio Program

From NSB App Studio
Jump to: navigation, search

A program is a set of Statements. Each program line may consist of the following elements:

KEYWORD arguments 'comment 

A KEYWORD is a word from the language that AppStudio understands. Examples are PRINT, INPUTBOX and IF. The Statement and its arguments determine what action (if any) will be taken by AppStudio when the line is executed.
Any text following ' on a line is a comment, and is ignored by AppStudio.


Multi-Line Statements

Each statement normally ends at the end of the statement line. If you have a very complex statement the line can be very long. This can make your programs difficult to read. You may split long statements by using the line-continuation sequence, a space followed by an underscore, ( _) at the end of the line. AppStudio combines the current line with the next line if the current line ends in ( _). Here is an example of line continuation:

PRINT "Long statements are no problem for" _
   & " AppStudio"

Multiple Statements per Line

By using a ":" character as a separator, you can put more than one statement on a line:

a=1: b=2: c=3

Literals, Data Types and Variables

Literals are literal values you use in your programs. You use them all the time: to set the initial value of a variable, to establish the starting and ending values of a FOR...NEXT loop, and so on. When you define a constant using the CONST Statement, the name of your constant can be treated as a literal. You cannot change the value of a literal.

Variables are the named holders of your data. A variable's value may be changed as needed. All variables are of variants, which means they assume the type of the value that is assigned to them. The type of a variable can change without restriction.

Variable Names

A variable is a name that holds a value. The name consists of a sequence of alphabetic and numeric characters, and the underscore (_) characters. There is no limit to the length of a variable name in AppStudio, and every character in the name is significant. (Some older BASICs could only use short names.) Variable names are not case sensitive, and spaces and other special characters may not be used. Variable names must start with a letter. NS Basic/App Studio constants may not be used as variable names. For a complete list of constants, see Appendix B.

The following list shows some variable names that are allowed by AppStudio:

text
LLAMAS
Jupiter
W1Spec
SouthPark

And some that are not allowed:

1table          'starts with a number 
X&Ycords        'uses special character & 
first counter   'has a space 
%correct        'does not start with a letter
print           'Keyword

Numeric Data Types

All numbers in AppStudio are internally stored as 64bit (8 bytes) floating point numbers, yielding an effective range of 5e-324 (negative) to 1.7976931348623157e+308 (positive).

Integers are considered reliable (numbers without a period or exponent notation) to 15 digits (9e15). Floating point numbers are considered only as reliable as possible and no more. This is an especially important concept to understand for currency manipulation as 0.06 + 0.01 resolves to 0.06999999999999999 instead of 0.07. To round it properly, use Round(0.06 + 0.01, 2).

Hex values are expressed with a leading 0x. Octal values just have a leading 0. So, 0x500 = 1280, while 0500 = 320.

Boolean Data Type

Booleans consists of two values: TRUE and FALSE. This data type is used with the IF Statement. It tests the Boolean value of an expression and selects the THEN Statement if it is TRUE, or the ELSE Statement if it is FALSE. The numeric value for TRUE is -1, and the numeric value for FALSE is 0.


Color Data Type

Colors are hex strings from “#000000” to “#FFFFFF”. A color value is created by mixing values for red, green, and blue that vary from 0 to 255, where 0 is dark and 255 if bright. The mixing formula is:

color = (red*65536) + (green * 256) + blue

Shades of black and white are created when equal amounts of red, green, and blue are used. See Appendix A for a list of common color constants.

Color Name Red, Green, Blue Color Value
Black 0, 0, 0 #000000, vbBLACK
Dark gray 128, 128, 128 #808080
Light gray 192, 192, 192 #COCOCO
White 255, 255, 255 #FFFFFF vbWHITE

String Data Type

Strings consist of a series of characters. A string can be approximately 2 billion characters long. There are a number of functions that manipulate strings. The concatenation operator (&) is used to join the string representations of two variables together. A string literal is enclosed in quotation marks:

"This is a string literal"

Strings are 1 indexed: the first character of a string is character 1 in string functions. Strings are enclosed in double quotes. A single quote(‘) is allowed inside a string. Use two double quotes ("") inside a quoted string to get a single double quote in the string.


Array Data Type

Arrays are lists of values stored with a single name. Each element in the array is referred to by a number or a string in square brackets after the variable name. The first element of an array is always zero (0), and each array can have many elements. ARR[2] refers to the third element in the ARR array. Each element in an array can be of any variable type, including another array. Parenthesis can be used instead of square brackets in most cases.

Arrays must be declared in a Dim statement. Arrays should have a Dim statement in each Code Module where they are used. For multidimensional arrays, A(2,3) refers to the same element as A[2][3].

Collections

A collection is an object used for grouping and managing related objects. It functions like an array, but with strings instead of numbers identifying elements in the array.

Dim item1, item2, item3, item4
item1="Knuth"
item2="Babbage"
item3="Kemeny"
item4="Kurtz"

'Create our collection
myCollection = new Object()
myCollection.add(item1, "firstKey")
myCollection.add(item2, "secondKey")
myCollection.add(item3, "thirdKey")
myCollection.add(item4, "fourthKey")

Print "After adding using myCollection.add(item, key):"
For Each key, item in myCollection
  Print key & "=" & item 
Next 
Print "myCollection[item1]=" & myCollection[item1] 
Print "myCollection[""Knuth""]=" & myCollection["Knuth"]

'Delete a member
myCollection.delete(item2)

Object Data Type

An object is a collection of named values, called properties. To refer to a property of an object, use the object name, a period and the property name:
Customer.Name

An object can have any number of properties. Properties can be any datatype, including functions and other objects.

Customer={Name: Lovelace, Age: 195, kids:{Byron: 1, Annabella: 2, Ralph: 3}}

Classes

Here are a couple of samples of Classes:

BASIC

'Define a class like this
Function Person(name, gender)
   'Add object properties like this
   this.name = name
   this.gender = gender
End Function

'Add methods like this.  All Person objects will be able to invoke this
Function speakName()
  Print "Howdy, my name is " & this.name
End Function

Person.prototype.speak = Eval("speakName")

'Instantiate new objects with 'new'
bob = new Person("Bob", "M")

'Invoke methods like this
bob.speak() 'Prints "Howdy, my name is Bob"

JavaScript

// Define a class like this
function Person(name, gender){

   // Add object properties like this
   this.name = name;
   this.gender = gender;
}

// Add methods like this.  All Person objects will be able to invoke this
Person.prototype.speak = function(){
    alert("Howdy, my name is" + this.name);
}

// Instantiate new objects with 'new'
var person = new Person("Bob", "M");

// Invoke methods like this
person.speak(); // alerts "Howdy, my name is Bob"


Next: Expressions and Operators