Using constants

I’m sorry, I’ve never understood Xojo constants and I can’t understand the new documentation either.

How do I set a string of characters as a constant string? I want to do this in the winMain opening event and then forget about it so I can always refer to it and never change it.

Thanks if you can help - Steve

Hi Steve,

You could make a constant as const kMyConstant As String = "This Cool Constant"

This may help:
https://documentation.xojo.com/api/language/const.html#const

The other way is to create a class constant.
image

Or that :grin:

Many thanks, I still don’t really understand it though. I thought I could just go to Insert Constant and give it a name and put the string in the default box, but that doesn’t seem to work. Also if I type const kS as string = “mystring” at the start of the winMain Opening event, it seems to be accepted, however if I put a break immediately after the declaration, I can’t find the constant listed anywhere, as I would if it were a variable. If I then put dim x as string = kS, I can see x has been populated by mystring. So I guess that will work, but it seems a complicated way of doing it and will I always be able to reference kS throughout the app or at least in the winMain and Modules? I suppose I can find out by trying but am getting so tired I need to get some sleep. Must go and bang my head against the wall until I pass out. Thanks for the help, people - Steve

If you want to use the constant outside of the method you’ll need to add a class constant.

Why you say it doesn’t seem to work?

If you need global constants add a module to your project and then add constants to the module.

2 Likes

Don’t forget about Scope - is the constant private or public?
If the constant is attached to a window, it can only be accessed by components of THAT window if it’s set to private.
If you want to access it from another window, it needs to be public, and and you need a reference to that window to access its constant.

If the constant is attached to the app and it’s public, then you can reference it from any window using app.myconstantname

2 Likes

Constants in code don’t exist at runtime. They get replaced with the literal value before the code is compiled.

i.e., you write this:

const kS as string = "mystring"
dim x as string = kS

The compiler will see this:

dim x as string = "mystring"
1 Like

Oh.
That’s a bit of surprise.
So while you create one global constant for use ‘everywhere’, if it happens to be a sizeable string, it actually increases the size of the app because the compiler makes copy after copy ?
Seems inefficient.

I appreciate that many people don’t care about the compiled size, and that this is an edge case that assumes large strings and repeated usage.
BUT for that case (straying off topic a bit) to keep the compiled size down, you could create a global computed variable , with only a Get side, which returned the string value you want. That string would only exist once in the compiled build on this basis.

Only a Xojo engineer can say for sure, but I think the compiler coalesces identical string literals. I just ran a quick and dirty test and the output exe only had one instance of the string.

1 Like

Thanks for looking.
That suggests the literal they insert is a pointer to the string storage. Thats more like what I expected.

That is true, but the constant name doesn’t exist in the executable.

And, since strings are immutable, only one instance needs to exist. For example

dim s1, s2, s3 as string
s1 = "Hello World"
s2 = s1
s3 = s2

There is only one memory location that contains “Hello World”. There aren’t 3 copies.

1 Like

It all seems over-complicated. I have a Module in the project and I see that Modules don’t have any events. What a boring pointless life they must have. I have given what I wanted to be a simple string constant to the module as a string Property set in the opening event of winMain and it works fine. I just won’t change it. What do you call a string variable you aren’t ever going to change?

The difference between a module and a class IS the lack of events.

A computed variable which has no ‘Set’ method.

Right click your property, choose ‘Convert to computed property’
When that is done, you get a Get and a Set method
remove the contents of the Set, or remove the Set method.
Now you have a string you cant amend.

If anyone tries to change it, I think I’ll just have to get my app to play a recording of John Gielgud in Shakespeare’s Julius Caesar, declaiming: “I am constant as the Northern Star, of whose true fixed and resting quality there is no fellow in the firmament.”

1 Like

The only reason to use a property for a constant is if the value needs to be calculated.

If it is a fixed value then all you need to do is:

  1. Choose Module from the Insert menu.

  2. Make sure the module is selected in the Navigator.

  3. Choose Constant from the Insert menu.

  4. Set the constant’s name, value and type.

  5. Set the constant scope:
    public to be global
    protected to be name spaced ModuleName.Constant
    private to be private to the module