Interface class with event

S

SteveR

Based on Tom Shelton's recommendation to use an interface class to manage
multiple .dll's, I started to write one but got stuck when it came to the
event. I have the following event in my .dll's:

public event EventHandler<PlayStateArgs> PlayStateEvent;


public class PlayStateArgs : EventArgs

{

private readonly string _strData;

public PlayStateArgs(string strData)

{

_strData = strData;

}

public string strData { get { return _strData; }}

}


I can create the event in the interface like:

public interface IVideo
{
event EventHandler PlayStateEvent;
}

But I don't know what to do about the <PlayStateArgs> part... How do you
write this?
 
W

Willem van Rumpt

Based on Tom Shelton's recommendation to use an interface class to manage
multiple .dll's, I started to write one but got stuck when it came to the
event. I have the following event in my .dll's:

public event EventHandler<PlayStateArgs> PlayStateEvent;


public class PlayStateArgs : EventArgs

{

private readonly string _strData;

public PlayStateArgs(string strData)

{

_strData = strData;

}

public string strData { get { return _strData; }}

}


I can create the event in the interface like:

public interface IVideo
{
event EventHandler PlayStateEvent;
}

But I don't know what to do about the<PlayStateArgs> part... How do you
write this?

Exactly as you would expect:

public interface IVideo
{
event EventHandler<PlayStateArgs> PlayStateEvent;
}
 
S

Steve Ricketts

I tried that first: event EventHandler<PlayStateArgs> PlayStateEvent; but
it says PlayStateArgs could not be found. I tried adding "class
PlayStateArgs : EventArgs" but couldn't get the syntax right. What would be
the syntax for specifying the PlayStateArgs?

Thanks!
 
P

Peter Duniho

Steve said:
I tried that first: event EventHandler<PlayStateArgs> PlayStateEvent;
but it says PlayStateArgs could not be found. I tried adding "class
PlayStateArgs : EventArgs" but couldn't get the syntax right. What
would be the syntax for specifying the PlayStateArgs?

The PlayStateArgs type needs to be visible to the assembly declaring the
interface itself. Either declare it in that interface (which is
probably the best approach, since presumably the EventArgs type is
closely tied to the interface type in which the event must be
implemented), or have that assembly reference the assembly where the
PlayStateArgs is defined.

Pete
 
W

Willem van Rumpt

I tried that first: event EventHandler<PlayStateArgs> PlayStateEvent;
but it says PlayStateArgs could not be found. I tried adding "class
PlayStateArgs : EventArgs" but couldn't get the syntax right. What would
be the syntax for specifying the PlayStateArgs?

Thanks!

Assuming the PlayStateArgs class is in the same assembly, you'll have to
include a "using" directive in the unit, or fully qualify the classname,
so either a

using <Your.Namespace>

or a

event EventHandler<Your.Namespace.PlayStateArgs> PlayStateEvent

is needed.

If it isn't in the same assembly, you need to reference the containing
assembly first, then proceed as described above.
 
S

SteveR

Feel like I'm taking baby steps here...

Ok, I was able to define the interface and args like:

namespace clsVideo
{
public interface IVideo
{
event EventHandler<PlayStateArgs> PlayStateEvent;
}

public class PlayStateArgs : EventArgs
{
public string strData;
}

}

Then in the class that's using the interface I have:

using clsVideo;
namespace VideoVLC

