473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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?
Nov 20 '05 #1
8 2283
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" <an*******@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
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?
On a similar note why can't I specify a declarative event handler on a shared or module event?

Nov 20 '05 #2
* =?Utf-8?B?RGFyZURldmls?= <an*******@discussions.microsoft.com> scripsit:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
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!
Nov 20 '05 #4
Dude, thats all Handles actually does...

calls addHandler

word.

"DareDevil" <an*******@discussions.microsoft.com> wrote in message
news:DD**********************************@microsof t.com...
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!
Nov 20 '05 #5
"DareDevil" <an*******@discussions.microsoft.com> schrieb
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

Nov 20 '05 #6
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
//
Nov 20 '05 #7
"DareDevil" <an*******@discussions.microsoft.com> schrieb
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

Nov 20 '05 #8
* =?Utf-8?B?RGFyZURldmls?= <an*******@discussions.microsoft.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: perplexed | last post by:
Is there a way to combine the values of multiple form items such as two textboxes and one radio button for insertion into one MYSQL database field?
5
by: ND | last post by:
I need to create a separate field from 4 fields, "street address", "city", "State" and "zip code". For example, Street address - 100 Forest Street City - Seattle State - WA Zip - 05555 ...
19
by: davegb | last post by:
Have been trying, in my spare time, to create an Access db for the employees in my group for over a month. It surprised me when I read in the Access Bible that a relationship between the Primary...
1
by: bill yeager | last post by:
I have a datagrid control within a datalist control. When I try and do a "Find" on the control, the object comes back with nothing and then my pgm crashes. I am 100% sure that my datagird inside...
4
by: CaptRR | last post by:
I think this is the right group to post to, so here goes. My problem is this, I cannot update the datarow to save my life. Been on this for 2 days now, and still am no closer to figuring it out...
1
by: dx | last post by:
I'm extremely frustrated with ASP.NET...again! To me this should be as simple as setting oCheckBox.Checked = True.. yet for some reason it isn't. I have a user control (ascx) that that has a...
3
by: Lyners | last post by:
I am having a hard time with this one, and I thought it would be easy. I have a datagrid in which I have textboxs for users to enter data. One of the fields in the database behind the datagrid...
1
by: Raja | last post by:
Hi Everybody Just playing with ObjectDataSource and noticed the following. I have a Gridview which binds to a ObjectDataSource. ObjectDataSource gets data from a typed dataset created with VWD. In...
4
by: cody | last post by:
It is possible to declare and use/instantiate a class with a uninitialized readonly field without even a compiler warning. Why don't I get warnings? public class Stuff { public readonly int a;...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.