Collabora Office 24.04 Bantuan
Berikut ini menjelaskan penggunaan dasar prosedur, fungsi dan properti di Collabora Office Basic.
Saat Anda membuat modul baru, Collabora Office Basic secara otomatis menyisipkan Sub yang disebut "Main". Nama bawaan ini tidak ada ubungannya dengan urutan atau titik awal proyek Collabora Office Basic. Anda juga bisa mengganti nama Subrutin ini dengan aman.
Beberapa batasan berlaku untuk nama variabel publik, subrutin, fungsi, dan properti Anda. Anda tidak boleh menggunakan nama yang sama sebagai salah satu modul dari pustaka yang sama.
Prosedur (Subrutin) fungsi (Function) dan properti (Property) membantu Anda mempertahankan ikhtisar terstruktur dengan memisahkan program menjadi bagian-bagian logis.
Salah satu manfaat dari prosedur, fungsi, dan properti adalah, setelah Anda mengembangkan kode program yang berisi komponen tugas, Anda dapat menggunakan kode ini di proyek lain.
Variabel bisa diteruskan ke kedua prosedur, fungsi atau properti. Sub Function atau Property harus dideklarasikan untuk mengharapkan parameter:
Sub SubName(Parameter1 As TYPENAME, Parameter2 As TYPENAME,...)
' tempatkan kode Anda di sini
End Sub
Sub dipanggil menggunakan sintaks berikut:
[Call] SubName( [Parameter1:=]Value1, [Parameter2:=]Value2, ...)
Parameter yang diteruskan ke Sub harus sesuai dengan yang ditentukan dalam deklarasi Sub.
The same process applies to a Function. In addition, functions always return a function result. The result of a function is defined by assigning the return value to the function name:
Function FunctionName(Parameter1 As TYPENAME, Parameter2 As TYPENAME,...) As TYPENAME
' your code goes here
FunctionName=Result
End Function
The Function is called using the following syntax:
Variable = FunctionName( [Parameter1:=]Value1, [Parameter2:=]Value2, ...)
Properties combine the syntax of procedures and functions. A Property usually requires up to one parameter.
Private _IsApproved As TYPENAME
Property Get IsApproved As TYPENAME
' your code goes here
IsApproved = some_computation
End Property
Property Let IsApproved(value As TYPENAME)
' your code goes here
_IsApproved = computed_value
End Property
The Property is called using the following syntax:
var = IsApproved
IsApproved = some_value
You can also use the fully qualified name to call a procedure, function or property:
[Call] Library.Module.Macro(), where Call is optional.
For example, to call the Autotext macro from the Gimmicks library, use the following command:
Gimmicks.AutoText.Main()
Parameters can be passed to a procedure, a function or a property either by reference or by value. Unless otherwise specified, a parameter is always passed by reference. That means that a Sub, a Function or a Property gets the parameter and can read and modify its value.
If you want to pass a parameter by value insert the key word ByVal in front of the parameter when you call a Sub, a Function or a Property, for example:
Function ReadOnlyParms(ByVal p2, ByVal p2)
' your code goes here
End Function
result = ReadOnlyParms(parm1, parm2)
In this case, the original content of the parameter will not be modified by the Function since it only gets the value and not the parameter itself.
Functions, procedures or properties can be defined with optional parameters, for example:
Sub Rounding(number, Optional decimals, Optional format)
' your code goes here
End Sub
When you call a function or a subroutine, you may pass its arguments by position or by name. Passing by position means just listing the arguments in the order in which the parameters are defined in the function or subroutine. Passing by name requires you to prefix the argument with the name of the corresponding parameter followed by a colon and an equal sign (:=). Keyword arguments may appear in any order. Refer to Basic Replace() function for such examples.
When needing to pass less parameters, use keywords arguments. Passing values for fewer parameters by position requires to supply values for all parameters before them, optional or not. This ensures that the values are in the correct positions. If you pass the parameters by name - using keyword arguments - you may omit all other intermediate arguments.
A variable defined within a Sub, a Function or a Property, only remains valid until the procedure is exited. This is known as a "local" variable. In many cases, you need a variable to be valid in all procedures, in every module of all libraries, or after a Sub, a Function or a Property is exited.
Global VarName As TYPENAME
The variable is valid as long as the Collabora Office session lasts.
Public VarName As TYPENAME
Variabelnya sah untuk semua modul.
Private VarName As TYPENAME
Variabelnya hanya sah untuk modul ini.
Dim VarName As TYPENAME
Variabelnya hanya sah untuk modul ini.
Enforce private variables to be private across modules by setting CompatibilityMode(True).
' ***** Module1 *****
Private myText As String
Sub initMyText
myText = "Hello"
print "in module1 : ", myText
End Sub
' ***** Module2 *****
'Option Explicit
Sub demoBug
CompatibilityMode( True )
initMyText
' Sekarang menghasilkan string kosong
' (atau menimbulkan kesalahan untuk Opsi Eksplisit)
print "Now in module2 : ", myText
End Sub
Static VarName As TYPENAME
The variable retains its value until the next time the a Function, Sub or Property is entered. The declaration must exist inside a Sub, a Function or a Property.
As with variables, include a type-declaration character after the function name, or the type indicated by As and the corresponding data type at the end of the parameter list to define the type of the function or property's return value, for example:
Function WordCount(WordText As String) As Integer