{

public class clsVideoVLC : clsVideo.IVideo

{
...
But now I'm getting two errors:
`VideoVLC.clsVideoVLC' does not implement interface member
`clsVideo.IVideo.PlayStateEvent.add' (CS0535)

`VideoVLC.clsVideoVLC' does not implement interface member
`clsVideo.IVideo.PlayStateEvent.remove' (CS0535)

Have I done something wrong... or not done something I should have?

sr
 
W

Willem van Rumpt

But now I'm getting two errors:
`VideoVLC.clsVideoVLC' does not implement interface member
`clsVideo.IVideo.PlayStateEvent.add' (CS0535)

`VideoVLC.clsVideoVLC' does not implement interface member
`clsVideo.IVideo.PlayStateEvent.remove' (CS0535)

Have I done something wrong... or not done something I should have?

sr

Without seeing the actual source code it's impossible to know.

Just as a quick test, you can select "implement interface" from the
context menu when the caret is at the "clsVideo.IVideo" of the class
declaration. If after that, the compiler still complains, some actual
source code would be nice so we can properly analyse what's going on.
 
S

sloan

Make a clean break from old naming conventions.

//
namespace clsVideo
{
//

That is uber confusing. You're using a VB6 hungarian notation for a
cls....for a c# namespace.... (???)
Break with the old....start today would be my advice.



http://msdn.microsoft.com/en-us/library/893ke618(VS.71).aspx


The general rule for naming namespaces is to use the company name followed
by the technology name and optionally the feature and design as follows.

CompanyName.TechnologyName[.Feature][.Design]
 
S

Steve Ricketts

Point well taken. These were a VB6 conversion and since I'm the only one
writing code, I never gave it much thought. Your suggestion is a good one
and I'll start paying more attention to the namespaces.

sr

sloan said:
Make a clean break from old naming conventions.

//
namespace clsVideo
{
//

That is uber confusing. You're using a VB6 hungarian notation for a
cls....for a c# namespace.... (???)
Break with the old....start today would be my advice.



http://msdn.microsoft.com/en-us/library/893ke618(VS.71).aspx


The general rule for naming namespaces is to use the company name followed
by the technology name and optionally the feature and design as follows.

CompanyName.TechnologyName[.Feature][.Design]





SteveR said:
Feel like I'm taking baby steps here...

Ok, I was able to define the interface and args like:

namespace clsVideo
{
public interface IVideo
{
event EventHandler<PlayStateArgs> PlayStateEvent;
}

public class PlayStateArgs : EventArgs
{
public string strData;
}

}

Then in the class that's using the interface I have:

using clsVideo;
namespace VideoVLC

{

public class clsVideoVLC : clsVideo.IVideo

{
...
But now I'm getting two errors:
`VideoVLC.clsVideoVLC' does not implement interface member
`clsVideo.IVideo.PlayStateEvent.add' (CS0535)

`VideoVLC.clsVideoVLC' does not implement interface member
`clsVideo.IVideo.PlayStateEvent.remove' (CS0535)

Have I done something wrong... or not done something I should have?

sr
 
S

Steve Ricketts

Here's all the code. I tried to strip everything but the essentials.
Getting the following error:

'Velocedge.CADE.Video.VLC.clsVideoVLC<PlayStateArgs>' does not implement
interface member 'Velocedge.CADE.Video.IVideo.PlayStateEvent'.
'Velocedge.CADE.Video.VLC.clsVideoVLC<PlayStateArgs>.PlayStateEvent' cannot
implement 'Velocedge.CADE.Video.IVideo.PlayStateEvent' because it does not
have the matching return type of
'System.EventHandler<Velocedge.CADE.Video.PlayStateArgs>'.


using System;
namespace Velocedge.CADE.Video
{
public interface IVideo
{
event EventHandler<PlayStateArgs> PlayStateEvent;
}

public class PlayStateArgs : EventArgs
{
private readonly string _strData;

public PlayStateArgs(string strData)
{
_strData = strData;
}
public string strData { get { return _strData; } }
}
}

using System;
using Velocedge.CADE.Video;

namespace Velocedge.CADE.Video.VLC
{
public class clsVideoVLC<PlayStateArgs> : IVideo
{

public clsVideoVLC ()
{
}

#region playState Event
public event EventHandler PlayStateEvent;

protected virtual void onPlayStateEvent(PlayStateArgs e)
{
EventHandler<PlayStateArgs> handler = PlayStateEvent;
if (handler != null)
{
handler(this, e);
}
}
public void doPlayState(string strData)
{
PlayStateArgs e = new PlayStateArgs(strData);
onPlayStateEvent(e);
}
#endregion

}
}
 
W

Willem van Rumpt

namespace Velocedge.CADE.Video.VLC
{
public class clsVideoVLC<PlayStateArgs> : IVideo

You don't want to define a generic clsVideoVLC<PlayStateArgs> class,
you want to define a clsVideoVLC class. This should be:

public class clsVideoVLC : IVideo
public event EventHandler PlayStateEvent;

This should be defined as
public event EventHandler<PlayStateArgs> PlayStateEvent;

The entire definition of clsVideo should look like this:

public class clsVideoVLC : IVideo
{

public clsVideoVLC ()
{
}

#region playState Event
protected virtual void onPlayStateEvent(PlayStateArgs e)
{
EventHandler<PlayStateArgs> handler = PlayStateEvent;
if (handler != null)
{
handler(this, e);
}
}
public void doPlayState(string strData)
{
PlayStateArgs e = new PlayStateArgs(strData);
onPlayStateEvent(e);
}
#endregion

public event EventHandler<PlayStateArgs> PlayStateEvent;
}

Depending if you have the right "using" statement on top or not,
"PlayStateArgs" and "IVideo" may need to be prepended with the full
namespace:
"Velocedge.CADE.Video."
 
S

Steve Ricketts

Man, I really apologize for taking so much of your time to help such an
obvious dunce. As you have probably guessed by your comments on namespaces,
they were a problem. I'm getting killed by them...

In the interface class I was using Velocedge.CADE.Video
In the clsVideoVLC class I was using Velocedge.CADE.Video.VLC

On the statement "public class clsVideoVLC : IVideo"

It gives me "type or namespace IVideo could not be found"

So, I change it to "public class clsVideoVLC : Velocedge.CADE.Video.IVideo"
and after the last "." it shows "IVideo" so I think I'm good.

But, it gives me "type or namespace IVideo does not exist in the namespace
Velocedge.CADE.Video".

Ok, I give up on fancy namespaces and change all namespaces to
Velocedge.CADE.

Only now I get "type or namespace IVideo does not exist in the namespace
Velocedge.CADE" regardless whether I use IVideo or Velocedge.CADE.IVideo

Sorry...
 
S

Steve Ricketts

Ok, forget that last post... somehow I finally got the clsVideoVLC
(namespace Velocedge.CADE.Video.VLC) and clsVideo (namespace
Velocedge.CADE.Video) to build. So, then I tried to use them in a test
program (.exe). I added both .dll's as references and included the two
using directives:

using Velocedge.CADE.Video;
using Velocedge.CADE.Video.VLC;

However, I'm getting "The type or namespace 'VLC' does not exist in the
namespace 'Velocedge.CADE.Video'. So, does that mean I need some sort of
namespace reference to VLC within my clsVideo (Velocedge.CADE.Video) class?

sr
 
P

Peter Duniho

Steve said:
Ok, forget that last post... somehow I finally got the clsVideoVLC
(namespace Velocedge.CADE.Video.VLC) and clsVideo (namespace
Velocedge.CADE.Video) to build. So, then I tried to use them in a test
program (.exe). I added both .dll's as references and included the two
using directives:

using Velocedge.CADE.Video;
using Velocedge.CADE.Video.VLC;

However, I'm getting "The type or namespace 'VLC' does not exist in the
namespace 'Velocedge.CADE.Video'. So, does that mean I need some sort
of namespace reference to VLC within my clsVideo (Velocedge.CADE.Video)
class?

That depends on where you're getting the error. Each assembly needs a
reference to whatever other assembly contains types it uses.

The error is telling you literally what it says. The namespace doesn't
exist, as far as is known when compiling that assembly. That can happen
because it really doesn't exist, because there are no public types in
the namespace, or because you have failed to reference as assembly that
contains public types in that namespace.

Without more specific information from you (i.e. the exact code, what
DLLs that code exists in, and which assembly you're compiling when the
error occurs), there's not much else that can be said. It's a pretty
straightforward error.

Pete
 

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