Gambas/Referencia Rápida

De Wikilibros, la colección de libros de texto de contenido libre.

Nota: Falta traducir algunas partes al español

Data Types[editar]

  • Boolean True or false
  • Byte 0 to 255
  • Short -32.768 to +32.767
  • Integer -2.147.483.648 to +2.147.483.647
  • Long -9.223.372.036.854.775.808 to +9.223.372.036.854.775.807
  • Single Like the float datatype in C.
  • Float Like the double datatype in C.
  • Date Date and time, each stored in an integer.
  • String A variable length string of characters.
  • Variant Any datatype.
  • Object Anonymous reference to any object.
  • Pointer A memory address.

Comments[editar]

 'All comments start with an apostrophe


Variable Declaration[editar]

[STATIC] {PUBLIC|PRIVATE} Identifier [Static array declaration] AS Datatype [=Expression]

STATIC PUBLIC GridX AS Integer
STATIC PRIVATE bGrid AS Boolean
PUBLIC Name AS String
PRIVATE Control AS Object
PRIVATE Languages AS String[] = [ "fr", "it", "es", "de", "ja" ]
PRIVATE DefaultLanguage AS String = Languages[1]
'All DIM declarations must be in the FUNCTION or SUB before the first executable command.
DIM fTimeDiff2 AS Float
DIM iTest AS Integer
DIM iK[9] AS Integer 'ik[0] to ik[8]
DIM shX[3,3] AS Short
DIM date1 AS Date

[STATIC] {PUBLIC|PRIVATE} Identifier AS NEW Native Datatype [Array dimensions...]

PUBLIC CONST WORD_MAX AS Integer = 12
PRIVATE Words AS NEW String[WORD_MAX * 2]
PUBLIC Matrix AS NEW Float[3, 3] 

Class Declaration[editar]

[STATIC] {PUBLIC|PRIVATE} Identifier AS NEW Class (Arguments...)

STATIC PRIVATE Tasks AS NEW List 
PRIVATE MyCollection AS NEW Collection(gb.Text) 


Constant Declaration[editar]

{PUBLIC|PRIVATE} CONST Identifier AS Datatype = Constant value

'Constant datatype can be: Boolean, Integer, Long, Float or String.
PUBLIC CONST MAX_FILE AS Integer = 30
PRIVATE CONST DEBUG AS Boolean = TRUE
PRIVATE CONST MAGIC_HEADER AS String = "# Gambas form file" 


Assignment[editar]

Destination = Expression
iVal = 1972
Name = "Gambas"
hObject.Property = iVal
cCollection[sKey] = Name 


Loop Control Structures[editar]

FOR Variable = Expression TO Expression [STEP Expression] ... NEXT 'Counter loop.

FOR iCount = 1 TO 20 STEP 3
  PRINT iCount & " ";
NEXT

FOR EACH Variable IN Expression ... NEXT

'Enumeration loop. 
FOR EACH Element IN Dict
  PRINT Element;
NEXT

DO [WHILE Condition] ... [BREAK|CONTINUE] ... LOOP [UNTIL Condition]

'Infinite loop. 
DO WHILE a <= 5
  PRINT "Hello World"; a
  INC a
LOOP
' The same effect with UNTIL
DO
  PRINT "Hello World"; a
  INC a
LOOP UNTIL a = 6

REPEAT ... UNTIL Condition

'Loop with an exit test at the end. 
REPEAT
  PRINT Timer 'The repeat loop is always executed at least once, even if the UNTIL value is initially false. 
UNTIL Timer > 10

WHILE Condition ... WEND

'Loop with an exit test at the beginning
WHILE a \<= 10
  PRINT "Hello World"; a
  INC a
WEND 


Test Control Structures & Functions[editar]

Value = Choose (Choice, Result1, Result2 [, ... ])

X = 3
PRINT Choose(X, "one", "two", "three", "four")	'three
PRINT IsNull(Choose(X * 2, "one", "two", "three", "four"))	'True

Value = IIf (bTest AS Boolean , vTrue , vFalse) Value = If (bTest AS Boolean , vTrue , vFalse)

