473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Testing identifier names for validity

I was wondering if there is way in the .NET framework to test if a
string represents a valid identifier name for the C# language. I want to
write a program that will generate code based on user input. Part of the
program's job will be to use names provided by the user to name classes,
methods, etc. Some error checking would be involved to make sure the
names use legal C# syntax. Before rolling my own code to do the error
checking, I wanted to check to see if this functionality already exists.
Nov 16 '05 #1
5 3018

"Wavemaker" <ja**********@BiteMeHotmail.com> wrote in message
news:nb********************@comcast.com...
I was wondering if there is way in the .NET framework to test if a string
represents a valid identifier name for the C# language. I want to write a
program that will generate code based on user input. Part of the program's
job will be to use names provided by the user to name classes, methods,
etc. Some error checking would be involved to make sure the names use legal
C# syntax. Before rolling my own code to do the error checking, I wanted to
check to see if this functionality already exists.


If you are using the code dom, I'm pretty sure this is handled for you(it'll
output identifiers named the same as keywords with the @ prefix).

Otheriwse, if you are generating code by hand, you will have to write code
to do it yourself, I'm afraid.
Nov 16 '05 #2

"Daniel O'Connell [C# MVP]" wrote:
"Wavemaker" wrote:
I was wondering if there is way in the .NET framework to test if a
string represents a valid identifier name for the C# language. I want
to write a program that will generate code based on user input. Part
of the program's job will be to use names provided by the user to name
classes, methods, etc. Some error checking would be involved to make
sure the names use legal C# syntax. Before rolling my own code to do
the error checking, I wanted to check to see if this functionality
already exists.


If you are using the code dom, I'm pretty sure this is handled for
you(it'll output identifiers named the same as keywords with the @
prefix).

Otheriwse, if you are generating code by hand, you will have to write
code to do it yourself, I'm afraid.


It's the latter. I've been thinking about this, though, and it shouldn't
be too hard to do this myself. The trickest part seems to be the first
character in the name. It will have to be treated as a special case.
Here's a small algorithm I just wrote for testing class names:

- Trim all whitespace from the beginning and end of the string.

- If the first character is NOT an underscore OR NOT a character OR NOT
an asterick, the identifier is NOT valid.
Return false.

- For each remaining character:
- If the character is NOT an underscore OR NOT a number OR NOT a
character, the identifier is NOT valid.
Return false.

- Return true.

I think this algorithm should work for what I'm doing, unless I've
missed something. Any comments will be welcomed.

Nov 16 '05 #3

"Wavemaker" wrote:

- If the first character is NOT an underscore OR NOT a character


Actually, I should have said:

- If the first character is NOT an underscore OR NOT a letter.
Nov 16 '05 #4

"Wavemaker" <ja**********@BiteMeHotmail.com> wrote in message
news:tJ********************@comcast.com...

"Daniel O'Connell [C# MVP]" wrote:
"Wavemaker" wrote:
I was wondering if there is way in the .NET framework to test if a
string represents a valid identifier name for the C# language. I want
to write a program that will generate code based on user input. Part
of the program's job will be to use names provided by the user to name
classes, methods, etc. Some error checking would be involved to make
sure the names use legal C# syntax. Before rolling my own code to do
the error checking, I wanted to check to see if this functionality
already exists.


If you are using the code dom, I'm pretty sure this is handled for
you(it'll output identifiers named the same as keywords with the @
prefix).

Otheriwse, if you are generating code by hand, you will have to write
code to do it yourself, I'm afraid.


It's the latter. I've been thinking about this, though, and it shouldn't
be too hard to do this myself. The trickest part seems to be the first
character in the name. It will have to be treated as a special case.
Here's a small algorithm I just wrote for testing class names:

- Trim all whitespace from the beginning and end of the string.

- If the first character is NOT an underscore OR NOT a character OR NOT
an asterick, the identifier is NOT valid.
Return false.


An asterisk is not a valid identifier character, it is an operator and a
type post fix.
Nov 16 '05 #5

"Daniel O'Connell [C# MVP]" wrote:
"Wavemaker" wrote:
Here's a small algorithm I just wrote for testing class names:

- Trim all whitespace from the beginning and end of the string.

- If the first character is NOT an underscore OR NOT a character OR
NOT
an asterick, the identifier is NOT valid.
Return false.


An asterisk is not a valid identifier character, it is an operator and
a > type post fix.


Thanks!

Erm, I noticed that my algorithm doesn't test to make sure the name is
not a reserved keyword if it is not prepended with an asterick. Then
there are Unicode escape sequences to watch out for... Looks like this
isn't as trivial as I thought it might be.
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tom Verbeure | last post by:
Hello All, so I'm now convinced that unit testing is really the way to go and I want to add it to an existing project. The first thing that I find out, is that, well, it's hard work. Harder than...
21
by: scandal | last post by:
I am a javascript newbie working on a script that checks whether a "path" from one element in an array to another is "blocked." Currently, the script pushes an already processed cell index (hence...
46
by: James Harris | last post by:
Before I embark on a new long-term project I'd appreciate your advice on how to split up long names. I would like to keep the standards for command names the same as that for variable names....
7
by: Tony Tone | last post by:
I am having trouble resolving this issue: Server Error in '/test' Application. -------------------------------------------------------------------------------- Compilation Error Description:...
39
by: windandwaves | last post by:
Hi Folk I have to store up to eight boolean bits of information about an item in my database. e.g. with restaurant drive-through facility yellow windows
9
by: Andy Dingley | last post by:
Here's a chunk of a longer piece of punditry I'm working on, re: the choices between doctypes for authoring and the State of the Union for validity. I've got a bucketload of nasty code and a bunch...
3
by: David T. Ashley | last post by:
I have this ugly habit of prefixing all function and variable names with some identifying information about the owning module and perhaps also the subcategory of function, i.e. ...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
21
by: Thomas Pornin | last post by:
Hello, with gcc-4.2.3, this does not compile: typedef int foo; int f(foo) int foo; { return foo; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.