Fundamentals of BASIC
Jump to navigation
Jump to search
Variables
Naming
The following list shows some variable names that are allowed by AppStudio:
text LLAMAS Jupiter W1Spec SouthPark btnSubmit txtCustomer
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
- Variable names are case sensitive.
- If you DIM a variable name, AppStudio will correct the case on entry.
Variables
- All variables are of type Variant.
- No explicit types
- Variable get their types from the value that is assigned.
- Variables Dimmed outside of an Function or Sub are global.
- Variables Dimmed inside a Function or Sub are local.
- Variables which are not declared are global.
Numeric
- Floating point: 5e-324 (negative) to 1.7976931348623157e+308 (positive)
- Integers: reliable to 15 digits.
- Hex: 0x500 = 1280
- Octal: O500 = 320
Booleans
- True or False.
Colors
- From “#000000” to “#FFFFFF” (black to white)
- color = (red*65536) + (green * 256) + blue
Strings
- Maximum length depends on memory, up to 2 billion characters
Arrays
- Dim A(9) declares an array with 10 elements
- Dim B(8,8) creates a two dimensional array
- Elements can be referred to as A(3), B(2,3), etc.
- Dim statement has to be in each Code Module
- The following notation is also OK: a[3], B[2][3}
- Array elements do not exist until they are assigned.
- To create empty array, do this: a=[]
- Use the Ubound() function to get the number of items in an array.
Collections
- Like an Array, but uses string values to index
Dim item1, myCollection(10) item1="Knuth" 'Create our collection myCollection[item1] = "firstKey" Print myCollection[item1] 'displays "firstkey"
Objects
- An object is a collection of named values, called properties
Customer={Name: Lovelace, Age: 195, kids:{Byron: 1, Annabella: 2, Ralph: 3}} Print Customer.name 'displays "LoveLace"
Classes
'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"
Control structures
If...Then
If a=b Then c=1 If a=b Then c=1 End If If a=b Then C=1 Else C=2 End If
Loops
For i=1 to 10 Print i Next i=0 Do While i<10 Print i i=i+1 Loop i=0 Do Print i i=i+1 Loop Until i=10 Rem While...Wend Example 'While...Wend repeats a group of statements Dim Counter Counter = 1 While Counter < 5 Print "Counter = " & Counter Counter = Counter + 1 Wend
With
Rem With sample 'With statement allows accessing properties on a specified object Dim theURL, theTitle With document theURL = .URL .title = "New Title" theTitle = .title Print .URL Print .title End With
Select Case
Rem Select Case Example 'Select Case performs conditional execution CheckHat("Blue") CheckHat("Orange") Sub CheckHat(Hat) Select Case Hat Case "Blue" Print "Kyle's hat" Case "Green" Print "Stan's hat" Case "Cyan" Print "Eric's hat" Case "Orange", "Hood" Print "Kenny's hat" Case "White" Print "Chef's hat" Case "Striped" Print "Mr. Hat" Case "Christmas", "Santa" Print "Mr. Hankey's hat" Case Else Print "Unknown Hat" End Select End Sub
Error trapping
Rem Try Example 'Try catches run time errors b=1 c=0 Try If c=0 Then throw "Division by zero!" a=b/c Catch err Print err Finally Print "done" End Try
Functions and Subroutines
Sub mySubroutine(a,b) Print a + b End Sub Function myFunction(a,b) myFunction = a + b End Function
Importing VB code
- Code using the above statements can usually be pasted in
- Remember to remove the AS clauses
- Simple controls (Buttons, TextBoxes, etc) have similar properties
- Advanced controls and properties may be different
A few interesting statements
- Print "Some data"
- console.log("a = " & a)