Events and List(Of ...)

M

marcotrapanese

Hello!

I wrote a class "Layers" with one event "DataChanged":

Public Class Layers

Private _Value as Object

Public Property Value() As Object
Get
Return _Value
End Get
Set(ByVal value As Object)
_Value = value
RaiseEvent DataChanged()
End Set
End Property

Public Event DataChanged()

End Class



I use it into another class:

Public Class Gauge

Private WithEvents _Layer As New List(Of Layers)

Public Property Layer() As List(Of Layers)
Get
Return _Layer
End Get
Set(ByVal value As List(Of Layers))
_Layer = value
End Set
End Property

Private Sub _DataChanged() Handles _Layer. <------- I'd
like .DataChanged
Draw()
End Sub
End Class


I'd like to handle in _DataChanged() the DataChanged event raised by
any Layer class into _Layer list.
But VB hide me the event after Hanldes _Layer.

Why? How may I fix this?

Note: if I declare _Layer as new Layers (without the List(Of ...)) all
works fine.

Thank you
Marco / iw2nzm
 
C

Charlie Brown

Hello!

I wrote a class "Layers" with one event "DataChanged":

Public Class Layers

Private _Value as Object

Public Property Value() As Object
Get
Return _Value
End Get
Set(ByVal value As Object)
_Value = value
RaiseEvent DataChanged()
End Set
End Property

Public Event DataChanged()

End Class

I use it into another class:

Public Class Gauge

Private WithEvents _Layer As New List(Of Layers)

Public Property Layer() As List(Of Layers)
Get
Return _Layer
End Get
Set(ByVal value As List(Of Layers))
_Layer = value
End Set
End Property

Private Sub _DataChanged() Handles _Layer. <------- I'd
like .DataChanged
Draw()
End Sub
End Class

I'd like to handle in _DataChanged() the DataChanged event raised by
any Layer class into _Layer list.
But VB hide me the event after Hanldes _Layer.

Why? How may I fix this?

Note: if I declare _Layer as new Layers (without the List(Of ...)) all
works fine.

Thank you
Marco / iw2nzm

You need to include the NEW keyword when declaring your Layers object,
otherwise change your class to have a static(shared) event handler
like so

Public Shared Event DataChanged()

If you use a static event, you may have to wire the event handler
manually instead of using the Handles keyword by using addhandler
somewhere in your code, perhaps a forms Load event

AddHandler Layers.DataChanged, AddressOf _DataChanged()
 
M

marcotrapanese

You need to include the NEW keyword when declaring your Layers object,

mmm... I'm not sure I understand: as you can see above I declared it
in this way:

The NEW keyword is present. Where should I use it?
otherwise change your class to have a static(shared) event handler
like so

Public Shared Event DataChanged()

If you use a static event, you may have to wire the event handler
manually instead of using the Handles keyword by using addhandler
somewhere in your code, perhaps a forms Load event

AddHandler Layers.DataChanged, AddressOf _DataChanged()

Ok, now works fine! Thanks

Marco / iw2nzm
 

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

Top