Events and Delegates

N

Nicholas Beenham

Okie next problem:

I have 2 classes one of which fires an event to the other

public delegate void class1eventHandler (object sender, EventArgs e);

public class Class1
{
public event class1eventHandler class1Event;

protected virtual void OnChanged(EventArgs e)
{
if (class1event != null)
class1event(this, e);
}
public void onChange()
{
OnChanged(EventArgs.Empty);
}
}
public class Class2
{
public void addHandler()
{
class2Instance.class1event += new class1EventHandler(targetMethod);
}
public void targetEvent {}

My problem is that the class2Instance does not recognise the
class1event....any thoughts??

Thanks nick
 
N

Nicholas Beenham

Yes they are both in the same namespace.
Here is all my code
Class1
private static ConveyorBelt theBelt = new ConveyorBelt();

private ConveyorBelt()
{
this.Subscribe();

}

public static ConveyorBelt getTheBelt()
{
return theBelt;
}

public void Subscribe()
{
/**this bit is buggin me**/ += new
BottleArrivedEventHandler(bottlesInPlaceEvent);
}

Class2
public delegate void BottleArrivedEventHandler(object sender, EventArgs e);

public class BottleProximitySensor
{

public event BottleArrivedEventHandler BottleArrived;

public BottleProximitySensor()
{

}

protected virtual void OnChanged(EventArgs e)
{
if (BottleArrived != null)
BottleArrived(this, e);
}

public void onArrival()
{
OnChanged(EventArgs.Empty);
}
}
}

Anyone got ideas?

Thanks
Nick
 
N

Nicholas Beenham

Aha cracked that bit,

One issue left, im using singletons so ...
I'm getting an An object reference is required for the nonstatic field,
method, or property 'BottlingPlant.ConveyorBelt.proxSensor'

for this segment of code
private BottleVolumeSensor theVolSensor = new BottleVolumeSensor();

private static WaterTap theTap = new WaterTap(theVolSensor);

Any ideas??
 
J

Jon Skeet [C# MVP]

Nicholas Beenham said:
Aha cracked that bit,

One issue left, im using singletons so ...
I'm getting an An object reference is required for the nonstatic field,
method, or property 'BottlingPlant.ConveyorBelt.proxSensor'

for this segment of code
private BottleVolumeSensor theVolSensor = new BottleVolumeSensor();

private static WaterTap theTap = new WaterTap(theVolSensor);


I don't see what relation the section of code you posted has to the
error message, to be honest - but I suspect that proxSensor is an
instance field, and you're trying to use it as if it were static.
 
A

Adrian Vinca [MSFT]

Hi,

I think you should declare the event this way:

public class Class1
{
public delegate void Class1EventHandler(object sender, EventArgs e);

public event Class1EventHandler OnClass1Event;

public void SendEvent()
{
this.OnClass1Event(this, new EventArgs() );
}

}
public class Class2
{
public void addHandler()
{
class2Instance.OnClass1Event += new
Class1.Class1EventHandler(OnEventReceived);
}
public void OnEventReceived(object sender, EventArgs e)
{
MessageBox.Show("I've just received an event !");
}
}

If you need to pass some extra parameters, you should create your own class
inhereted from EventArgs.

I have 2 classes one of which fires an event to the other

public delegate void class1eventHandler (object sender, EventArgs e);

public class Class1
{
public event class1eventHandler class1Event;

protected virtual void OnChanged(EventArgs e)
{
if (class1event != null)
class1event(this, e);
}
public void onChange()
{
OnChanged(EventArgs.Empty);
}
}
public class Class2
{
public void addHandler()
{
class2Instance.class1event += new class1EventHandler(targetMethod);
}
public void targetEvent {}

My problem is that the class2Instance does not recognise the
class1event....any thoughts??

Thanks nick

Adrian Vinca [MSFT], Developer Division
--------------------------------------------------------------------
This reply is provided "AS IS", without warranty (express or implied).

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top