Class Constants- Strings with non-printable characters?

Curious- Is it possible / reasonable to create class constants as strings that contain non-printable characters? For example, I want to store a string (“ESC!?”) which contains an embedded ESC character. I would normally construct as:

GET_PRINTER_STATUS_CMD = chr(27) + "!?"

This is one example from a bunch of fixed strings used for interrogating the status of a serially connected device. Since there are so many of them I thought it might make the code more self-documenting if I could throw them into class constants with meaningful names.

A thought

If you add global computed variables to a module, you can leave the SET method empty so that any attempt to write back will fail, and they will be ‘constant’

1 Like

… = &u1B + “!?”

That will only work for character codes less than 128.

1 Like

When entered into the IDE as the default value for a class constant, the &u1b is taken literally. i.e., not interpreted as a Unicode codepoint.

I missed that you wanted a class constant. A shared computed property is probably the best way to go.

NO

Yes but it has some risks. It is better if you create the string, encode it in base64 and store THAT in the constant. Then just decode it before use it.

Dim DecodedVariable As String = DecodeBase64(SomeEncodedConst)

While you can’t enter an escape character into the constant editor in the IDE you can paste a string containing the escape characters.

Try this.
Create a project. In the openning event handler add

Var s As String = ChrB(&h1B) + "!?"
Var c As New Clipboard
c.Text = s

Run the project to push the escape sequence into your clipboard. Add a constant and paste into the default value field. On Windows I found I needed to press the ellipses button & paste again.

Now you have a constant that contains your escape sequence.

3 Likes