Underscore in Name property

  • Thread starter Thread starter Lee
  • Start date Start date
L

Lee

Hello all,

<new to .net>

If the following piece of code:

Private Sub bDice1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles bDice1.Click, _
bDice2.Click, bDice3.Click, bDice4.Click, bDice5.Click



Dim btn As New OpenNETCF.Windows.Forms.ButtonEx
btn = CType(sender, OpenNETCF.Windows.Forms.ButtonEx)
MessageBox.Show(btn.Name.ToString())

End Sub

..net reports the name prefixed with an underscore. Isn't a prefixed
underscore a convention to represent member variables of a class? Why
would it report it that way? Of course, Name is a member, but should
it report the name like that to another object using it?

I don't mind stripping the underscore off when I need to, but I figured
I'm doing something wrong...
 
It is a VB thing. You have declared bDice1 WithEvents. Part of the way that
"magic" works is to declare behind the scenes the variable with an
underscore. I don't have a reference to hand but if you need (I doubt it) to
dig further, somewhere on the web this is described. If you use the
AddHandler/AddressOf approach, the issue goes away.

I trust just knowing that it is like that by design and only applies to
WithEvents variables is enough. If not, post back.

Cheers
Daniel
 
You can use the .NET reflector (http://www.aisto.com/roeder/dotnet/) to
look at your form and see how it works. For each WithEvents field in
your form class, you will have a field prefixed with an underscore and
a property matching your field name. For with events to work, it uses
a property so that event handlers can be added/removed when the the
field is assigned to a different refence. The property name exactly
matches the name you give to the field so that it is used whenever you
refer to the field.
 
Back
Top