Michael Van Cleave
Traveling the technical world, learning the language

VB.NET Structures with Events?

Tuesday, October 18, 2005 6:39 AM

So it has been posted previously that Structures were very much like Classes in the .NET framework with some very subtle differences. 

While going over the material on structures in my class last night one of my better students that comes from a Java background asked a very interesting question.  “If structures are so close to classes then can they handle events?”

Come to find out you can declare an event in a structure just as you can declare a Sub, Functions, Properties, and Constructors.  But when I tried to declare a variable in my code as the type of structure “WithEvents”, the compiler seemed to have a problem with it.  Also, it seems that if you were forgo the “WithEvents” declaration then you are not able to attach to the event with a handler.

So it seems that I was able to create the event, but not actually attach to it to do anything useful. 

Now, I know that most of my recent experience is in C# where things don't seem to be as complicated and wordy as VB.NET, but I am sure that could have not typed in a step or missed some keyword.  So if there is anyone out there that might have a good example of this, please let me know.  I would love to show my class this.

Thanks.

Michael


Feedback

# re: VB.NET Structures with Events?

Imports System.Text
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TxtTest As System.Windows.Forms.TextBox

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.TxtTest = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(5, 5)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(110, 20)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'TxtTest
'
Me.TxtTest.Location = New System.Drawing.Point(5, 40)
Me.TxtTest.Multiline = True
Me.TxtTest.Name = "TxtTest"
Me.TxtTest.Size = New System.Drawing.Size(245, 85)
Me.TxtTest.TabIndex = 1
Me.TxtTest.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 148)
Me.Controls.Add(Me.TxtTest)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region
Structure StudentRecord
'Eventi
Event AgeChanged()
'campi privati
Private mName As String
Private mAge As Integer
'Proprietà
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal NewName As String)
mName = NewName
End Set
End Property
Public Property Age() As Integer
Get
Return mAge
End Get
Set(ByVal NewAge As Integer)
mAge = NewAge
RaiseEvent AgeChanged()
End Set
End Property

'costruttore!
Public Sub New(ByVal inName As String, ByVal inAge As Integer)
Name = inName
Age = inAge
End Sub


' Metodo per solevare eventi
Public Sub ChangeAge()
RaiseEvent AgeChanged()
End Sub
End Structure


Class StudentClass
'Public Name As String
'Public Age As Integer
Private mName As String
Private mAge As Integer
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal NewName As String)
mName = NewName
End Set
End Property
Public Property Age() As Integer
Get
Return mAge
End Get
Set(ByVal NewAge As Integer)
mAge = NewAge
End Set
End Property
Public Sub New(ByVal Name As String, ByVal Age As Integer)
Me.Name = Name
Me.Age = Age
End Sub
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sBuilder As New StringBuilder

Dim StudA As StudentRecord = New StudentRecord("Bob (Struct)", 35)
Dim StudB As StudentRecord = StudA
'Da questo punto in avanti, teniamo d'occhio studA.Age
AddHandler StudA.AgeChanged, AddressOf Me.ChangeTHeAge
'
'Da questo punto in avanti, teniamo d'occhio studB.Age
AddHandler StudB.AgeChanged, AddressOf Me.ChangeTHeAge
With StudB
.Name = "Sue (Struct)"
.Age = .Age + 1
End With

Call PrintTHis(StudA, sBuilder)
Call PrintTHis(StudB, sBuilder)

'
sBuilder.Append("-", 20)
sBuilder.Append(vbCrLf)
'
Dim StuClassA As StudentClass = New StudentClass("Bob (Class)", 35)
Dim StuClassB As StudentClass = StuClassA
With StuClassB
.Name = "Sue (Class)"
.Age = .Age + 1
End With
Call PrintTHis(StuClassA, sBuilder)
Call PrintTHis(StuClassB, sBuilder)
TxtTest.Text = sBuilder.ToString()
'Da farsi sempre
RemoveHandler StudA.AgeChanged, AddressOf Me.ChangeTHeAge
RemoveHandler StudB.AgeChanged, AddressOf Me.ChangeTHeAge
End Sub

Private Sub PrintTHis( _
ByVal studX As StudentClass _
, ByVal sBuilder As StringBuilder)

Dim TmpStuRec As StudentRecord
TmpStuRec.Age = studX.Age
TmpStuRec.Name = studX.Name
Call PrintTHis(TmpStuRec, sBuilder)
End Sub

Private Sub PrintTHis( _
ByVal studX As StudentRecord _
, ByVal sBuilder As StringBuilder)
sBuilder.Append("Nome= " & studX.Name & " Età:" & studX.Age & vbCrLf)
End Sub
Private Sub ChangeTHeAge()
MessageBox.Show("Age Changed!!")
End Sub
End Class
3/7/2007 8:58 AM | Cesare

# re: VB.NET Structures with Events?

Excellent. I will be sure to make this a part of my lesson plan now.

Thank you very much.

Michael 3/7/2007 9:34 AM | Michael

Post a comment





 

Please add 3 and 2 and type the answer here:




Archives

Post Categories

Great Links

Other Blogs

Pod Casts

Syndication: