error :Cannot implicitly convert type

J

juli

I declared:
public delegate void PaintEventHandler(object objSender,PaintEventArgs
pea);
and
this.Paint+=new PaintEventHandler(MyPaintHandler);
and the:
static void MyPaintHandler(object objSender,PaintEventArgs pea)
{
}


I got this error:

Cannot implicitly convert type 'deleg.Form1.PaintEventHandler' to
'System.Windows.Forms.PaintEventHandler'
 
A

Ashish Das

reason is this --

this.Paint is actually a event which is declared as following

public event System.Windows.Forms.PaintEventandler Paint

....and NOT...

public event deleg.Form1.PaintEventHandler Paint

so, if you try to give your own override to it it will give error
 
A

Ashish Das

small typo, please read ------

public event System.Windows.Forms.PaintEventHandler Paint
-------------------------------------------------^
 
J

juli jul

Thanks for the reply but I keep having the same error in this case also:

class EventListener
{
private Janus.Windows.GridEX.GridEX gridex;
public EventListener(Janus.Windows.GridEX.GridEX grid)
{
this.gridex=grid;
this.gridex.SelectionChanged+=new ChangedEventHandler(GridexChanged);
}

private void GridexChanged(object sender, EventArgs e)
{
Console.WriteLine("This is called when the event fires.");
}
}


-------------------------------------------------------
in other class:
EventListener listener = new EventListener(this.gridex_exc);

and the delegate:
public delegate void ChangedEventHandler(object sender, EventArgs e);

Why I keep having this error? Thanks!

error:
error CS0029: Cannot implicitly convert type
'WindowsApplication2.ChangedEventHandler' to 'System.EventHandler'
 
J

Joakim Karlsson

juli said:
Thanks for the reply but I keep having the same error in this case also:

class EventListener
{
private Janus.Windows.GridEX.GridEX gridex;
public EventListener(Janus.Windows.GridEX.GridEX grid)
{
this.gridex=grid;
this.gridex.SelectionChanged+=new ChangedEventHandler(GridexChanged);
}

private void GridexChanged(object sender, EventArgs e)
{
Console.WriteLine("This is called when the event fires.");
}
}


-------------------------------------------------------
in other class:
EventListener listener = new EventListener(this.gridex_exc);

and the delegate:
public delegate void ChangedEventHandler(object sender, EventArgs e);

Why I keep having this error? Thanks!

error:
error CS0029: Cannot implicitly convert type
'WindowsApplication2.ChangedEventHandler' to 'System.EventHandler'

How is the Janus.Windows.GridEX.GridEX.SelectionChanged event declared.
I suspect it is declared as:

public event EventHandler SelectionChanged;

This is ok, as your ChangedEventHandler delegate has exactly the same
signature as the System.EventHandler delegate. It really doesn't bring
anything new to the table.

Either change the SelectionChanged to use your ChangedEventHandler
delegate, or change your event subscription code to:

this.gridex.SelectionChanged+=new EventHandler(GridexChanged);

/Joakim
 
A

Ashish Das

If you find these kind of problems, the best is to use object browser as it
gives you the idea of how things are and can be used.

Thanks,
Ashish Das
 

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