Implement VB.NET interface via C# - Problem?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've built a simple Interface in VB.NET - other VB.NET projects can reference
and use this interface just fine:

Public Interface IC4IPlugin

Enum TerminateReason
IC4IClosing
WindowsSessionEnding
Other
End Enum

ReadOnly Property PluginName() As String

Function Initialize(ByVal host As IC4IHost, ByVal startForm As
System.Windows.Forms.Form) As Boolean
Function Terminate(ByVal reason As TerminateReason) As Boolean
Function SetUserCredentials() As Boolean

Event PluginClosing(ByVal sender As Object, ByVal e As EventArgs)
Event PluginException(ByVal sender As Object, ByVal e As EventArgs)
Event PluginLogRequest(ByVal sender As Object, ByVal e As EventArgs)
Event PluginClassificationChange(ByVal sender As Object, ByVal e As
EventArgs)

End Interface

So far so good... until you try to implement this interface via C# and run
into this error:

The type or namespace name 'PluginClassificationChangeEventHandler' could
not be found (are you missing a using directive or an assembly reference?)

I get one of those errors for each of the events defined in the interface.

Obviously I have checked that 1) The .DLL containing the interface is
referenced in the project.

What am I missing here? Is there some kind of incompatibility between the
languages that I'm not aware of? I realize that C# (insofar as the code is
concerned) handles events differently - is this a part of the situation?

If any of you have any ideas on what might be the issue - that would be
appreciated.

Thanks,

Chris
 
Chris,
Try:
Public Interface IC4IPlugin

Enum TerminateReason
IC4IClosing
WindowsSessionEnding
Other
End Enum

ReadOnly Property PluginName() As String

Function Initialize(ByVal host As IC4IHost, ByVal startForm As
System.Windows.Forms.Form) As Boolean
Function Terminate(ByVal reason As TerminateReason) As Boolean
Function SetUserCredentials() As Boolean

Event PluginClosing As EventHandler
Event PluginException As EventHandler
Event PluginLogRequest As EventHandler
Event PluginClassificationChange As EventHandler

End Interface

When you use:
Event PluginClassificationChange (ByVal sender As Object, ByVal e As
EventArgs)

To define an event, VB.NET automatically creates a nested Delegate
(PluginClassificationChangeEventHandler) which is used to define the Event.

Seeing as you are defining this events as "Standard" events, you can simply
use "As EventHandler" to prevent VB.NET from creating the nested Delegates.
Event PluginClassificationChange As EventHandler


Also, its generally better (smaller assemblies, fewer types) if you use the
"As EventHandler" syntax when you define you events, as this prevents VB.NET
from creating individual Delegate types for each event. You can define your
own Delegate & EventArgs if you need to, allowing your events to share a
single "EventHandler" class.

Something like:

Public Class IC4IEventArgs
Inherits EventArgs

' parameters (properties) unique to IC4I events

End Class

Public Delegate Sub IC4IEventHandler(ByVal sender As Object, ByVal e As
IC4IEventArgs)
Event PluginClosing As IC4IEventHandler

Hope this helps
Jay
 
Hello Jay,

Good information - thank you! I knew there were a few things going on behind
the scenes but had not understood exactly *what* was happening with my
definitions. I'll give this a go in the morning - thanks again!

Chris
 
Well I'm still fighting with this thing and could use some input... It seems
that the Interface thing works better if I build the Interface in C# and use
a file reference.

My question now is how do I implement this interface in C#? I'll be the
first to admit that I primarily code in VB.NET - therefore a lot of the stuff
(as Jay has already mentioned) happens behind the scenes for me.

Here is the Interface:

public class IC4IDefinition
{
public delegate void PluginClosingEventHandler(object sender,
PluginClosingEventArgs e);
public delegate void PluginLogRequestEventHandler(object sender,
PluginLogRequestEventArgs e);
public delegate void PluginExceptionEventHandler(object sender,
PluginExceptionEventArgs e);
public delegate void PluginClassificationChangedEventHandler(object sender,
PluginClassificationChangedEventArgs e);

public enum TerminateReason {IC4IClosing, WindowsSessionEnding, Other}

public interface IC4IPlugin
{
string PluginName
{
get;
}
bool Initialize(IC4IHost host, System.Windows.Forms.Form startForm);
bool Terminate(TerminateReason reason);
bool SetUserCredentials();

event PluginClosingEventHandler PluginClosing;
event PluginLogRequestEventHandler PluginLogRequest;
event PluginExceptionEventHandler PluginException;
event PluginClassificationChangedEventHandler PluginClassificationChanged;
}
}

What I'm looking for is *how* do I implement the Interface in C# -
specifically how do I raise the events? I used Visual Studio to build (via
the implement interface command) out the class that uses the interface, but
I'm not able to raise the events.

Ideas? Again, C# is not my 'native' language so any code samples or
tutorials that might prove useful would be greatly appreciated. Thanks!

Chris
 
Thank you for the resources Jay - however I'm not making any progress on
this. I think I must have something wrong with the interface or somewhere
else that I am not seeing that is creating this issue for me.

For example, I'm trying to invoke one of the events on the interface via the
following code:

PluginLogRequest(this,new EPG.AC.Interfaces.PluginLogRequestEventArgs());

This does not appear to be correct because the hosting control never
receives an event notification from this code.

I hope that makes sense... I'm hitting the books on C# - but if you have
additional input or could show me where I've incorrectly defined the
interface I would great appreciate it. Thanks!

Chris
 
Please disregard my previous post as my colleague was able to point out what
happened...

Somehow during all my fiddling around with the interface, I had lost the
event wireups for the UI elements of the form - therefore those events that
were setup to fire the Interface events were not being called.

Problem solved.

I would also like to say THANKS! to Jay (again) for the previous posts -
yours are always informative and I thank you for your valuable time.

Chris
 
Back
Top