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.
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
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
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.
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?
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.”