Connecting Tech Pros Worldwide Forums | Help | Site Map

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

c#2006user
Guest
 
Posts: n/a
#1: Mar 20 '06
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.Comb ine(MainListSelectedEvent,
value); }
remove { MainListSelectedEvent =
(MainListSelectedEventHandler)System.Delegate.Remo ve(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(ma inList_LeaveControl);
this.mainList.Selected += new
dmControls.ListControl.SelectedEventHandler(mainLi st_Selected);
this.mainList.SelectionChanged += new
dmControls.ListControl.SelectionChangedEventHandle r(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(mainLi st_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();
}

}
}


Nick Hounsome
Guest
 
Posts: n/a
#2: Mar 20 '06

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



- 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" <htpc_2006@hotmail.com> wrote in message
news:1142820289.523006.119370@g10g2000cwb.googlegr oups.com...[color=blue]
> 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.Comb ine(MainListSelectedEvent,
> value); }
> remove { MainListSelectedEvent =
> (MainListSelectedEventHandler)System.Delegate.Remo ve(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(ma inList_LeaveControl);
> this.mainList.Selected += new
> dmControls.ListControl.SelectedEventHandler(mainLi st_Selected);
> this.mainList.SelectionChanged += new
> dmControls.ListControl.SelectionChangedEventHandle r(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(mainLi st_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();
> }
>
> }
> }
>[/color]


c#2006user
Guest
 
Posts: n/a
#3: Mar 20 '06

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


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#2006user
Guest
 
Posts: n/a
#4: Mar 20 '06

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


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(mainLi st_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.

Chris Dunaway
Guest
 
Posts: n/a
#5: Mar 20 '06

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


c#2006user wrote:[color=blue]
> ok i tried reducing it to
>
> public delegate void MainListSelectedEventHandler(string sSelected,
> object Data);
> public event MainListSelectedEventHandler MainListSelectedEvent ;
>[/color]
[color=blue]
> this.mainList.Selected += new
> dmControls.ListControl.SelectedEventHandler(mainLi st_Selected); //in
> the DisplayPage method
>[/color]

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

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

Nick Hounsome
Guest
 
Posts: n/a
#6: Mar 21 '06

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



"Chris Dunaway" <dunawayc@gmail.com> wrote in message
news:1142889477.899317.283780@g10g2000cwb.googlegr oups.com...[color=blue]
> c#2006user wrote:[color=green]
>> ok i tried reducing it to
>>
>> public delegate void MainListSelectedEventHandler(string sSelected,
>> object Data);
>> public event MainListSelectedEventHandler MainListSelectedEvent ;
>>[/color]
>[color=green]
>> this.mainList.Selected += new
>> dmControls.ListControl.SelectedEventHandler(mainLi st_Selected); //in
>> the DisplayPage method
>>[/color]
>
> The event variable is called MainListSelectedEvent so I think the line
> above should be:
>
> this.mainList.MainListSelectedEvent += new
> MainListSelectedEventHandler(mainList_Selected);[/color]

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#2006user
Guest
 
Posts: n/a
#7: Mar 21 '06

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


i done this and it seemed to work

MainListSelectedEvent += new
MainListSelectedEventHandler(mainList_Selected);

thanks a million for your help

Nick Hounsome
Guest
 
Posts: n/a
#8: Mar 22 '06

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



"c#2006user" <htpc_2006@hotmail.com> wrote in message
news:1142963025.408456.49230@j33g2000cwa.googlegro ups.com...[color=blue]
>i done this and it seemed to work
>
> MainListSelectedEvent += new
> MainListSelectedEventHandler(mainList_Selected);
>
> thanks a million for your help
>[/color]

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


Closed Thread