Is it possible to declare a dynamic constant in VB .NET?
constants, dynamic, vb.net
Solution
What you are looking for is the readonly keyword. A time stamp has to be calculated at run time and cannot be constant.
ReadOnly TIME_STAMP As String = Format(Now(), "hhmm")
Problem
I'm trying to save a timestamp into a constant at the beginning of a program's execution to be used throughout the program. For example: ``` Const TIME_STAMP = Format(Now(), "hhmm") ``` However, this code generates a compiler error - "Constant expression is required." Does that mean all constants in VB .NET have to contain flat, static, hard-coded data? I know that it's possible to initialize a constant with a dynamic value in other languages (such as Java) - what makes it a constant is that after the initial assignment you can no longer change it. Is there an equivalent in VB .NET?