Skip navigation.

Blog

Using Reserved Words as Identifiers in VB.Net

Up until now, if I wanted to use a reserved keyword as a variable, function or property name (such as Public Property ReadOnly() As Boolean), the compiler would complain.

Luckily I happened across a very simple answer yesterday; VB.Net works in a similar way to SQL - to use a reserved word as an identifier, just put square brackets around it!

i.e. Public Property [ReadOnly]() As Boolean

File under: things I can't believe I didn't know!


Reader Comments

Skip to form

April 16, 2009, ryla says:

THANKS works like a charm

October 15, 2011, anonymous says:

This was in VB6 also, AFAIK.

October 25, 2011, Karin says:

This doesn't seems to work in .net 4

October 25, 2011, Theo Gray says:

Hello Karin,

I've just tested here and all apears to work fine. e.g.:

Dim _ReadOnly As Boolean = True
Private Property [ReadOnly] As Boolean
    Get
        Return _ReadOnly
    End Get
    Set(ByVal value As Boolean)
        _ReadOnly = value
    End Set
End Property

or just like the example above:

Public Property [ReadOnly]() As Boolean

October 25, 2011, Karin says:

I have a keyword in a namespace in a data model (edmx) which works fine in .net 3.5 but not in .net 4
:o(

October 25, 2011, Theo Gray says:

Ah, ic, so you're saying this technique doesn't work in an .edmx file?

October 25, 2011, Theo Gray says:

I have no experience with edmx, but are would changing the case of a offending name (e.g Name="readONLY") possibly get it to become useable?


Comment on This Article:

Your Name:
Your Email Address:
 

Your Email Address will not be made public.
Comment:
All HTML, except <i>, <b>, <u> will require your comment to be moderated before it is publicly displayed.
 
If you would like your own avatar displayed, read about comment avatars.