469,910 Members | 1,581 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,910 developers. It's quick & easy.

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

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

Feb 13 '07 #1
7 2365
"Zytan" <zy**********@yahoo.comschrieb:
>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 S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Feb 13 '07 #2
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
Feb 13 '07 #3
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

Feb 13 '07 #4
"Zytan" <zy**********@yahoo.comschrieb:
>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.

'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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Feb 13 '07 #5
'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
Feb 14 '07 #6
It might be a Unicode issue. Try ChrW instead.
>
Best regards,

Martin
Yup, thanks. Please read Herfried's very informative post for the
precise cause.

Zytan

Feb 14 '07 #7
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

Feb 14 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Steven T. Hatton | last post: by
6 posts views Thread by p|OtrEk | last post: by
6 posts views Thread by C L Humphreys | last post: by
2 posts views Thread by lauren quantrell | last post: by
reply views Thread by tom olson | last post: by
2 posts views Thread by Joe Duchtel | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.