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

Firing an event with a class property

I want to fire an event within the property of a class. Here's an example:

private bool _canprint;
public bool CanPrint
{
get
{
return _canprint;
}
set
{
_canprint = value;

try
{
if (ModelEvent != null)
{
//ModelEvent(this, new EventArgs());
//ModelEvent("CanPrint", new EventArgs());
}
}
catch
{
// Handle exceptions
}
}
}
Originally I used the "this" keyword, as I had seen elsewhere. But this
just passes the instantiation of the class and not the property itself. The
problem is that I don't know precisely which Property is firing the event.
So I thought about just changing it to a string but that seems less than
elegant.

Hoping someone can suggest how I can pass a reference to the property itself.

Robert W.
MWTech
Vancouver, BC
Nov 17 '05 #1
3 1483
I just discovered something new:

By declaring "using System.Reflection;" I could then fire the event this way:

Type modelType = this.GetType();
PropertyInfo property = modelType.GetProperty("CanPrint");
ModelEvent(property, new EventArgs());
But it seems weird to me to be using System Reflection WITHIN the class
itself.

Hoping someone can either confirm I'm taking the right approach or show me a
better way.

Robert

"Robert W." wrote:
I want to fire an event within the property of a class. Here's an example:

private bool _canprint;
public bool CanPrint
{
get
{
return _canprint;
}
set
{
_canprint = value;

try
{
if (ModelEvent != null)
{
//ModelEvent(this, new EventArgs());
//ModelEvent("CanPrint", new EventArgs());
}
}
catch
{
// Handle exceptions
}
}
}
Originally I used the "this" keyword, as I had seen elsewhere. But this
just passes the instantiation of the class and not the property itself. The
problem is that I don't know precisely which Property is firing the event.
So I thought about just changing it to a string but that seems less than
elegant.

Hoping someone can suggest how I can pass a reference to the property itself.

Robert W.
MWTech
Vancouver, BC

Nov 17 '05 #2
Robert W. <Ro*****@discussions.microsoft.com> wrote:
I want to fire an event within the property of a class. Here's an example:
<snip>
Hoping someone can suggest how I can pass a reference to the property
itself.


What do you mean by "the property itself"? The property is just a pair
of methods, effectively. If you want the *value* of the property, just
use it as you would anywhere else. What are you really trying to pass
to ModelEvent?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
Jon,

Thank you for replying. I'm sorry that I didn't explain mysef clearly. I'm
still learning; learning quickly, but still learning!

To answer your question, it was actually a reference to the Property itself
that I was looking for - ie. the entire structure including:
- The Property's type
- The GetValue method
- The SetValue method

As these things often go, I eventually found a simple way to do this. I
just unboxed the "sender" object as follows:

PropertyInfo propInfo = (PropertyInfo) sender;

This provided me exactly the information I needed. With the propInfo
variable I could then access every aspect of the Property.

But thank you again,

Robert W.
MWTech
Vancouver, BC


"Jon Skeet [C# MVP]" wrote:
Robert W. <Ro*****@discussions.microsoft.com> wrote:
I want to fire an event within the property of a class. Here's an example:


<snip>
Hoping someone can suggest how I can pass a reference to the property
itself.


What do you mean by "the property itself"? The property is just a pair
of methods, effectively. If you want the *value* of the property, just
use it as you would anywhere else. What are you really trying to pass
to ModelEvent?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #4

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

Similar topics

1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
3
by: JoakimR | last post by:
Hello, I have a problem with events not firing. I've created a web user control which renders a calendar using a table control. In two cells I'm adding LinkButtons for "prev/next month". However,...
10
by: Wylbur via DotNetMonster.com | last post by:
Hello to all of you geniuses, I'm having a problem trying to get an Init handler to fire for a Placeholder control at the initialization phase. I’ve posted this problem to 3 other ASP.NET...
6
by: crk2 | last post by:
Here a simple one. (At least I think it is?) and any help would be truly appreciated. I have an inherited textbox on my form based on a custom texbox control. It looks something like this ...
28
by: Tim_Mac | last post by:
hi, i'm new to .net 2.0, and am just starting to get to grips with the gridview. my page has autoEventWireUp set to true, which i gather is supposed to figure out which handlers to invoke when...
5
by: Joe | last post by:
Hi I am adding a class to a ComboBox - and all is fine except that I fill the combobox on the Form_Load Method and it causes the method private void comboBox2_SelectedIndexChanged(object...
4
by: TS | last post by:
I am creating a User control and i create some dynamic controls in the init handler. one of the controls is a custom validator which i assign a serverValidate event handler. I usally always do my...
14
by: TS | last post by:
I have this custom data list control and i override the onItemDatabound event. After upgrading to vs 2005, this event is not always getting called, though it does at other times. No changes were...
4
by: Joergen Bech | last post by:
I sometimes use delegates for broadcasting "StateChanged" events, i.e. if I have multiple forms and/or controls that need updating at the same time as the result of a change in a global/common...
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
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
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
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...

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.