0

I have a solution with 2 projects inside. Project A is Vb.net and Project B is C#

I need to receive an event on Project A when certain conditions are met in Project B.

I made all the imports and project B is visible and usable from Project A.

As far as I arrived, I declare the event in Project B:

public EventHandler MatchFound;

And I think the event can be fired inside Project B with:

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

The question is, how to receive this event in vb.net Project A?

PeterPam
  • 337
  • 6
  • 20
  • So project A is referencing project B? Then all public classes in project B are accessible and usable in project A also. So you can use the classes the same way as if those were part of the project A, so I'm sure what is the problem you are facing? – Esko Apr 30 '19 at 12:04
  • The code you shared is vb.net, but you say it's in a c# project.... your question is unclear at best. events are a part of the framework and as such, it doesn't matter if the code that raise the event is written in C# or vb.Net, you handle it the same as any other event, by adding an event handler. I don't remember the exact syntax for vb.Net, though. – Zohar Peled Apr 30 '19 at 12:07
  • Time for an [MCVE]. But a simple one 2 simple project with the event firing annd the event handler to help use understand what the big picure exactly is. – Drag and Drop Apr 30 '19 at 12:09
  • @ZoharPeled, sorry. i have updated the code in my question. My problem is how to declare the event in VB Project A in order to receive it when it is raised on Project B – PeterPam Apr 30 '19 at 12:24
  • @Esko: i need how to declare the event in Project A – PeterPam Apr 30 '19 at 12:25
  • You would subscribe to the event in the same way as any other event, really. The fact they are written in different languages is irrelevant (due to the magic of .NET). **Specifically** what trouble are you having? What have you tried? Maybe you don't understand how to reference the class from one project to the other? – ADyson Apr 30 '19 at 12:45
  • @ADyson, yes, i don't understand how to reference the class for the event. To work with other items in the Project B i have no problems... – PeterPam Apr 30 '19 at 12:49
  • have you added an `Imports` statement to add the class's namespace in your VB code? Have you declared an instance of the class? – ADyson Apr 30 '19 at 13:03
  • P.S. I'd expect the declaration `public EventHandler MatchFound;` in the C# to be `public event EventHandler MatchFound;` . Maybe that's the issue? – ADyson Apr 30 '19 at 13:05
  • Yes, both done.The instance declared with WithEvents instanceX. But when i'm stucked at "Private sub blah() handles instanceX.¿? – PeterPam Apr 30 '19 at 13:06
  • @ADyson. SOLVED. problem was "event" word missing in declaration. how to set your response as good? – PeterPam Apr 30 '19 at 13:07
  • I'll write it as a proper answer, hang on – ADyson Apr 30 '19 at 13:07

1 Answers1

1

The problem description was a little unclear, but based on the comments it seems you're unable to properly link the event to the handler function in the VB project. It sounds like you simply can't find it in Intellisense, or get a compiler error if you try to reference it.

The reason for this is that you haven't declared the handler property as an event in the C# project:

public EventHandler MatchFound;

should be changed to

public event EventHandler MatchFound;
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • if project B uses another class named classBB included in the Project B, can i set a event in classBB and use it from the VB.NET project A? – PeterPam Apr 30 '19 at 13:59
  • Yes you can, so long as that class, and its event, are both `public`. Just the same as this one, by the sounds of it. – ADyson Apr 30 '19 at 14:11
  • but classBB is already declared in Proyect B. i've created a new event in project B but this event can't be called from the classBB: "The name ... does not exist in the current context) – PeterPam Apr 30 '19 at 14:34
  • Sorry, the exact scenario is not very clear. I thought you just wanted to add another event to classBB. Now you're saying "classBB is already declared"...it's unclear how that's relevant - if you're going to add an event to it I would expect that it was already declared. I also can't work out how that relates to the error message you've reported. But since I can't see what code you wrote, or where, I can't really tell you how to fix it. A description of code is always, by definition, less detailed and specific than the code itself, and therefore it's hard to provide a specific fix. – ADyson Apr 30 '19 at 14:40
  • You know what, it would be much simpler if you just ask a new question showing all the relevant bits of code (the classBB, the event declaration, and the code where you try to use the event), as well as the error message in full. Then we might have a chance of sorting it out. If you comment here with a link to your new question I'll take a look. Thanks – ADyson Apr 30 '19 at 14:41
  • I have posted a new question with more about this: https://stackoverflow.com/questions/55924755/how-to-raise-event-from-class-to-another-class-in-c-sharp – PeterPam Apr 30 '19 at 15:58