1

I have a solution with a VB.NET ProjectA Vb.net, and a C# ProjectB.

ProjectA has Form1.vb ProjectB has Form1.cs and ClassBB.cs

Start project is ProjectA and Form1.vb starts with this declaration:

Public WithEvents PB As New ProjectA.Form1

And with this, i take the event:

Private Sub arrived() Handles PB.MatchFound
' stuff to do
end sub

In Form1.cs:

public event EventHandler MatchFound;
protected void OnMatchFound()
 {
   if (MatchFound!= null)
       MatchFound(this, EventArgs.Empty);
  }

I can fire this event from any place in Form1.cs in ProjectB. Form1.cs also has in order to call some functions in ClassBB:

ClassBB ClassBB;
MyClassBB = new ClassBB();

I need to fire OnMatchEvent from ClassBB in order to receive the event in Form1 in ProjectA.

If i raise the event from Form1.cs from a function in ProjectB all work ok. But i don't know how to fire the event from a function inside ClassBB.

How to do this?

David Wilson
  • 4,369
  • 3
  • 18
  • 31
PeterPam
  • 337
  • 6
  • 20
  • https://stackoverflow.com/questions/4378339/raise-an-event-of-a-class-from-a-different-class-in-c-sharp – ADyson Apr 30 '19 at 16:03
  • You can't do this, as the answer above mentions. But I don't see why you need to either. If Form1 is calling some code in classBB, then it can just fire the event when that code in classBB is finished. If you need some values from classBB in order to populate the event, then you should ensure that the method in classBB either returns those values as the return data of the event, or populates some public property in classBB from where the form code can fetch them. – ADyson Apr 30 '19 at 16:06
  • Another approach would be to make classBB declare its own event. At the right moment, classBB would raise its event. Form1 would listen to that event, and when it receives it, it would raise its own event in turn. So then you have a chain of events which fire each other, and eventually the message gets to where it needs to go. – ADyson Apr 30 '19 at 16:08
  • what exactly makes it necessary to fire the event from a remote class? which class finds the match? What kind of information does the event transport? – Mong Zhu Apr 30 '19 at 16:15
  • @ADyson, there is not a function to call. In classBB there is a timer working and once some condition is true, i need to fire the event ni order to Form1 in ProjectA knows the condition is true – PeterPam Apr 30 '19 at 17:24
  • @ADyson, how to Form1 listen to an event in ClassBB? That's what a solution. – PeterPam Apr 30 '19 at 17:25
  • Same way as you listen to events in your other earlier question – ADyson Apr 30 '19 at 17:53
  • Doesnt work. i get null here if (MatchFound!= null) MatchFound(this, EventArgs.Empty); – PeterPam Apr 30 '19 at 18:00
  • 1
    You really need to create a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) because it's difficult to tell what you are trying to accomplish. Which form is raising the event and which form is subscribing to it? Your question is not very clear, especially because you use the same class names in both projects. It's confusing. – Chris Dunaway Apr 30 '19 at 18:37
  • @PeterPam can you show your attempt to implement the second event (listening to the event from classBB)? Then we have a better chance of fixing it – ADyson Apr 30 '19 at 20:47
  • i've created a new post as i saw the basic problem is another https://stackoverflow.com/questions/55932595/how-to-fire-correctly-an-event-in-c-sharp – PeterPam May 01 '19 at 06:52

0 Answers0