Help Needed! Really Stuck. C# dvents & null delegates

C

c#2006user

Hi everyone.

i know there is a lot of code and ive reduced it as much as i could,
its a bit much to ask but i am really stuck!
i've been at this for a week! this is converted from vb to c# and
something has gone wrong with the events as the original code used
"WithEvents" and "Handles" keywords.

basically when i debug and get to the mainList_Selected(.............)
method the MainListSelectedEvent is null and i think this is where the
problem is.

if anyone could help id be really greatful!!

This is the main class and this is the method that is called

private void displayHomePage()
{
try
{
Page pg;
while (history.Count > 0)
{
pg = (Page) history.Pop();
pg.Dispose();
}
if (currentPage != null)
{
currentPage.Dispose();
currentPage = null;
}
HomePage newPage = new HomePage();
currentPage = newPage;
MusicXP.frmMain X = this;
newPage.DisplayPage(ref X);
}
catch (System.Exception ex)
{
ErrorHandler.GenericHandler(ex);
}

}

----------------------------------------------------------------------------------------------------------------------------------

public abstract class Page : IDisposable
{
protected dmControls.imageList albumList;
protected dmControls.detailImageList detailList;
protected dmControls.ListControl mainList;
protected dmControls.ListControl sideList;
protected dmControls.VisKeys softKeyboard;
protected frmMain theForm;
protected int albumListSelectedIndex;
protected int detailListSelectedIndex;
protected int mainListSelectedIndex;
protected int sideListSelectedIndex;
protected string hasFocus;
protected string softKeyboardText;

//I took out the main events and delegates that were created in
the conversion, this i think is where the problem
//The MainListSelectedEvent is Null When it is called in the
mainList_Selected.

public delegate void MainListSelectedEventHandler(string
sSelected, object Data);
public MainListSelectedEventHandler MainListSelectedEvent; //=
new MainListSelectedEventHandler(mainList_Selected);
public event MainListSelectedEventHandler MainListSelected
{
add { MainListSelectedEvent =
(MainListSelectedEventHandler)System.Delegate.Combine(MainListSelectedEvent,
value); }
remove { MainListSelectedEvent =
(MainListSelectedEventHandler)System.Delegate.Remove(MainListSelectedEvent,
value); }
}

public void mainList_Selected(string sSelected, object Data)
{
// object tmpObject = new object();
//Data = tmpObject;
Sounds.PlaySelectSound();
if (MainListSelectedEvent != null)
MainListSelectedEvent(sSelected, Data);
}

"The Rest of the Delegates are in here"
"The Rest of the Event are in here"

public virtual void DisplayPage(ref frmMain myForm)
{
myForm.currentPage = this;
this.albumList = myForm.albumList;
this.detailList = myForm.detailList;
this.mainList = myForm.listNavigation;
this.sideList = myForm.sideNavigation;
this.softKeyboard = myForm.softKeyboard;
this.theForm = 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_SelectionChanged);


"the Rest of the assignment are in here.............."


}

public Page()
{

}

public void Dispose()
{
this.Dispose();
}
}



---------------------------------------------------------------------------------------------------------------------------------------------------

class HomePage : NavigationPage
{

public HomePage()
{

string[] navArray = new string[6];
navArray[0] = "Recent Albums";
navArray[1] = "Browse All Albums";
navArray[2] = "Browse Artists";
navArray[3] = "Playlists";
navArray[4] = "Scan Music Files";
navArray[5] = "Settings";

base.navArray = navArray;
base.itemCount = navArray.GetLength(0);
//base.mainList.Selected += new
dmControls.ListControl.SelectedEventHandler(mainList_Selected);


}

private void HomePage_MainListSelected(string sSelected, object
Data)
{
stuff in here............................
}

public override void SavePageState(ref frmMain myForm)
{
base.SavePageState(ref myForm);
}

public override void DisplayPage(ref frmMain myForm)
{
base.DisplayPage(ref myForm);
myForm.wmp.Visible = true;
myForm.SetUpHomeView();
}

}
}
 
N

Nick Hounsome

- You never attach a handler to the event MainListSelected.
- I see no reason for all the ref parameters.
You only need ref for a form if you want to write use myForm as an
L-value NOT when you are just calling its members or properties.
- You've gone to a lot of trouble to write code that the compiler will do
for you - All you need is:

public event MainListSelectedEventHandler MainListSelected;
public void mainList_Selected(string sSelected, object Data)
{
//........
if (MainListSelectedEvent != null)
MainListSelectedEvent(.....);
}

The compiler creates the delegate and add and remove methods.

- You have commented out an initialization of your delegate which would have
assigned it the method that actually calls it - this would have been
infinitely recursive - I have no idea what you really intended.
- IMHO the whole thing is a bit of a mess because of your naming - You have
something that isn't an event called MainListSelectedEvent; You have an
event called Selected on something called mainList and an event called
MainListSelected on the page.

c#2006user said:
Hi everyone.

i know there is a lot of code and ive reduced it as much as i could,
its a bit much to ask but i am really stuck!
i've been at this for a week! this is converted from vb to c# and
something has gone wrong with the events as the original code used
"WithEvents" and "Handles" keywords.

basically when i debug and get to the mainList_Selected(.............)
method the MainListSelectedEvent is null and i think this is where the
problem is.

if anyone could help id be really greatful!!

This is the main class and this is the method that is called

