Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 107,710 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,112 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Subscribing to events in base class

 
Reply to this topicStart new topic

Subscribing to events in base class, direct update results in null event handler

redivider
post 3 Jul, 2008 - 10:03 AM
Post #1


New D.I.C Head

*
Joined: 28 May, 2008
Posts: 10

I have two usercontrols inheriting from UCBase (UCMain, and UCStartup).


The event is defined in base using…

CODE
public delegate void StepChangeHandler(object sender, StepChangeArgs e);
public event StepChangeHandler OnStepChanged;


The event is fired when the set accessor is called (in the base) for CurrentStep…

CODE
public virtual Enums.TPStep CurrentStep
{
get { return UCControlBase._CurrentStep; }
set {_CurrentStep = value;
  _CompletedSteps |= ~value;
  OnStepChanged(this, new StepChangeArgs(value)); } //*
}


In UCMain I’m subscribing to the event in the base…
CODE
  base.OnStepChanged += new StepChangeHandler(StepChanged);


I also have an accessor to the base CurrentStep in UCMain,
CODE
public override ClassLibrary.Enums.TPStep CurrentStep
{ get { return base.CurrentStep; }
  set { base.CurrentStep = value;}
}


What I’m trying to do is have the event get caught in UCMain whenever the currentstep is updated from any other control (all of which will decend from UCBase).

From UCStartup…
If I have a reference to UCMain in my UCStartup I can call the update from UCMain using…
CODE
main.CurrentStep =Enums.TPStep.FirstStep;

I don’t think I should have to call it through UMain for the event to be defined however, in the following case the “//*” above results in OnStepChanged being null
CODE
base.CurrentStep = ClassLibrary.Enums.TPStep.FirstStep


WHY? WHY WHY?
Anybody?
User is offlineProfile CardPM

Go to the top of the page


baavgai
post 3 Jul, 2008 - 10:32 AM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,530



Thanked 41 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


I'd need to see more code to understand what you're trying to do. In the meantime, here's some basic event code that may spark an idea.

csharp

using System;

namespace ConsoleApplication1 {
class Enums {
public enum TPStep { Larry, Moe, Curley }
}

class StepChangeArgs : EventArgs {
protected Enums.TPStep currentStep;
public StepChangeArgs(Enums.TPStep currentStep) {
this.currentStep = currentStep;
}
public Enums.TPStep CurrentStep { get { return this.currentStep; } }
}


class EBase {
public delegate void StepChangeHandler(object sender, StepChangeArgs e);
public event StepChangeHandler OnStepChanged;

protected Enums.TPStep currentStep = Enums.TPStep.Larry;

public virtual Enums.TPStep CurrentStep {
get { return this.currentStep; }
set {
this.currentStep = value;
if (this.OnStepChanged!=null) {
OnStepChanged(this, new StepChangeArgs(value));
}
}
}
}

class EBaseFoo : EBase {
}

class Program {
static void Main(string[] args) {
EBaseFoo foo = new EBaseFoo();
foo.OnStepChanged+=new EBase.StepChangeHandler(foo_OnStepChanged);
foo.CurrentStep = Enums.TPStep.Curley;
foo.CurrentStep = Enums.TPStep.Moe;
Console.WriteLine("Done");
Console.ReadLine();
}

static void foo_OnStepChanged(object sender, StepChangeArgs e) {
Console.WriteLine("OnStepChanged: " + e.CurrentStep);
}
}
}

User is online!Profile CardPM

Go to the top of the page

redivider
post 3 Jul, 2008 - 11:52 AM
Post #3


New D.I.C Head

*
Joined: 28 May, 2008
Posts: 10

My question is, then, why is this.OnStepChanged ==null when updating the base.CurrentStep directly from UCStartup, but its valid if i send my request through UCMain (which subscribes to the event, and updates base.CurrentStep itsself).

Does the update request have to come from the object that subscribes to the service, in order for the it to catch the event?.......

...Wait... I think i figured it out. I have two base instances, so of course OnStepChanged would not be defined for the other instance.

Is it possible to make OnStepChanged be static. Wrap an accessor around it so i can access the same value from all instances??
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/30/08 03:11AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month