PRINT If((X MOD 2) = 0, "even", "odd") 
PRINT "You have " & X & " message" & If(X <> 1, "s", "") & " waiting." 

IF Expression [{AND IF|OR IF} Expression ...] [THEN] ... [ELSE IF Expression [{AND IF|OR IF} Expression ...] [THEN] ...] [ELSE ...] ENDIF

FOR k = 1 TO 10
  IF k < 5 OR IF k > 5 THEN
    PRINT k;;
  ELSE
    PRINT "5 has been reached!"
  END IF
NEXT 

SELECT [CASE] Expression [CASE Expression [TO Expression2] [, ... ] ...] [CASE Expression [TO Expression2] [, ... ] ...] [{CASE ELSE|DEFAULT} ...] END SELECT

SELECT CASE w
  CASE 1
    a = a + 1
  CASE 2
    b = b + 1
  CASE 3
    c = c + 1
  CASE 4
    d = d + 1
  CASE 5
    e = e + 1
  CASE 6
    f = f + 1
  CASE ELSE
    PRINT "This is impossible!"
END SELECT 


Operators[editar]

Arithmetic Operators

A = Number + Number 'Adds two numbers
A =- Number 'Computes the opposite sign of a number. Zero is the opposite of itself
A = Number – Number 'Subtracts two numbers
A = Number * Number 'Multiplies two numbers
A = Number / Number 'Divides two numbers. A Division By Zero (#26) error will occur if the value of the number to the right of the slash is zero
A = Number ^ Power 'Raises Number to the power Power.
A = Number \ Number
A = Number DIV Number 'Computes the quotient of the two Integer numbers, truncating the result. A Division By Zero (#26) error will occur if the value of the number to the right of the backslash is zero
A = Number MOD Number 'Computes the remainder of the quotient of the two numbers. A Division By Zero (#26) error will occur if the value of the number to the right of the operator MOD is zero

Comparison Operators

Number = Number 'Returns TRUE if two numbers are equal
Number <> Number 'Returns TRUE if two numbers are different
Number1 < Number2 'Returns TRUE if Number1 is strictly lower than Number2
Number1 > Number2 'Returns TRUE if Number1 is strictly greater than Number2
Number1 <= Number2 'Returns TRUE if Number1 is lower or equal than Number2
Number1 >= Number2 'Returns TRUE if Number1 is greater or equal than Number2
'If the result of a Comparison is assigned to an integer variable, then the result is either -1 (True) or 0 (False) 

Assignment Operators

Variable = Expression 'Direct assignment
Variable += Expression 'Assignment with addition. This is a synonymous for Variable = Variable + Expression
Variable -= Expression 'Assignment with substraction. This is a synonymous for Variable = Variable - Expression
Variable *= Expression 'Assignment with multiplication. This is a synonymous for Variable = Variable * Expression
Variable /= Expression 'Assignment with division. This is a synonymous for Variable = Variable / Expression
Variable \= Expression 'Assignment with integer division.This is a synonymous for Variable = Variable \ Expression
Variable &= Expression 'Assignment with string concatenation. This is a synonymous for Variable = Variable & Expression
Variable &/= Expression 'Assignment with path concatenation. This is a synonymous for Variable = Variable &/ Expression


Method Declaration[editar]

Procedures

[STATIC] {PUBLIC|PRIVATE} {PROCEDURE|SUB} Identifier ( [[BYREF] Parameter AS Datatype [, ...]] [,] [OPTIONAL [BYREF] Optional Parameter AS Datatype [, ... ]] [,] [...] ) ... END

PUBLIC SUB Button1_Click()
  PRINT Calc(0);; Calc(0.5);; Calc(1)
END

Functions

[STATIC] {PUBLIC|PRIVATE} {FUNCTION|PROCEDURE|SUB} Identifier ( [[BYREF] Parameter AS Datatype [, ...]] [,] [OPTIONAL [BYREF] Optional Parameter AS Datatype [, ...]] [,] [...] ) AS Datatype ... RETURN Value 'In Gambas the RETURN value is passed as a parameter to the RETURN statement. In VB the RETURN value is assigned to the FUNCTION name: Solve1 = TRUE END

