상수 및 리터럴 데이터 형식(Visual Basic)Constant and Literal Data Types (Visual Basic)
리터럴 값인 변수의 값 또는 숫자 3 또는 문자열 "Hello"와 같은 식의 결과가 아니라 자체적으로 표현 됩니다.A literal is a value that is expressed as itself rather than as a variable's value or the result of an expression, such as the number 3 or the string "Hello". 상수는 리터럴 대신 해당 값이 달라질 변수와 달리 프로그램 전체에서이 동일한 값을 유지 하는 의미 있는 이름입니다.A constant is a meaningful name that takes the place of a literal and retains this same value throughout the program, as opposed to a variable, whose value may change.
때 Option Infer 은 Off
하 고 Option Strict 는 On
, 데이터 형식으로 모든 상수를 명시적으로 선언 해야 합니다.When Option Infer is Off
and Option Strict is On
, you must declare all constants explicitly with a data type. 다음 예제에서는 데이터의 유형은 MyByte
데이터 형식으로 명시적으로 선언 된 Byte
:In the following example, the data type of MyByte
is explicitly declared as data type Byte
:
Option Strict On
Public Class Sample
Public Const MyByte As Byte = 2
End Class
때 Option Infer
은 On
또는 Option Strict
은 Off
를 사용 하 여 데이터 형식을 지정 하지 않고 상수를 선언할 수 있습니다는 As
절.When Option Infer
is On
or Option Strict
is Off
, you can declare a constant without specifying a data type with an As
clause. 컴파일러는 상수 식의 형식에서 형식을 결정합니다.The compiler determines the type of the constant from the type of the expression. 기본적으로 캐스팅 된 리터럴 숫자 정수는 Integer
데이터 형식입니다.A numeric integer literal is cast by default to the Integer
data type. 부동 소수점 숫자에 대 한 기본 데이터 형식 Double
, 및 키워드 True
하 고 False
지정을 Boolean
상수입니다.The default data type for floating-point numbers is Double
, and the keywords True
and False
specify a Boolean
constant.
리터럴 및 형식 강제 변환Literals and Type Coercion
일부 경우에는 특정 데이터 형식에 리터럴을 강제로 하려는 예를 들어, 매우 큰 정수 리터럴 값 형식의 변수에 할당할 때 Decimal
합니다.In some cases, you might want to force a literal to a particular data type; for example, when assigning a particularly large integral literal value to a variable of type Decimal
. 다음 예제에서는 오류가 발생 합니다.The following example produces an error:
Dim myDecimal as Decimal
myDecimal = 100000000000000000000 ' This causes a compiler error.
리터럴 표현에서 오류 결과입니다.The error results from the representation of the literal. 합니다 Decimal
데이터 형식을 큰 값을 가질 수 있지만 리터럴으로 암시적으로 표시 됩니다는 Long
, 수는 없습니다.The Decimal
data type can hold a value this large, but the literal is implicitly represented as a Long
, which cannot.
리터럴 두 가지 방법으로 특정 데이터 형식으로 강제 변환할 수 있습니다: 형식 문자를 추가 하 여 또는 묶기 문자 안에 배치 하 여 합니다.You can coerce a literal to a particular data type in two ways: by appending a type character to it, or by placing it within enclosing characters. 형식 문자 또는 문자를 포함 해야 합니다 즉시 리터럴 앞 이나 뒤의 공백이 나 어떠한 종류의 문자 없이 합니다.A type character or enclosing characters must immediately precede and/or follow the literal, with no intervening space or characters of any kind.
이전 예제가 제대로 실행 되도록 하려면 추가 하는 D
형식으로 나타낼 수를 발생 시키는 리터럴 문자를 Decimal
:To make the previous example work, you can append the D
type character to the literal, which causes it to be represented as a Decimal
:
Dim MyDecimal As Decimal = 100000000000000000000D
다음 예제에서는 형식 문자 및 문자 바깥쪽의 올바른 사용법을 보여 줍니다.The following example demonstrates correct usage of type characters and enclosing characters:
' Default to Integer.
Public Const DefaultInteger As Integer = 100
' Default to Double.
Public Const DefaultDouble As Double = 54.3345612
' Force constant to be type Char.
Public Const MyCharacter As Char = "a"c
' DateTime constants.
Public Const MyDate As DateTime = #1/15/2001#
Public Const MyTime As DateTime = #1:15:59 AM#
' Force data type to be Long.
Public Const MyLong As Long = 45L
' Force data type to be Single.
Public Const MySingle As Single = 45.55!
다음 표에서 바깥쪽 문자 및 Visual Basic에서 사용할 수 있는 형식 문자.The following table shows the enclosing characters and type characters available in Visual Basic.
데이터 형식Data type | 구분 기호Enclosing character | 추가 형식 문자Appended type character |
---|---|---|
Boolean |
(없음)(none) | (없음)(none) |
Byte |
(없음)(none) | (없음)(none) |
Char |
"" | CC |
Date |
# | (없음)(none) |
Decimal |
(없음)(none) | D 또는 @D or @ |
Double |
(없음)(none) | R 또는 #R or # |
Integer |
(없음)(none) | I 또는 %I or % |
Long |
(없음)(none) | L 또는 &L or & |
Short |
(없음)(none) | SS |
Single |
(없음)(none) | F 또는!F or ! |
String |
"" | (없음)(none) |
참고자료See also
- 사용자 정의 상수User-Defined Constants
- 방법: 상수 선언How to: Declare A Constant
- 상수 개요Constants Overview
- Option Strict 문Option Strict Statement
- Option Explicit 문Option Explicit Statement
- 열거형 개요Enumerations Overview
- 방법: 열거형 선언How to: Declare an Enumeration
- 열거형 및 이름 한정Enumerations and Name Qualification
- 데이터 형식Data Types
- 상수 및 열거형Constants and Enumerations
피드백
여러분의 의견을 듣고 싶습니다. 제공하려는 유형을 선택하세요.
피드백 시스템은 GitHub 문제를 기반으로 구축되었습니다. 블로그에서 자세히 알아보세요.
피드백 로드 중...