473,591 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Can Print", 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 1494
I just discovered something new:

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

Type modelType = this.GetType();
PropertyInfo property = modelType.GetPr operty("CanPrin t");
ModelEvent(prop erty, 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("Can Print", 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*****@discus sions.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.co m>
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*****@discus sions.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.co m>
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
8373
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 I want to do is to allow the user to click on the row that corresponds to the correct address, and have the code behind populate the form's Address1, Address2 etc. controls with the relevant data items. I put the code for this into the...
3
4307
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, the events from these are not fired. In the PageLoad event I check if it's a PostBack. If it isn't, I'm calling DisplayCalendar() to render the calendar. This part works and I get the output. If it *is* a postback (i.e. one of the linkbuttons...
10
3866
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 forums, and noone wants to touch it. I tried to attach a literal control to a placeholder: <>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>
6
7126
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 (I'll keep it simple) Base Class: Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) MyBase.OnTextChanged(e) MsgBox("In the class")
28
10248
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 appropriate based on your method names . the GridView has OnRowCommand="GridView1_RowCommand" in the aspx. my problem is that the RowCommand event is firing twice (95% of the time) on the page. the other 5% it only fires once. there's no
5
4646
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 sender, EventArgs e) To fire twice Once when I comboBox2.DataSource = allMonths; And again when I execute comboBox2.DisplayMember = "Name";
4
4184
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 controls as custom server controls and don't understand why this event won't fire. I figured if the creation of the control was in the init, it would be initialized and have its event handlers set up, then after Load, the control would call its...
14
8653
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 made when upgrading to 2.0 is there any reason for this because of .net 2.0? thanks
4
2036
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 object, I keep local references to this object in each UI object, e.g. Private WithEvents _tools As RepeatTools and catch messages in an event handler like this:
0
7934
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8236
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...
0
8362
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7992
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
6639
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...
0
3850
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
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2378
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
0
1199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.