PC Review


Reply
Thread Tools Rate Thread

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

 
 
Zytan
Guest
Posts: n/a
 
      13th Feb 2007
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

 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      13th Feb 2007
"Zytan" <(E-Mail Removed)> schrieb:
>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/>

 
Reply With Quote
 
Martin H.
Guest
Posts: n/a
 
      13th Feb 2007
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
 
Reply With Quote
 
Zytan
Guest
Posts: n/a
 
      13th Feb 2007
> 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

 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      13th Feb 2007
"Zytan" <(E-Mail Removed)> schrieb:
>> 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/>

 
Reply With Quote
 
Zytan
Guest
Posts: n/a
 
      14th Feb 2007
> '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


 
Reply With Quote
 
Zytan
Guest
Posts: n/a
 
      14th Feb 2007
> 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

 
Reply With Quote
 
Zytan
Guest
Posts: n/a
 
      14th Feb 2007
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
string constants / conversion from const char * to String kelvin.koogan@googlemail.com Microsoft VC .NET 5 25th Jun 2007 01:35 PM
String^, const char*, std::string, and c_str( ) Kevin Frey Microsoft VC .NET 3 21st Feb 2007 02:03 AM
Can't use String[] as const John Bailo Microsoft C# .NET 3 7th Dec 2004 07:45 AM
const string values ESPN Lover Microsoft C# .NET 6 15th Oct 2004 07:51 AM
System::String, std::string and const char* and newline Vasco Lohrenscheit Microsoft VC .NET 0 13th Jan 2004 03:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:39 AM.