const string = chr() & chr() is picky

Z

Zytan

I am trying to set a const string made up of Chr(), but if the value
passed into Chr() is too large, the compiler complains that it is not
constant!! Example:

Public Const m_Data As String = Chr(&HD) & Chr(&HE) 'ok
Public Const m_Data As String = Chr(&HD0) & Chr(&HE0) 'not ok

But, Char stores 0..255, so it should be ok in both cases. What's
going on?

Zytan
 
H

Herfried K. Wagner [MVP]

Zytan said:
I am trying to set a const string made up of Chr(), but if the value
passed into Chr() is too large, the compiler complains that it is not
constant!! Example:

Public Const m_Data As String = Chr(&HD) & Chr(&HE) 'ok
Public Const m_Data As String = Chr(&HD0) & Chr(&HE0) 'not ok

But, Char stores 0..255, so it should be ok in both cases. What's
going on?

Note that 'Chr' uses Windows ANSI. Maybe it's better to use 'ChrW', which
is Unicode-enabled, instead.
 
M

Martin H.

Hello Zytan,
Public Const m_Data As String = Chr(&HD) & Chr(&HE) 'ok
Public Const m_Data As String = Chr(&HD0) & Chr(&HE0) 'not ok

But, Char stores 0..255, so it should be ok in both cases. What's
going on?

It might be a Unicode issue. Try ChrW instead.

Best regards,

Martin
 
Z

Zytan

Note that 'Chr' uses Windows ANSI. Maybe it's better to use 'ChrW', which
is Unicode-enabled, instead.

You are completely right. I knew the above, but i didn't think it
mattered. I was thinking ChrW() would return a two byte Char. Which
it does, and so does Chr(), since it's all unicode in VB .NET. Duh.

I still don't understand why it was complaining. I should note that
Chr() does accept 0..255, and I was within that range. So, something
is still wrong here.

But your suggestion works, so thanks!

Zytan
 
H

Herfried K. Wagner [MVP]

Zytan said:
You are completely right. I knew the above, but i didn't think it
mattered. I was thinking ChrW() would return a two byte Char. Which
it does, and so does Chr(), since it's all unicode in VB .NET. Duh.

I still don't understand why it was complaining. I should note that
Chr() does accept 0..255, and I was within that range. So, something
is still wrong here.


'Chr' does not accept values greater than 127 when used in constants. The
reason is that 'Chr' would return different characters depending on the
system's Windows ANSI codepage for larger values. 'ChrW' always returns the
same character for a certain character code.
 
Z

Zytan

'Chr' does not accept values greater than 127 when used in constants.

Sounds peculiar...
The
reason is that 'Chr' would return different characters depending on the
system's Windows ANSI codepage for larger values.

....until you said that.
'ChrW' always returns the
same character for a certain character code.

It all makes sense. And now that's something I'll remember. Again,
thanks for a very informative post, Herfried.

Zytan
 
Z

Zytan

Note that 'Chr' uses Windows ANSI. Maybe it's better to use 'ChrW', which
is Unicode-enabled, instead.

Wait, luckily i had a debug.assert() to ensure things were ok, and i
found the following are not equivalent:

Dim a As String = Chr(&HD0) & Chr(&HE0)
Dim b As String = ChrW(&HD0) & ChrW(&HE0)

So, using ChrW(), while it allows b to be Const, where a cannot be
Const, it does not make b = a.

Zytan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top