Click here to Skip to main content
15,907,687 members
Home / Discussions / C#
   

C#

 
AnswerRe: passing data from child form to parent Pin
dan!sh 12-Dec-09 18:56
professional dan!sh 12-Dec-09 18:56 
GeneralRe: passing data from child form to parent Pin
tasumisra12-Dec-09 19:38
tasumisra12-Dec-09 19:38 
GeneralRe: passing data from child form to parent Pin
dan!sh 12-Dec-09 21:15
professional dan!sh 12-Dec-09 21:15 
GeneralRe: passing data from child form to parent Pin
OriginalGriff12-Dec-09 22:20
mveOriginalGriff12-Dec-09 22:20 
GeneralRe: passing data from child form to parent Pin
dan!sh 13-Dec-09 0:05
professional dan!sh 13-Dec-09 0:05 
GeneralRe: passing data from child form to parent Pin
tasumisra13-Dec-09 2:47
tasumisra13-Dec-09 2:47 
GeneralRe: passing data from child form to parent Pin
OriginalGriff13-Dec-09 2:52
mveOriginalGriff13-Dec-09 2:52 
GeneralRe: passing data from child form to parent Pin
DaveyM6913-Dec-09 4:10
professionalDaveyM6913-Dec-09 4:10 
Well said! Using constructor overloads to pass instance data, or static objects/properties on instances to avoid using events and delegates is far too common. Unfortunately, custom events and event args is quite confusing for a noob so the quick and dirty is often adopted and even recommended Dead | X|

For anyone else that comes accross this - it's not that hard to do it properly!
public class YourClass
{
    public event EventHandler<YourEventArgs> YourEvent;

    private int yourProperty;

    public int YourProperty
    {
        get { return yourProperty; }
        set
        {
            if (yourProperty != value)
            {
                yourProperty = value;
                OnYourEvent(new YourEventArgs(yourProperty));
            }
        }
    }

    protected virtual void OnYourEvent(YourEventArgs e)
    {
        EventHandler<YourEventArgs> eh = YourEvent;
        if (eh != null)
            eh(this, e);
    }
}

public class YourEventArgs : EventArgs
{
    private int yourProperty;

    public YourEventArgs(int parameter)
    {
        this.yourProperty = parameter;
    }

    public int YourProperty
    {
        get { return yourProperty; }
    }
}
An example of useage
public class Consumer
{
    private YourClass yourClass;

    public Consumer()
    {
        yourClass = new YourClass();
        yourClass.YourEvent += new EventHandler<YourEventArgs>(yourClass_YourEvent);
        yourClass.YourProperty++;
    }

    private void yourClass_YourEvent(object sender, YourEventArgs e)
    {
        Console.WriteLine(e.YourProperty);
    }
}


Dave

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)

GeneralRe: passing data from child form to parent Pin
dan!sh 13-Dec-09 5:38
professional dan!sh 13-Dec-09 5:38 
GeneralRe: passing data from child form to parent Pin
tasumisra13-Dec-09 2:45
tasumisra13-Dec-09 2:45 
QuestionHttpWebRequest.BegineGetResponse() not running asynchronously… Pin
royk12312-Dec-09 17:53
royk12312-Dec-09 17:53 
AnswerRe: HttpWebRequest.BegineGetResponse() not running asynchronously… Pin
Luc Pattyn13-Dec-09 2:04
sitebuilderLuc Pattyn13-Dec-09 2:04 
GeneralRe: HttpWebRequest.BegineGetResponse() not running asynchronously… Pin
royk12314-Dec-09 0:37
royk12314-Dec-09 0:37 
Questionconnectionstring Pin
farokhian12-Dec-09 10:03
farokhian12-Dec-09 10:03 
QuestionException Handling Pin
Razanust12-Dec-09 7:53
Razanust12-Dec-09 7:53 
AnswerRe: Exception Handling Pin
PIEBALDconsult12-Dec-09 8:52
mvePIEBALDconsult12-Dec-09 8:52 
QuestionSchedulle a task for some days of the week Pin
baranils12-Dec-09 6:14
baranils12-Dec-09 6:14 
AnswerRe: Schedulle a task for some days of the week Pin
Abhinav S12-Dec-09 6:20
Abhinav S12-Dec-09 6:20 
AnswerRe: Schedulle a task for some days of the week Pin
Luc Pattyn12-Dec-09 6:22
sitebuilderLuc Pattyn12-Dec-09 6:22 
GeneralRe: Schedulle a task for some days of the week Pin
baranils12-Dec-09 6:35
baranils12-Dec-09 6:35 
GeneralRe: Schedulle a task for some days of the week Pin
baranils12-Dec-09 6:56
baranils12-Dec-09 6:56 
GeneralRe: Schedulle a task for some days of the week Pin
Luc Pattyn12-Dec-09 7:21
sitebuilderLuc Pattyn12-Dec-09 7:21 
GeneralRe: Schedulle a task for some days of the week Pin
baranils12-Dec-09 7:38
baranils12-Dec-09 7:38 
GeneralRe: Schedulle a task for some days of the week Pin
PIEBALDconsult12-Dec-09 8:55
mvePIEBALDconsult12-Dec-09 8:55 
GeneralRe: Schedulle a task for some days of the week Pin
baranils12-Dec-09 9:24
baranils12-Dec-09 9:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.