Constants

Ricky D

Well-Known Member
Licensed User
Longtime User
I do it like this :

B4X:
Dim someconst As String = "some string"

then reference someconst anywhere as you would if we did have constant declarations like this :

B4X:
Dim mynewvar As String = "new value"
mynewvar = mynewvar & someconst 'results in new valuesome string

You can declare the "constant" as any regular B4A type.

Does this make sense?

regards, Ricky
 
Last edited:
Upvote 0

Jost aus Soest

Active Member
Licensed User
Longtime User
Constants are 0-ary functions!

Use something like this to prevent setting the "variable" to a new value:
B4X:
Sub ConstInt As Int: Return 33: End Sub
Sub ConstString As String: Return "bla bla": End Sub

@Erel:
What's about automagically pre-compiling
B4X:
Const n As t = v
to
B4X:
Sub n As t: Return v: End Sub
in the next(?) version of B4A? ;)
 
Upvote 0

EduardoElias

Active Member
Licensed User
Longtime User
I get frequently frustrated with the lack of constants and enum types. I come from Delphi background where these are used frequently.

I found a way that is helping me with the version 2.50+ when compiling to library option. I create the main class code module, and add another code module only to define in process_globals the constant variables, with 'c' starting on the name.

I name this module with 'const' added to library name, to make more readable.

For those that like creating subs for constants it could be an option also. Since commonly constants are global to the entire source code in almost all languages.

So it will show up even if you do not have the main class created yet.

just my 2 cents.
 
Upvote 0

Joe_L

Member
Licensed User
Longtime User
+1. In 35 years of programming I have never come across a language without constants. Even 8 bit 8080 assembler had it. Utterly essential, especially working across multiple shared modules. Consts need to be public/private too.
 
Upvote 0

EduardoElias

Active Member
Licensed User
Longtime User
+1. In 35 years of programming I have never come across a language without constants. Even 8 bit 8080 assembler had it. Utterly essential, especially working across multiple shared modules. Consts need to be public/private too.

yes, but consider that b4a is new, i believe 2 years old, and there are many things to implement. When i get first version 1.90, was a lot more simple, now we have got some stuff (classes, libraries) I believe we should keep feeding that can help direct where b4a should go.
 
Upvote 0
Top