private void displayHomePage()
{
try
{
Page pg;
while (history.Count > 0)
{
pg = (Page) history.Pop();
pg.Dispose();
}
if (currentPage != null)
{
currentPage.Dispose();
currentPage = null;
}
HomePage newPage = new HomePage();
currentPage = newPage;
MusicXP.frmMain X = this;
newPage.DisplayPage(ref X);
}
catch (System.Exception ex)
{
ErrorHandler.GenericHandler(ex);
}

}

----------------------------------------------------------------------------------------------------------------------------------

public abstract class Page : IDisposable
{
protected dmControls.imageList albumList;
protected dmControls.detailImageList detailList;
protected dmControls.ListControl mainList;
protected dmControls.ListControl sideList;
protected dmControls.VisKeys softKeyboard;
protected frmMain theForm;
protected int albumListSelectedIndex;
protected int detailListSelectedIndex;
protected int mainListSelectedIndex;
protected int sideListSelectedIndex;
protected string hasFocus;
protected string softKeyboardText;

//I took out the main events and delegates that were created in
the conversion, this i think is where the problem
//The MainListSelectedEvent is Null When it is called in the
mainList_Selected.

public delegate void MainListSelectedEventHandler(string
sSelected, object Data);
public MainListSelectedEventHandler MainListSelectedEvent; //=
new MainListSelectedEventHandler(mainList_Selected);
public event MainListSelectedEventHandler MainListSelected
{
add { MainListSelectedEvent =
(MainListSelectedEventHandler)System.Delegate.Combine(MainListSelectedEvent,
value); }
remove { MainListSelectedEvent =
(MainListSelectedEventHandler)System.Delegate.Remove(MainListSelectedEvent,
value); }
}

public void mainList_Selected(string sSelected, object Data)
{
// object tmpObject = new object();
//Data = tmpObject;
Sounds.PlaySelectSound();
if (MainListSelectedEvent != null)
MainListSelectedEvent(sSelected, Data);
}

"The Rest of the Delegates are in here"
"The Rest of the Event are in here"

public virtual void DisplayPage(ref frmMain myForm)
{
myForm.currentPage = this;
this.albumList = myForm.albumList;
this.detailList = myForm.detailList;
this.mainList = myForm.listNavigation;
this.sideList = myForm.sideNavigation;
this.softKeyboard = myForm.softKeyboard;
this.theForm = 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_SelectionChanged);


"the Rest of the assignment are in here.............."


}

public Page()
{

}

public void Dispose()
{
this.Dispose();
}
}



---------------------------------------------------------------------------------------------------------------------------------------------------

class HomePage : NavigationPage
{

public HomePage()
{

string[] navArray = new string[6];
navArray[0] = "Recent Albums";
navArray[1] = "Browse All Albums";
navArray[2] = "Browse Artists";
navArray[3] = "Playlists";
navArray[4] = "Scan Music Files";
navArray[5] = "Settings";

base.navArray = navArray;
base.itemCount = navArray.GetLength(0);
//base.mainList.Selected += new
dmControls.ListControl.SelectedEventHandler(mainList_Selected);


}

private void HomePage_MainListSelected(string sSelected, object
Data)
{
stuff in here............................
}

public override void SavePageState(ref frmMain myForm)
{
base.SavePageState(ref myForm);
}

public override void DisplayPage(ref frmMain myForm)
{
base.DisplayPage(ref myForm);
myForm.wmp.Visible = true;
myForm.SetUpHomeView();
}

}
}
 
C

c#2006user

Thanks for thaking the time to reply Nick,
This is mostly auto-converted code from a vb.net project and i presume
that is why its
so overly complex/messy and hence why im so confused. (i could find no
examples of delegates and events that matched what i got from the
converter!)

Anyway I will try your suggestion and comment out the stuff you think i
dont need.
 
C

c#2006user

ok i tried reducing it to

public delegate void MainListSelectedEventHandler(string sSelected,
object Data);
public event MainListSelectedEventHandler MainListSelectedEvent ;

this.mainList.Selected += new
dmControls.ListControl.SelectedEventHandler(mainList_Selected); //in
the DisplayPage method

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

but it still jumps over MainListSelectedEvent(sSelected, Data);
because its null.

any other ideas nick or anyone.
thanks again.
 
C

Chris Dunaway

c#2006user said:
ok i tried reducing it to

public delegate void MainListSelectedEventHandler(string sSelected,
object Data);
public event MainListSelectedEventHandler MainListSelectedEvent ;
this.mainList.Selected += new
dmControls.ListControl.SelectedEventHandler(mainList_Selected); //in
the DisplayPage method

The event variable is called MainListSelectedEvent so I think the line
above should be:

this.mainList.MainListSelectedEvent += new
MainListSelectedEventHandler(mainList_Selected);
 
N

Nick Hounsome

Chris Dunaway said:
The event variable is called MainListSelectedEvent so I think the line
above should be:

this.mainList.MainListSelectedEvent += new
MainListSelectedEventHandler(mainList_Selected);

No - he means it. This is what I meant earlier about confusing naming - he
wants page.mainList.Selected to trigger page.MainListSelectedEvent -
confusing naming and not very OO.

There has to be a line of code somewhere saying page.MainListSelectedEvent
+= something. Where (and when) is it?
 
C

c#2006user

i done this and it seemed to work

MainListSelectedEvent += new
MainListSelectedEventHandler(mainList_Selected);

thanks a million for your help
 
N

Nick Hounsome

c#2006user said:
i done this and it seemed to work

MainListSelectedEvent += new
MainListSelectedEventHandler(mainList_Selected);

thanks a million for your help

I don't see how that can work because mainList_Selected calls the
MainListSelectedEvent according to your posting and this would result in a
infinite recursion
 

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