FUNCTION Calc(fX AS Float) AS Float
  RETURN Sin(fX) * Exp(- fX)
END


Error Management[editar]

  • DEBUG Prints a debugging message.
  • FINALLY Executes a block of code in a function, even if there was an error.
  • CATCH Catches an error in a function.
  • Error The Error static class.
  • TRY Tries to execute a statement, without raising an error.
  • ERROR Returns if an error happened, or prints an error message. Returns TRUE if an error has happened. Use it just after a TRY instruction to know if the executed instructions failed.
'Prints a file to the screen
SUB PrintFile(FileName AS STRING)
  DIM hFile AS File
  DIM sLig AS STRING
  OPEN FileName FOR READ AS #hFile
  WHILE NOT EOF(hFile)
    LINE INPUT #hFile, sLig
    PRINT sLig
  WEND
FINALLY 'Always executed, even if a error is raised
  CLOSE #hFile
CATCH 'Executed only if there is an error
  PRINT "Cannot print file "; FileName
END


Functions[editar]

String Functions

  • Asc Returns the ASCII code of a character in a string.
  • Chr$ Returns a character from its ASCII code.
  • Comp Compares two strings.
  • InStr Searches a string into another string.
  • LCase$ Converts a string to lowercase.
  • Left$ Returns the first characters of a string.
  • Len Returns the length of a string.
  • LTrim$ Strips white spaces from the left of a string.
  • Mid$ Returns a part of a string.
  • Replace$ Replaces in a string a substring by another one.
  • Right$ Returns the last characters of a string.
  • RInStr Searches a string into another string from its right.
  • RTrim$ Strips white spaces from the right of a string.
  • Scan Splits a string against a regular expression pattern.
  • Space$ Returns a string containing only space.
  • Split Splits a string into substrings.
  • String$ Returns the same string concatenated many times.
  • Subst$ Substitutes strings in a pattern.
  • Trim$ Strips white spaces from a string.
  • UCase$ Converts a string to uppercase.

Stream & Input/Output functions

  • CLOSE Closes a stream
  • Eof Returns if the end of file is reached.
  • ERROR Prints expressions to the error standard output.
  • ERROR TO Redirects the standard error output.
  • FLUSH Flushes the output of a buffered stream.
  • INPUT Reads strings from a text stream and converts them into values.
  • INPUT FROM Redirects the standard input.
  • LINE INPUT Reads lines from a text stream.
  • LOCK Locks an opened stream.
  • Lof Returns the length of a stream.
  • OPEN Opens a file for reading or writing and creates a stream for it.
  • OUTPUT TO Redirects the standard output.
  • PIPE Opens a named pipe for reading or writing and creates a stream for it.
  • PRINT Prints expressions to a stream.
  • READ Reads binary data from a stream.
  • SEEK Change the position of the stream file pointer.
  • Seek Gets the position of the stream file pointer.
  • UNLOCK Unlocks an opened stream.
  • WRITE Write binary data to a stream.

File & Directory Functions

  • Access Tests the access authorization of a file.
  • COPY Copy a file.
  • DFree Returns the free space on a device.
  • Dir Browses a directory.
  • Exist Checks if a specific file or directory exists.
  • IsDir Returns if a path points at a directory.
  • KILL Removes a file.
  • LINK Creates a symbolic link.
  • MKDIR Creates a directory.
  • MOVE Renames or moves a file or a directory.
  • RDir Browses a directory recursively.
  • RMDIR Removes an empty directory.
  • Stat Get information about a file.
  • Temp$ Makes temporary file names.

Arithmetical Functions

  • Abs Returns the absolute value of a number.
  • DEC Decrements a variable.
  • Fix Returns the integer part of a number.
  • Frac Returns the fractional part of a number.
  • INC Increments a variable.
  • Int Returns the mathematical integer part of a number.
  • Max Returns the maximum number.
  • Min Returns the minimum number.
  • Round Rounds a number.
  • Sgn Returns the sign of a number.

