Solved

How does this code work?

Posted on 2008-06-23
4
245 Views
Last Modified: 2011-08-18
I've inherited this project and dont understand this section:.

1. In the aspx page, there's an event:
public event EventHandler AddressSaved;

2. User clicks Save button and there's:
OnAddressSaved(e);

3. There's a routine for OnAddressSaved(e);
protected virtual void OnAddressSaved(EventArgs e)
    {
        if (AddressSaved != null)
        {
            AddressSaved(this, e);
        }
    }

*** Now, I right click on AddressSaved(this,e) and choose "go to definition" to see where it actually saves and calls the database but it goes to the event handler.

So where is it actually saving??
0
Comment
Question by:Camillia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 18

Accepted Solution

by:
philipjonathan earned 200 total points
ID: 21846241
>>> 1. public event EventHandler AddressSaved;

It's declaring an event AddressSaved on that class.
Event allows your class to notifies a client class that something has happened. Visit this link for more info:
http://www.codeproject.com/KB/cs/events.aspx


>>> 2. OnAddressSaved(e);

It invokes the event.


>>> 3. There's a routine for OnAddressSaved(e);
protected virtual void OnAddressSaved(EventArgs e)
    {
        if (AddressSaved != null)
        {
            AddressSaved(this, e);
        }
    }


The virtual method on that class that actually invokes the event.


Now, what you need to do is to search that uses the event on this class. Normally it looks something like the code below. Let's say your class is MyClass, the actual event handler that you are looking for is some_method below.

MyClass obj1 = new MyClass(...);
obj1.AddressSaved += new EventHandler(some_method);

Open in new window

0
 
LVL 6

Assisted Solution

by:Bruce_1975
Bruce_1975 earned 150 total points
ID: 21846266
AddressSaved(this,e) raises the AddressSaved - Event.

Every registered event handler will execute the code defined in the callback function (this can be any control, even outside your project, because the event is public).

Step in with the debugger and gon on step by step. If you use VS2005 use the F11 key.

Regards,
Bruce
0
 
LVL 2

Assisted Solution

by:Alexey_Varlamov
Alexey_Varlamov earned 150 total points
ID: 21846373
This code declares the variable AddressSaved of special type - event (a special type of Delegate type).
Like any variable, the AddressSaved holds some value or has a null value. The AddressSaved != null checks if this variable is not null. The value of AddressSaved is a method (method address, method pointer, ...) - an event handler method.

Your Click event executes the OnAddressSaved(e) method. This method just redirects the execution code to the procedure, assigned to the AddressSaved variable. Yes, all magic is in the syntax: if you write the event variable name with parentheses, the .NET Framework executes the method stored in variable. But this "redirection" works only in the class that variable is defined (so, if you have a public event variable, you could not execute the assigned method outside of class, but you can assign the method to this variable from another class - that are delegates for)

So, to find the method that actually saves the data to database, please use debugger to trace the execution of  AddressSaved(this, e);  or find all assignements of AddressSaved variable within all of  the code (however, if no assigments before execution, the AddressSaved will = null). Also, it is possible to assign value at run-time through reflection!


0
 
LVL 7

Author Comment

by:Camillia
ID: 21846417
let me see. I think i found it in the code.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Trying to understand what this mean...types(lstTypes[i].AvailableDatatype) 2 38
c#, datatable, aspx 4 52
C# LINQ query question 11 33
Example code 13 43
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question