Why can't a ReadOnly field be combined with WithEvents?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Class MyClas
Friend ReadOnly WithEvents MyObject as AnotherClas
..
End Clas

I have a VB.NET class that has a readonly object (i.e. It can only be assigned in the constructor). This enables me to safely expose it at friend scope without the possibility that it can be accidentally released or replaced. If however I want to handle its events I find that the compiler objects to the introduction of the WithEvents keyword. I tried to do the same thing in C# and it compiled fine, although I don't think C# doesn't support declarative event handlers. Does anyone know the reasoning behind these problems in VB.NET?

On a similar note why can't I specify a declarative event handler on a shared or module event?
 
Don't declare it as a field, declare it as a property

i.e.

Class MyClass

Private WithEvents _MyObject as AnotherClass

Friend Readonly Property MyObject as AnotherClass
Get
Return _MyObject
End Get
End Property


DareDevil said:
Class MyClass
Friend ReadOnly WithEvents MyObject as AnotherClass
...
End Class

I have a VB.NET class that has a readonly object (i.e. It can only be
assigned in the constructor). This enables me to safely expose it at friend
scope without the possibility that it can be accidentally released or
replaced. If however I want to handle its events I find that the compiler
objects to the introduction of the WithEvents keyword. I tried to do the
same thing in C# and it compiled fine, although I don't think C# doesn't
support declarative event handlers. Does anyone know the reasoning behind
these problems in VB.NET?
 
* =?Utf-8?B?RGFyZURldmls?= said:
On a similar note why can't I specify a declarative event handler on a shared or module event?

For shared events, you can do that:

\\\
Public Class Main
Public Shared Sub Main()
AddHandler SampleClass.SampleEvent, AddressOf SampleClass_SampleEvent
SampleClass.RaiseSampleEvent()
RemoveHandler SampleClass.SampleEvent, AddressOf SampleClass_SampleEvent
Console.Read()
End Sub
Private Shared Sub SampleClass_SampleEvent()
Console.WriteLine("SampleEvent occured!")
End Sub
End Class

Public Class SampleClass
Public Shared Event SampleEvent()

Public Shared Sub RaiseSampleEvent()
RaiseEvent SampleEvent()
End Sub
End Class
///
 
DareDevil said:
Class MyClass
Friend ReadOnly WithEvents MyObject as AnotherClass
...
End Class

I have a VB.NET class that has a readonly object (i.e. It can only be
assigned in the constructor). This enables me to safely expose it at
friend scope without the possibility that it can be accidentally
released or replaced. If however I want to handle its events I find
that the compiler objects to the introduction of the WithEvents
keyword. I tried to do the same thing in C# and it compiled fine,
although I don't think C# doesn't support declarative event handlers.
Does anyone know the reasoning behind these problems in VB.NET?

WithEvents makes the compiler create a property, and in it's Set-procedure,
the Addhandler statements necessary to handle the events specified by the
Handles keyword are inserted. As there is no Set-procedure with a Readonly
property, this is no place to insert Addhandler, consequently WithEvents is
not possible.
On a similar note why can't I specify a declarative event handler on
a shared or module event?

Pardon?


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Problem with that is that firstly I can now muck up the _MyObject assignment outside of the constructor but within the class. Secondly outside of the class I am unable to attach declarative event handlers (keyword: handles), I must use addhandler and removehandler instead even through for my purposes the event handlers are not dynamic within the lifespan of the application. Its a pain!
 
Dude, thats all Handles actually does...

calls addHandler

word.

DareDevil said:
Problem with that is that firstly I can now muck up the _MyObject
assignment outside of the constructor but within the class. Secondly outside
of the class I am unable to attach declarative event handlers (keyword:
handles), I must use addhandler and removehandler instead even through for
my purposes the event handlers are not dynamic within the lifespan of the
application. Its a pain!
 
What I'd like to be able to do the following but it doesn't compile. I like to know is it just a limitation of way VB.NET has been made or is there a practical reason why it shouldn't be/isn't possible
\\Public Class Mai
Public Shared Sub Main(
SampleClass.RaiseSampleEvent(
Console.Read(
End Su
Private Shared Sub SampleClass_SampleEvent() handles SampleClass.SampleEven
Console.WriteLine("SampleEvent occured!"
End Su
End Clas

Public Class SampleClas
Public Shared Event SampleEvent(

Public Shared Sub RaiseSampleEvent(
RaiseEvent SampleEvent(
End Su
End Clas
//
 
DareDevil said:
What I'd like to be able to do the following but it doesn't compile.
I like to know is it just a limitation of way VB.NET has been made or
is there a practical reason why it shouldn't be/isn't possible?

\\Public Class Main
Public Shared Sub Main()
SampleClass.RaiseSampleEvent()
Console.Read()
End Sub
Private Shared Sub SampleClass_SampleEvent() handles
SampleClass.SampleEvent
Console.WriteLine("SampleEvent occured!")
End Sub
End Class

Public Class SampleClass
Public Shared Event SampleEvent()

Public Shared Sub RaiseSampleEvent()
RaiseEvent SampleEvent()
End Sub
End Class
///


Handles can only handle events of Fields declared with Withevents.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
* =?Utf-8?B?RGFyZURldmls?= said:
What I'd like to be able to do the following but it doesn't compile. I
like to know is it just a limitation of way VB.NET has been made or is
there a practical reason why it shouldn't be/isn't possible?

The limitation is "by design".
\\Public Class Main
Public Shared Sub Main()
SampleClass.RaiseSampleEvent()
Console.Read()
End Sub
Private Shared Sub SampleClass_SampleEvent() handles SampleClass.SampleEvent

Place the cursor over 'Handles', then press F1 and read the
documentation. There you will read why that doesn't work.
 
Back
Top