Logarithms & Exponentials Functions

  • Cbr Cubic root
  • Exp Exponential, e^x
  • Exp2 2^x
  • Exp10 10^x
  • Expm Exp(x) - 1
  • Log Neperian logarithm, base e logarithm
  • Log2 Base 2 logarithm
  • Log10 Decimal logarithm
  • Logp Log(1+x)
  • Sqr Square root

Trigonometric Functions

  • ACos Computes the arc cosine of an angle.
  • ACosh Computes the hyperbolic arc cosine of an angle.
  • Ang Computes the angle polar coordinate from two rectangular coordinates.
  • ASin Computes the arc sine of an angle.
  • ASinh Computes the hybperbolic arc sine of an angle.
  • ATan Computes the arc tangent of an angle.
  • ATan2 Computes the arc tangent of two numbers.
  • ATanh Computes the hyperbolic arc tangent of an angle.
  • Cos Computes the cosine of an angle.
  • Cosh Computes the hyperbolic cosine of an angle.
  • Deg Converts radians to degrees.
  • Hyp Calculate the hypotenuse of a triangle.
  • Mag Computes the distance polar coordinate from two rectangular coordinates.
  • Pi Returns π or a multiple of π.
  • Sin Computes the sine of an angle.
  • Sinh Computes the hyperbolic sine of an angle.
  • Tan Computes the tangent of an angle.
  • Tanh Computes the hyperbolic tangent of an angle.
  • Rad Converts degrees to radians.

Random Numbers Functions

  • RANDOMIZE Initializes the pseudo-random number generator.
  • Rnd Returns a pseudo-random number.

Date & Time Functions

  • Date Returns a date without its time component.
  • DateAdd Adds a period to a given date.
  • DateDiff Returns the period between two dates.
  • Day Returns the day of a Date value.
  • Hour Returns the hours of a Date value.
  • Minute Returns the minutes of a Date value.
  • Month Returns the month of a Date value.
  • Now Returns the current date and time.
  • Second Returns the seconds of a Date value.
  • Time Returns the time part of a Date value.
  • Timer Returns the number of elapsed seconds since the program started.
  • Week Returns the week number of a Date value.
  • WeekDay Returns the week day of a Date value.
  • Year Returns the year of a Date value.

Datatype Functions

  • IsBoolean Returns if an expression is a Boolean value.
  • IsByte Returns if an expression is a Byte value.
  • IsDate Returns if an expression is a Date value.
  • IsFloat Returns if an expression is a Float value.
  • IsInteger Returns if an expression is an Integer value.
  • IsLong Returns if an expression is a Long value.
  • IsNull Returns if an expression is NULL.
  • IsNumber Returns if an expression is a number.
  • IsObject Returns if an expression is an Object value.
  • IsShort Returns if an expression is a Short value.
  • IsSingle Returns if an expression is a Single value.
  • IsString Returns if an expression is a String value.
  • TypeOf Returns the type of the value of an expression.

Character Test Functions

  • IsAscii Tests if a string contains only ASCII characters.
  • IsBlank Tests if a string contains only blank characters.
  • IsDigit Tests if a string contains only digits.
  • IsHexa Tests if a string contains only hexadecimal digits.
  • IsLCase Tests if a string contains only lowercase letters.
  • IsLetter Tests if a string contains only letters.
  • IsPunct Tests if a string contains only printable non-alphanumeric characters.
  • IsSpace Tests if a string contains only space characters.
  • IsUCase Tests if a string contains only uppercase letters.

Localization and Translation Functions

  • Format$ Format a number or a date.
  • Str$ Converts a number or a date into a string.
  • Subst$ Substitutes strings in a pattern.
  • Tr$ Translate a string.
  • Val Converts a string into a number or a date.

Formatting functions

  • Bin$ Format a number in binary.
  • Format$ Format a number or a date.
  • Hex$ Format a number in hexadecimal.

Para mas información visita http://gambas.sourceforge.net/