467,077 Members | 1,033 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

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
  • viewed: 1890
Share:
3 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tom | last post: by
2 posts views Thread by Christoph Boget | last post: by
5 posts views Thread by Waleed AlRashoud | last post: by
reply views Thread by Mike | last post: by
14 posts views Thread by v4vijayakumar | last post: by
11 posts views Thread by Yarco | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.