473,404 Members | 2,137 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,404 software developers and data experts.

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 2104
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.comwrote in message
news:D5**********************************@microsof t.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_ValueChanged (ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles m_Amount.ValueChanged
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_ValueChanged
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.comwrote in message
news:D5**********************************@microsof t.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.comwrote in message
news:EA**********************************@microsof t.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_ValueChanged (ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles m_Amount.ValueChanged
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_ValueChanged
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.comwrote in message
news:D5**********************************@microso ft.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
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
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
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...
0
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...
3
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...
2
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...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
3
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...
11
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.