Which one of the following is invalid?

G

Guest

Without typing it in, which one of the following is invalid and why?


Public Shared ReadOnly mc_SingleQuote_One As Char = Convert.ToChar(&H27)
Public Const mc_SingleQuote_Two As Char = Microsoft.VisualBasic.ChrW(&H27)






















Answer:
They are both VALID.
Most people will guess that the 2nd one is invalid because const variables
require a constant value for initialization.
However, the 2nd version works.

The question is...WHY?

It appears that the Microsoft.VisualBasic assembly is automatically calling
the ChrW function once the value of the parameter is changed. Looking at the
documentation, it appears that ChrW is language independent. Ok...but when I
change it to Chr, it also works! Chr DOES take into account the CodePage.

Why are the values produced by these functions allowed to be stored in a
const?
Is the Microsoft.VisualBasic assembly somehow embedded in the IDE?

As far as being, which one is "better"...I would go with the first option
because it can be directly related to its C# counterpart. Furthermore, the
shared variables are only loaded once a call is made into the class
containing them. This provides lazy instantiation, which is ncie, but the
const provides a precompiled value.
 
M

Mattias Sjögren

The question is...WHY?

http://msdn.microsoft.com/library/en-us/vbls7/html/vblrfvbspec9_1_1.asp

"The following constructs are permitted in constant expressions:
[...]
The following run-time functions:
Microsoft.VisualBasic.Strings.ChrW
Microsoft.VisualBasic.Strings.Chr, if the constant value is
between 0 and 128."

Why are the values produced by these functions allowed to be stored in a
const?

Because the compiler knows how to evaluate the expression at compile
time.



Mattias
 

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