Search notes:

VBA statement: const

The VBA statement const declares a constant value.
option explicit

const g_forty_two = 42

private const g_seven = 7
public  const g_five  = 5

'
' It is possible to assign a calculated value to
' a constant:
'
const calculated = g_seven * g_five

'
' However, it is not possible to assign a value to a
' constant as returned value from a function. The following
' statement would cause the error:
'
'      Constant expression required
'
' const anotherCalculated = someNumber()


'
' When declaring constants, their data type can be
' explicitely stated. Without declaring the datatype,
' the data type of the following constant would be integer:
'
const dataTypedConst as long = 10


sub main() ' {

   '
   ' Constants, obviously, cannot be changed.
   ' The following statements would cause the error:
   '
   '    Assignment to constant not permitted
   '
   ' g_forty_two = 45
   ' g_seven = 9
   ' g_five  = 18

   debug.print ("Calculated = " & calculated)

   debug.print(typeName(g_seven       )) ' Integer
   debug.print(typeName(dataTypedConst)) ' Long

end sub ' }

function someNumber() ' {
    someNumber = 99
end function ' }
Github repository about-VBA, path: /language/statements/const.bas

See also

The dim statement.
VBA statements

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1745453942, '18.223.239.171', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/VBA/language/statements/const(96): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78