변수 사용

다음은 Collabora Office Basic의 변수 사용에 대한 기본적인 사항을 설명한 것입니다.

변수의 명명 규칙

변수 이름은 최대 255자로 구성할 수 있습니다. 변수 이름의 첫 번째 문자는 반드시 A-Z 또는 a-z 사이의 문자여야 합니다. 변수 이름에 숫자는 사용할 수 있지만, 문장 부호나 특수 문자는 사용할 수 없습니다. 단, 밑줄("_")은 사용할 수 있습니다. Collabora Office 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'

변수 선언

Collabora Office 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'
warning

변수를 특정 유형으로 선언한 다음에는 해당 변수를 동일한 이름을 가지고 다른 유형으로 선언할 수 없습니다.


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
note

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로 선언됩니다.

변수 유형

Collabora Office Basic은 다음과 같은 네 가지 변수 클래스를 지원합니다.

Integer 변수

Integer 변수는 -32768에서 32767까지의 범위를 가집니다. Integer 변수에 부동 소수점 값을 할당할 경우 소수점 이하 자릿수가 다음 정수로 반올림됩니다. Integer 변수는 프로시저에서 신속하게 계산되기 때문에 루프의 카운터 변수에 적합합니다. Integer 변수에는 단지 2바이트의 메모리만 필요하며 유형 선언 문자는 "%"입니다.

Dim Variable%
Dim Variable As Integer

Long Integer 변수

Long Integer 변수는 -2147483648에서 2147483647까지의 범위를 가집니다. Long Integer 변수에 부동 소수점 값을 할당할 경우 소수점 이하 자릿수가 다음 정수로 반올림됩니다. Long Integer 변수는 프로시저에서 신속하게 계산되기 때문에 큰 값에 대한 루프의 카운터 변수에 적합합니다. Long Integer 변수에는 4바이트의 메모리가 필요하며 유형 선언 문자는 "&"입니다.

Dim Variable&
Dim Variable As Long

decimal 변수

decimal 변수는 양수, 음수 또는 0 값을 취할 수 있습니다. 소수점 이하 자릿수는 최대 29개로 제한됩니다.

소수 앞에 더하기(+) 또는 빼기(-) 기호를 사용할 수 있습니다(공백 유무 관계 없음).

integer 변수에 소수가 할당되면 Collabora Office Basic이 숫자를 올림 또는 버림합니다.

Single 변수

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 변수

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

Currency 변수

Currency 변수는 내부적으로 64비트 숫자(8바이트)로 저장되며 소수점 앞 자릿수가 15자리이고 소수점 이하 자릿수가 4자리인 고정 소수점 숫자로 표시됩니다. 값 범위는 -922337203685477.5808에서 +922337203685477.5807까지입니다. Currency 변수는 정확도가 높은 통화 값 계산에 사용되며 유형 선언 문자는 "@"입니다.

Dim Variable@
Dim Variable As Currency

Literals for integers

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 변수

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 "$".

tip

In BASIC String functions, the first character of the string has index 1.


Dim Variable$
Dim Variable As String

Boolean 변수

Boolean 변수는 TRUE 값 또는 FALSE 값 중 하나만 저장합니다. 숫자 0은 FALSE, 다른 모든 값은 TRUE로 평가됩니다.

Dim Variable As Boolean

Date 변수

Date 변수는 내부 서식으로 저장된 날짜 및 시간 값만 포함할 수 있습니다. Dateserial, Datevalue, Timeserial 또는 Timevalue를 사용하여 Date 변수에 할당된 값은 자동으로 내부 서식으로 변환됩니다. Date 변수는 Day, Month, Year 또는 Hour, Minute, Second 함수를 사용하여 일반 숫자로 변환됩니다. 내부 서식은 두 숫자의 차를 계산하여 날짜/시간 값을 비교할 수 있게 합니다. Date 변수는 키워드 Date로만 선언할 수 있습니다.

Dim Variable As Date

Literals for Dates

Date literals allow to specify unambiguous date variables that are independent from the current language. Literals are enclosed between hash signs #. Possible formats are:

start_date = #12/30/1899# ' = 1
dob = #2010-09-28#

The Variant type

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
note

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.

warning

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" 값으로 자동 설정됩니다. 다음 규칙에 주의합니다.

Numeric 변수는 선언되자마자 값 "0"이 자동으로 할당됩니다.

Date 변수에는 값 0이 내부적으로 할당됩니다. 이것은 Day, Month, Year 또는 Hour, Minute, Second 함수를 사용하여 값을 "0"으로 변환하는 것과 같습니다.

String 변수는 선언될 때 빈 문자열("")이 할당됩니다.

배열

Collabora Office 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'

색인 범위는 양수와 음수를 모두 포함할 수 있습니다.

상수

상수는 고정 값을 가집니다. 상수는 다음과 같이 프로그램에서 한 번만 지정되며 나중에 다시 지정할 수 없습니다.

Const ConstName=Expression

Please support us!