LibreOffice 7.5 Help
ដូចខាងក្រោមពណ៌នាអំពីមូលដ្ឋាននៃការប្រើអថេរក្នុង LibreOffice Basic ។
ឈ្មោះអថេរអាចមានតួអក្សរច្រើនបំផុត ២៥៥ តួ ។ តួអក្សរទីមួយនៃឈ្មោះអថេរ ត្រូវតែ ជាអក្សរពី A-Z ឬ a-z ។ លេខក៏អាចត្រូវបានប្រើក្នុងឈ្មោះអថេរ ប៉ុន្តែសញ្ញាវណ្ណយុត្ត និងតួអក្សរពិសេសមិនត្រូវបានអនុញ្ញាតទេ លើកលែងតែតួអក្សរ ("_") ។ ក្នុង LibreOffice Basic ឈ្មោះអថេរមិនប្រកាន់អក្សរតូចធំទេ ។ ឈ្មោះអថេរអាចមានដកឃ្លា ប៉ុន្តែត្រូវតែដាក់សញ្ញា "[ ]" នៅជុំវិញ ប្រសិនបើពួកវាមាន ។
ឧទាហរណ៍អំពីឈ្មោះអថេរ ៖
MyNumber=5 'Correct'
MyNumber5=15 'Correct'
MyNumber_5=20 'Correct'
My Number=20 'Not valid, variable with space must be enclosed in square brackets'
[My Number]=12 'Correct'
DéjàVu=25 'Not valid, special characters are not allowed'
5MyNumber=12 'Not valid, variable may not begin with a number'
Number,Mine=12 'Not valid, punctuation marks are not allowed'
ក្នុង LibreOffice Basic អ្នកមិនចាំបាច់ប្រកាសអថេរជាក់លាក់ ។ ការប្រកាសអថេរមួយអាចត្រូវបានធ្វើដោយប្រើ Dim ។ អ្នកអាចប្រកាសអថេរច្រើនជាងមួយនៅពេលតែមួយ ដោយបំបែកដោយសញ្ញា (,) ។ ដើម្បីកំណត់ប្រភេទអថេរ ប្រើសញ្ញាប្រភេទប្រកាសបន្ទាប់ពីឈ្មោះ ឬពាក្យគន្លឹះដែលសមរម្យក៏បាន ។
ឧទាហរណ៍សម្រាប់ការប្រកាសអថេរ ៖
Dim a$ 'Declares the variable "a" as a String'
Dim a As String 'Declares the variable "a" as a String'
Dim a$, b As Integer 'Declares one variable as a String and one as an Integer'
Dim c As Boolean 'Declares c as a Boolean variable that can be TRUE or FALSE'
នៅពេលអ្នកបានប្រកាសអថេរមួយជាប្រភេទពិតប្រាកដមួយ អ្នកមិនអាចប្រកាសអថេរដោយប្រើឈ្មោះដូចគ្នាម្តងទៀតជាប្រភេទផ្សេងបានទេ !
When you declare multiple variables in a single line of code you need to specify the type of each variable. If the type of a variable is not explicitly specified, then Basic will assume that the variable is of the Variant type.
' Both variables "a" and "b" are of the Integer type
Dim a As Integer, b As Integer
' Variable "c" is a Variant and "d" is an Integer
Dim c, d As Integer
' A variable can also be explicitly declared as a Variant
Dim e As Variant, f As Double
The Variant type is a special data type that can store any kind of value. To learn more, refer to the section The Variant type below.
ដើម្បីបង្ខំការប្រកាសអថេរ ប្រើពាក្យបញ្ជាដូចខាងក្រោម ៖
Option Explicit
សេចក្តីថ្លែងការណ៍Option Explicit ត្រូវតែជាបន្ទាត់ទីមួយក្នុងម៉ូឌុល នៅមុន SUB ទីមួយ ។ ជាទូទៅ មានតែអារេប៉ុណ្ណោះដែលត្រូវការប្រកាសជាក់លាក់ ។ អថេរផ្សេងទៀតទាំងអស់ ត្រូវបានប្រកាសដោយយោងទៅតួអក្សរប្រភេទប្រកាស ឬប្រសិនបើត្រូវបានលុបចោល វាជាប្រភេទលំនាំដើម Single ។
LibreOffice Basic គាំទ្រអថេរបួនប្រភេទ ៖
អថេរ ជាលេខ អាចផ្ទុកតម្លៃលេខ ។ អថេរខ្លះត្រូវបានប្រើដើម្បីរក្សាទុកចំនួនធំ ឬតូច និងផ្សេងទៀតត្រូវបានប្រើ សម្រាប់ចំនួនទសភាគ ឬប្រភាគ ។
String អថេរដែលមានខ្សែតួអក្សរ ។
អថេរ ប៊ូលីន មានតម្លៃពិត ឬមិនពិត ។
វត្ថុ អថេរអាចរក្សាទុកវត្ថុប្រភេទខុសៗគ្នា ដូចជាតារាង និងឯកសារក្នុងឯកសារមួយ ។
អថេរចំនួនគត់ស្ថិតនៅចន្លោះពី -32768 ដល់ 32767 ។ ប្រសិនបើអ្នកផ្តល់តម្លៃទសភាគទៅអថេរចំនួនគត់មួយ ខ្ទង់ទសភាគត្រូវបានបង្គត់ទៅចំនួនគត់បន្ទាប់ ។ អថេរចំនួនគត់ត្រូវបានគណនាយ៉ាងលឿនក្នុងបែបបទ និងសមស្របសម្រាប់អថេររាប់ក្នុងរង្វិលជុំ ។ អថេរចំនួនគត់ត្រូវការអង្គចងចាំតែ ២ បៃប៉ុណ្ណោះ ។ "%" គឺជាតួអក្សរប្រភេទការប្រកាស ។
Dim Variable%
Dim Variable As Integer
អថេរចំនួនគត់ធំស្ថិតនៅចន្លោះពី -2147483648 ដល់ 2147483647 ។ ប្រសិនបើអ្នកផ្តល់តម្លៃទសភាគទៅអថេរចំនួនគត់ធំ ខ្ទង់ទសភាគត្រូវបានបង្គត់ទៅចំនួនគត់បន្ទាប់ ។ អថេរចំនួនគត់ធំត្រូវបានគណនាយ៉ាងលឿនក្នុងបែបបទ និងសមស្របសម្រាប់អថេររាប់ក្នុងរង្វិលជុំ សម្រាប់តម្លៃធំៗ ។ អថេរចំនួនគត់ធំទាមទារអង្គចងចាំ ៤ បៃ ។ "&" គឺជាតួអក្សរប្រភេទការប្រកាស ។
Dim Variable&
Dim Variable As Long
អថេរគោលដប់អាចយកលេខវិជ្ជមាន អវិជ្ជមាន ឬសូន្យ ។ ភាពត្រឹមត្រូវគឺមានរហូតដល់ ២៩ តួលេខ ។
អ្នកអាចប្រើសញ្ញា (+) ឬ (-) ជាបុព្វបទសម្រាប់លេខគោលដប់ (ដោយមាន ឬគ្មានចន្លោះ) ។
ប្រសិនបើលេខគោលដប់ត្រូវបានកំណត់ជាអថេរចំនួនគត់ LibreOffice Basic បង្គត់ចំនួនចុះ ឬឡើង ។
Single variables can take positive or negative values ranging from 3.402823 x 10E38 to 1.401298 x 10E-45. Single variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Single variables are suitable for mathematical calculations of average precision. Calculations require more time than for Integer variables, but are faster than calculations with Double variables. A Single variable requires 4 bytes of memory. The type-declaration character is "!".
Dim Variable!
Dim Variable As Single
Double variables can take positive or negative values ranging from 1.79769313486232 x 10E308 to 4.94065645841247 x 10E-324. Double variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Double variables are suitable for precise calculations. Calculations require more time than for Single variables. A Double variable requires 8 bytes of memory. The type-declaration character is "#".
Dim Variable#
Dim Variable As Double
អថេររូបិយប័ណ្ណត្រូវបានរក្សាទុកខាងក្នុងជាចំនួន ៦៤ ប៊ីត (៨ បៃ) និងបានបង្ហាញជាចំនួនទសភាគថេរមួយដែលមានខ្ទង់មិនមែនទសភាគ ១៥ ខ្ទង់ និងខ្ទង់ទសភាគ ៤ ខ្ទង់ ។ តម្លៃស្ថិតនៅចន្លោះពី -922337203685477.5808 ដល់ +922337203685477.5807 ។ អថេររូបិយប័ណ្ណត្រូវបានប្រើដើម្បីគណនាតម្លៃរូបិយប័ណ្ណដែលមានចំនួនខ្ទង់ទសភាគខ្ពស់ ។ តួអក្សរប្រភេទការប្រកាសគឺ "@" ។
Dim Variable@
Dim Variable As Currency
Numbers can be encoded using octal and hexadecimal forms.
xi = &o13 ' 8 + 3
ci = &h65 ' 6*16 + 5
MAX_Integer = &o77777 ' 32767 = &h7FFF
MIN_Integer = &o100000 ' -32768 = &h8000
MAX_Long = &h7fffffff ' 2147483647 = &o17777777777
MIN_Long = &h80000000 ' -2147483648 = &o20000000000
String variables can hold character strings with up to 2,147,483,648 characters. Each character is stored as the corresponding Unicode value. String variables are suitable for word processing within programs and for temporary storage of any non-printable character up to a maximum length of 2 Gbytes. The memory required for storing string variables depends on the number of characters in the variable. The type-declaration character is "$".
In BASIC String functions, the first character of the string has index 1.
Dim Variable$
Dim Variable As String
អថេរប៊ូលីនទុកតែតម្លៃមួយនៃតម្លៃពីរ ៖ ពិត ឬមិនពិត ។ លេខ ០ វាយតម្លៃជា មិនពិត គ្រប់តម្លៃផ្សេងទៀតវាយតម្លៃជា ពិត ។
Dim Variable As Boolean
អថេរកាលបរិច្ឆេទអាចមានតែតម្លៃកាលបរិច្ឆេទ និងពេលវេលាដែលរក្សាទុកក្នុងទ្រង់ទ្រាយខាងក្នុង ។ តម្លៃដែលបានផ្តល់ទៅអថេរកាលបរិច្ឆេទជាមួយ Dateserial, Datevalue, Timeserial ឬTimevalue ត្រូវបានបម្លែងដោយស្វ័យប្រវត្តិទៅជាទ្រង់ទ្រាយខាងក្នុង ។ អថេរកាលបរិច្ឆេទត្រូវបានបម្លែងជាលេខធម្មតាដោយប្រើអនុគមន៍ Day, Month, Year ឬHour, Minute, Second ។ ទ្រង់ទ្រាយខាងក្នុងអាចធ្វើឲ្យប្រើបានការប្រៀបធៀបនៃតម្លៃកាលបរិច្ឆេទ/ពេលវេលា ដោយគណនាភាពខុសគ្នារវាងចំនួនពីរ ។ អថេរទាំងនេះអាចត្រូវបានប្រកាសតែជាមួយពាក្យគន្លឹះ Date ប៉ុណ្ណោះ ។
Dim Variable As Date
Date literals allow to specify unambiguous date variables that are independent from the current language. Literals are enclosed between hash signs #. Possible formats are:
#yyyy-mm-dd#
#mm/dd/yyyy#
start_date = #12/30/1899# ' = 1
dob = #2010-09-28#
Variables declared as Variant can handle any data type. This means that the actual data type is defined during runtime as a value is assigned to the variable.
There are three main ways to create a Variant variable, as shown below:
Dim varA ' The type is not specified, hence the variable is a Variant
Dim varB as Variant ' The variable is explicitly declared as a Variant
varC = "abc" ' Previously undeclared variables are treated as Variants
The example below uses the TypeName function to show how the type of a Variant variable changes upon assignment.
Dim myVar As Variant
MsgBox TypeName(myVar) ' Empty
myVar = "Hello!"
MsgBox TypeName(myVar) ' String
myVar = 10
MsgBox TypeName(myVar) ' Integer
A Variant variable is initialized with the Empty special data type. You can use the IsEmpty function to test if a variable is an Empty Variant.
You can also use the keyword Any to declare a variable as a Variant. However, Any is deprecated and is available for backward compatibility.
Arguments with type Variant or Any passed in function calls are not checked for their types.
Dim myVar As Any ' Variable "myVar" is a Variant
នៅពេលអថេរត្រូវបានប្រកាស វាត្រូវបានកំណត់ដោយស្វ័យប្រវត្តិទៅជាតម្លៃ "Null" ។ ចំណាំការសន្មតដូចខាងក្រោម ៖
អថេរ ជាលេខ ត្រូវបានផ្តល់តម្លៃ "0" ដោយស្វ័យប្រវត្តិ នៅពេលពួកវាត្រូវបានប្រកាស ។
អថេរកាលបរិច្ឆេទ ត្រូវបានផ្តល់តម្លៃខាងក្នុងគឺ 0 ដែលសមមូលទៅនឹងការបម្លែងតម្លៃជា "0" ជាមួយអនុគមន៍ Day, Month, Year ឬ Hour, Minute, Second ។
អថេរ String ត្រូវបានផ្តល់តម្លៃខ្សែអក្សរទទេ ("") នៅពេលពួកវាត្រូវបានប្រកាស ។
LibreOffice Basic ស្គាល់អារេមួយ ឬច្រើនវិមាត្រ ដែលកំណត់ដោយប្រភេទអថេរបានបញ្ជាក់ ។ អារេគឺសមរម្យ សម្រាប់កែសម្រួលបញ្ជី និងតារាងក្នុងកម្មវិធី ។ ធាតុនីមួយៗនៃអារេ អាចត្រូវបានផ្តល់ទីតាំងតាមរយៈសន្ទស្សន៍ជាលេខ ។
អារេ ត្រូវតែ ប្រកាសដោយប្រើ Dim ។ មានវិធីជាច្រើន ដើម្បីកំណត់ជួរសន្ទស្សន៍នៃអារេមួយ ៖
Dim Text$(20) '21 elements numbered from 0 to 20'
Dim Text$(5,4) '30 elements (a matrix of 6 x 5 elements)'
Dim Text$(5 To 25) '21 elements numbered from 5 to 25'
Dim Text$(-15 To 5) '21 elements (including 0), numbered from -15 to 5'
ជួរសន្ទស្សន៍អាចរួមបញ្ចូលនូវចំនួនវិជ្ជមាន ក៏ដូចជាចំនួនអវិជ្ជមាន ។
Constants មានតម្លៃថេរ ។ ពួកវាត្រូវបានកំណត់តែម្តងប៉ុណ្ណោះក្នុងកម្មវិធី និងមិនអាចកំណត់ម្តងទៀតនៅពេលក្រោយ ៖
Const ConstName=Expression