473,783 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to raise an event on a private member (VB.NET)

I constructed a new Class with some private members.
I would like an event to be raised on the moment the value of one of those
private members is changed.
How do I define an event for that private member and how do I raise the event?

--
RW
Jul 26 '07 #1
3 2136
If I understand correctly, you want to publicly raise an event when a value
of a private property changes.

You can declare the the event publicly, then in the set statement, raise
that event --- then handle the publicly raised event.

Take a look at the RaiseEvent, AddHandler and possibly RemoveHandler
methods:
RaiseEvent http://msdn2.microsoft.com/en-us/lib...ed(vs.80).aspx
AddHandler http://msdn2.microsoft.com/en-us/lib...ka(VS.80).aspx
RemoveHandler http://msdn2.microsoft.com/en-us/lib...ac(VS.80).aspx

If I've misunderstood what you're looking for, let us know.
"RW" <RW@discussions .microsoft.comw rote in message
news:D5******** *************** ***********@mic rosoft.com...
>I constructed a new Class with some private members.
I would like an event to be raised on the moment the value of one of those
private members is changed.
How do I define an event for that private member and how do I raise the
event?

--
RW

Jul 28 '07 #2
Thanks Matt,

What I actualy want to happen is that when the value of a private member of
a class in changed (e.g. via it's property) it automaticaly raises an event.
The event is not explicitly risen through the RaiseEvent command.

Private m_Amount As Long

Public Property Amount() As Long
Get
Return m_Amount
End Get
Set(ByVal Value As Long)
m_Amount = Value
End Set
End Property

Public Sub m_Amount_ValueC hanged (ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles m_Amount.ValueC hanged
Try
...
Catch ex As Exception
...
Finally
...
End Try
End Sub
When in the code somewhere is executed e.g.:

Amount += 1

increasing the value of m_Amount with 1, the sub m_Amount_ValueC hanged
should be executed automaticaly

--
RW
"Matt F" wrote:
If I understand correctly, you want to publicly raise an event when a value
of a private property changes.

You can declare the the event publicly, then in the set statement, raise
that event --- then handle the publicly raised event.

Take a look at the RaiseEvent, AddHandler and possibly RemoveHandler
methods:
RaiseEvent http://msdn2.microsoft.com/en-us/lib...ed(vs.80).aspx
AddHandler http://msdn2.microsoft.com/en-us/lib...ka(VS.80).aspx
RemoveHandler http://msdn2.microsoft.com/en-us/lib...ac(VS.80).aspx

If I've misunderstood what you're looking for, let us know.
"RW" <RW@discussions .microsoft.comw rote in message
news:D5******** *************** ***********@mic rosoft.com...
I constructed a new Class with some private members.
I would like an event to be raised on the moment the value of one of those
private members is changed.
How do I define an event for that private member and how do I raise the
event?

--
RW


Jul 31 '07 #3
What I'm suggestiong is kind of a hack-ish work around type thing ----
always something to avoid, but the only way I can think of to handle what
you want. That doesn't mean there isn't something else out there, just
throwing in my two cents here.

Essentially, I'm saying use the code you have below, but (and this is the
bad part) you have to remember to never set the value of m_amount directly
in the class, but rather always set it through Amount. Then in your set,
call RaiseEvent. One way I can think of to enforce not setting m_amount
directly is to declare it as a different type and then convert in the
property set/get --- this is all pretty hack-ish. Another, much cleaner
option is to create a small class that contains a property of Amount,
declare the class as private, raise the event in the sub-class, handle the
class declared in and pass it on up the chain.

"RW" <RW@discussions .microsoft.comw rote in message
news:EA******** *************** ***********@mic rosoft.com...
Thanks Matt,

What I actualy want to happen is that when the value of a private member
of
a class in changed (e.g. via it's property) it automaticaly raises an
event.
The event is not explicitly risen through the RaiseEvent command.

Private m_Amount As Long

Public Property Amount() As Long
Get
Return m_Amount
End Get
Set(ByVal Value As Long)
m_Amount = Value
End Set
End Property

Public Sub m_Amount_ValueC hanged (ByVal sender As System.Object, ByVal e
As
System.EventArg s) Handles m_Amount.ValueC hanged
Try
...
Catch ex As Exception
...
Finally
...
End Try
End Sub
When in the code somewhere is executed e.g.:

Amount += 1

increasing the value of m_Amount with 1, the sub m_Amount_ValueC hanged
should be executed automaticaly

--
RW
"Matt F" wrote:
>If I understand correctly, you want to publicly raise an event when a
value
of a private property changes.

You can declare the the event publicly, then in the set statement, raise
that event --- then handle the publicly raised event.

Take a look at the RaiseEvent, AddHandler and possibly RemoveHandler
methods:
RaiseEvent http://msdn2.microsoft.com/en-us/lib...ed(vs.80).aspx
AddHandler http://msdn2.microsoft.com/en-us/lib...ka(VS.80).aspx
RemoveHandle r
http://msdn2.microsoft.com/en-us/lib...ac(VS.80).aspx

If I've misunderstood what you're looking for, let us know.
"RW" <RW@discussions .microsoft.comw rote in message
news:D5******* *************** ************@mi crosoft.com...
>I constructed a new Class with some private members.
I would like an event to be raised on the moment the value of one of
those
private members is changed.
How do I define an event for that private member and how do I raise the
event?

--
RW



Jul 31 '07 #4

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

Similar topics

4
5743
by: Tom | last post by:
Let's say I've got a class defined in a header file. class foo { private: ... int bar; .... }; Now lets say I have a function that needs to access the private variable
2
1829
by: Christoph Boget | last post by:
Let's take the following class: class MyClass { private int privateVar; public int PublicVar { get { return privateVar; } } public MyClass() {}
5
3223
by: Waleed AlRashoud | last post by:
Hi, I hope u can help me I asked this question before but I didn't explain it very well. Let say I hvae a thread 'A', creates object 'O', and start thread 'B' to do some work on 'O', OK? I want thread 'B' to fire event of 'O' BUT MUST THAT EVENT DONE BY THREAD 'A' not 'B'!!
0
954
by: Mike | last post by:
I have a asp.net (vb.net) web app that calls a VB.NET class. This class has a raise Event in it. - raised event getStuff I can't call the raised Event getStuff. Can i not call or execute a raised event or an event from a asp.net application? this class works because i use it from a vb.net winform application. thx
3
2120
by: moondaddy | last post by:
I wrote my own List class which I use to bind to list controls. this class inherits CollectionBase and implements IBindingList. This class contains a list of business classes such as customers for example. In the customer class I have an event called MyTestEvent and when certain conditions arise, I want to raise this event which would cause an event to fire in the List class. How can this be done when I don't actually declare the...
2
1941
by: tony | last post by:
Hello !! I have a question about event. This small program below is taken from the internet and I wonder if there are any advantage to use this Onxxx in this case it's OnEventSeven instead of doing in the following way. 1. In method GenNumbers() you remove this statement "OnEventSeven(new DivBySevenEventArgs(i));" with these
14
4212
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
3
1635
by: Rastra | last post by:
Hi I have been trying to raise an event in the user control. The problem is I always get null reference in the event and my event never gets fired. Can someone help me? The Code I have written is as follows: // Decelerating the delegate and event public delegate void OnButtonClick(string strValue); public event OnButtonClick OtherMonth; protected void Page_Load(object sender, EventArgs e) {
11
6447
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10081
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.