vb.net to c# conversion help please

H

htpc_2006

Hi everyone, i have a vb.net program ive converted most of the code but


i know events dont translate well, the original vb.net code uses
withevents and .handles and i can't get the events in c# to work 100%


they seem to hook up ok but when the eventhandler is called from the
method its null and fails.
or maybe im reading it wrong?


public abstract class Page : IDisposable
{
protected ListControl mainList;


public virtual void DisplayPage(ref frmMain myForm)
{
this.mainList.LeaveControl += new
dmControls.ListControl.LeaveControlEventHandler(mainList_LeaveControl);



this.mainList.Selected += new
dmControls.ListControl.SelectedEventHandler(mainList_Selected);
this.mainList.SelectionChanged += new
dmControls.ListControl.SelectionChangedEventHandler(mainList_SelectionChang­­­ed);




}


protected delegate void
MainListLeaveControlEventHandler(dmControls.Direction direction);
private MainListLeaveControlEventHandler MainListLeaveControlEvent;
protected event MainListLeaveControlEventHandler MainListLeaveControl
{
add { MainListLeaveControlEvent =
(MainListLeaveControlEventHandler)System.Delegate.Combine(MainListLeaveCont­­­rolEvent,


value); }
remove { MainListLeaveControlEvent =
(MainListLeaveControlEventHandler)System.Delegate.Remove(MainListLeaveContr­­­olEvent,



value); }



}


protected delegate void MainListSelectedEventHandler(string sSelected,
object Data);
protected MainListSelectedEventHandler MainListSelectedEvent;
protected event MainListSelectedEventHandler MainListSelected
{
add { MainListSelectedEvent =
(MainListSelectedEventHandler)System.Delegate.Combine(MainListSelectedEvent­­­,


value); }
remove { MainListSelectedEvent =
(MainListSelectedEventHandler)System.Delegate.Remove(MainListSelectedEvent,



value); }



}


protected delegate void MainListSelectionChangedEventHandler(string
sSelected, object Item);
private MainListSelectionChangedEventHandler
MainListSelectionChangedEvent;
protected event MainListSelectionChangedEventHandler
MainListSelectionChanged
{
add { MainListSelectionChangedEvent =
(MainListSelectionChangedEventHandler)System.Delegate.Combine(MainListSelec­­­tionChangedEvent,


value); }
remove { MainListSelectionChangedEvent =
(MainListSelectionChangedEventHandler)System.Delegate.Remove(MainListSelect­­­ionChangedEvent,



value); }



}


private void mainList_LeaveControl(Direction direction)
{
..........................


}


private void mainList_Selected(string sSelected, object Data)
{
if (MainListSelectedEvent != null)
MainListSelectedEvent(sSelected, Data);


}
}


DisplayPage is then overridden and called from different type of pages
that inherit from page.

its the MainListSelectedEvent thats null when i try and call it.


any help really appreciated!


thanks.
bryan
 
J

Jon Skeet [C# MVP]

Hi everyone, i have a vb.net program ive converted most of the code but

i know events dont translate well, the original vb.net code uses
withevents and .handles and i can't get the events in c# to work 100%

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

(The code you posted is neither short, nor complete.)
 

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