473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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();
}

}
}

Mar 20 '06 #1
7 1455

- 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" <ht*******@hotmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
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();
}

}
}

Mar 20 '06 #2
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.

Mar 20 '06 #3
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.

Mar 20 '06 #4
c#2006user wrote:
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


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

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

Mar 20 '06 #5

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
c#2006user wrote:
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


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?
Mar 21 '06 #6
i done this and it seemed to work

MainListSelectedEvent += new
MainListSelectedEventHandler(mainList_Selected);

thanks a million for your help

Mar 21 '06 #7

"c#2006user" <ht*******@hotmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
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
Mar 22 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Stephen | last post by:
I am trying to add some code to below to include a datatable and fill the datatable. The reason for doing this is so as I can check to see whether there are any rows returned by the stored...
2
by: MichaelH | last post by:
Hi all, I am writing a program that has to draw musical notes on a staff. To do this I need to draw Ellispses (representing the notes) on a horizontal line and rotate them slightly (by 15...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
10
by: Mae Lim | last post by:
Dear all, I'm new to C# WebServices. I compile the WebService project it return no errors "Build: 1 succeeded, 0 failed, 0 skipped". Basically I have 2 WebMethod, when I try to invoke the...
5
by: Buchwald | last post by:
hello group, I have a long (large) script that shows a random picture when a webpage is refreshed. It's long because i have a lot of pictures: 246 Here is some code:...
3
by: Dennis | last post by:
Hello all. I have an app that is giving me problems on a WindowsXP x64 machine. When I go to close the application the Main form closes, but when I look at TaskManager the process still resides...
2
by: trihanhcie | last post by:
I m currently working on a Unix server with a fedora 3 as an os My current version of mysql is 3.23.58. I'd like to upgrade the version to 5.0.18. After downloading from MYSQL.COM the package on...
9
by: smartbei | last post by:
Hello, I am a newbie with python, though I am having a lot of fun using it. Here is one of the excersizes I am trying to complete: the program is supposed to find the coin combination so that with...
2
by: rookiejavadude | last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone...
1
by: Tom | last post by:
First, I posted a similar request for help in another group and now don't find the posting. Problem with my newsreader perhaps ... but apologies if this appears as a cross posting. My code is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.