variable name

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

hello all,
I have 2 class vars:
Private _chkTemp() As CheckBox

Private WithEvents chkTemp As CheckBox

And get an error on _chkTemp:

Conflicts with 'Private Dim _chkTemp As System.Windows.Forms.CheckBox',
which is implicitly declared for 'Private Dim WithEvents chkTemp As
System.Windows.Forms.CheckBox' in class 'frmX'.

Looks like the underscore does not guarantee uniqueness of a variable. Is
there something special about underscores?

Thanks
Frank
 
Hello Frank

I've run FXCop on my project and one of errors i got was :

Do not use underscores when specifying parameter names.
Some generated Visual Studio identifiers for applications
contain underscore characters. Underscore characters
should generally be avoided in public identifiers
specified in libraries.

Kind Regards
Jorge
 
Frank said:
hello all,
I have 2 class vars:
Private _chkTemp() As CheckBox

Private WithEvents chkTemp As CheckBox

And get an error on _chkTemp:

Conflicts with 'Private Dim _chkTemp As System.Windows.Forms.CheckBox',
which is implicitly declared for 'Private Dim WithEvents chkTemp As
System.Windows.Forms.CheckBox' in class 'frmX'.

Looks like the underscore does not guarantee uniqueness of a variable. Is
there something special about underscores?

Thanks
Frank

If you run ildasm against one of your exe or dlls (I am using a aspx.net
dll) you will find each of the 'withevents' objects seems to be translated
by vb into a property which gets/sets a private variable with the same name
prefixed by an underscore.
I cant say if its a VB thing only or .net in general.
 
* "Frank said:
I have 2 class vars:
Private _chkTemp() As CheckBox

Private WithEvents chkTemp As CheckBox

And get an error on _chkTemp:

Conflicts with 'Private Dim _chkTemp As System.Windows.Forms.CheckBox',
which is implicitly declared for 'Private Dim WithEvents chkTemp As
System.Windows.Forms.CheckBox' in class 'frmX'.

Looks like the underscore does not guarantee uniqueness of a variable. Is
there something special about underscores?

Yes, there is something special about underscores. VB.NET generates
fields automatically and the names of these fields begin with '_'. So
you will have a name conflict.

I suggest to use the 'm_' prefix instead, because this prefix will avoid
those clashes and it will make code better readable, especially when
procedure separators are turned on.
 
...better readable, especially when procedure separators are turned on.

That is definately true.

Now I finally have two good reasons to stop using _ prefixes.

Greg
 
If you run ildasm against one of your exe or dlls (I am using a aspx.net
dll) you will find each of the 'withevents' objects seems to be translated
by vb into a property which gets/sets a private variable with the same name
prefixed by an underscore.
I cant say if its a VB thing only or .net in general.

It's a VB thing. VB needs a way to call AddHandler/RemoveHandler
whenever you assign to the variable, so it hides the "WithEvents" field
behind a property.

None of the other major .NET languages do this, but it's possible that a
few of the less popular compilers might do the same thing, I don't know.
 

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

Back
Top