User Tools

Site Tools


k7:k7.1:start

7.1 Constants and Enumeration

In contrast to variables, constants contain any number of fixed values that you cannot change at program runtime. A corresponding attempt would result in an error message! You must also specify the required data type for the constants in the declaration.

7.1.1.1 Constants

Constants data types can only be Boolean, integer, long, float or string. With the optional keywords Public or Private you can set the access rights to a constant. If you use the keyword Public, the declared constant can also be accessed from the other classes that have a reference to an object of this class. Without public or private, only one class global constant is declared.

Public Const FILE_PATH As String = "/home/hans/files"
Const COURSE_NUMBER As Integer = 7
Const FACTOR As Float = 2.222
Private Const DEBUG As Boolean = True

These declarations of constants are incorrect:

(1) Const VALUE As Float = COURSE_NUMBER / FACTOR
(2) Const FACTOR3 As Float = 2.71828182845 / 3.1415
(3) Const DATE As Date = "12.12.2012"
(4) Const NUMBER_PI As Float = Pi()

Comment:

  • Calculations are performed in lines 1 and 2.
  • The Date data type must not be used for a constant declaration (line 3).
  • In line 4, the constant NUMBER_PI is assigned the function value of a function, which is not allowed.

7.1.2 Enumeration - Enumeration

An enumeration is a list of integer constants in a list where each constant has its own name and integer value. The individual values in the list are separated by commas. If you do not assign an integer value, the first value in the list is 0 and all subsequent values are increased by 1. The keyword is Enum. You can also define access rights for constants using the keywords Public or Private.

(1) Public Enum Spring, Summer, Autumn, Winter
(2) Private Enum top = 5, bottom, left, right
(3) Enum LehmannRed = 16711680, MeierYellow = &HFFFF44, BoegeGreen = &H00FF08
(4)
(5) Public Sub SetSeason(iSeason As Integer)
(6)   Select Case iSeason
(7)     Case Spring
(8)         Message.Info("Spring lets its blue ribbon ...!")
(9)     Case Summer
(10)         Message.Info("Summer, sun, Baltic Sea beach!")
(11)     Case Autumn
(12)         Message.Info("When the leaves fall in autumn ...!")
(13)     Case Else ' Winter
(14)         Message.Info("Winter at last!")
(15)   End Select ' iSeason
(16) End ' SetSeason(...)
(17)
(18) Public Sub SetColour(iColour As Integer)
(19)   TextArea1.Background = iColour
(20) End ' SetColour(...)
(21)
(22) Public Sub btnEnumeration_Click()
(23)    SetSeason(Winter)
(24)    SetColour(MeierGelb)
(25)    Message.Info("The value for left is " & left & ".")
(26) End ' btnEnumeration_Click()

Comments:

  • The comments here refer only to the declarations of the enumerations. The other procedures show how to use the three lists of integer constants.
  • In line 1, a list of integer constants is created in which the value 0 is automatically assigned to the first constant. For all further constants, the start value is increased by 1. You could have declared the list with Spring=0, Summer=1, Autumn=2, Winter=3.
  • In the second list, the number 5 is assigned to the first value. Thus, the constant is automatically assigned the value 6 at the bottom, the value 7 at the left, and the value 8 is the corresponding value for the right.
  • In the third list, each constant in the colour list is assigned its own integer value (e. g. hexadecimal).
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k7/k7.1/start.txt · Last modified: 05.02.2022 (external edit)

Page Tools