473,804 Members | 2,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Select ed(............ .)
method the MainListSelecte dEvent 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.Dis pose();
currentPage = null;
}
HomePage newPage = new HomePage();
currentPage = newPage;
MusicXP.frmMain X = this;
newPage.Display Page(ref X);
}
catch (System.Excepti on ex)
{
ErrorHandler.Ge nericHandler(ex );
}

}

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

public abstract class Page : IDisposable
{
protected dmControls.imag eList albumList;
protected dmControls.deta ilImageList detailList;
protected dmControls.List Control mainList;
protected dmControls.List Control sideList;
protected dmControls.VisK eys softKeyboard;
protected frmMain theForm;
protected int albumListSelect edIndex;
protected int detailListSelec tedIndex;
protected int mainListSelecte dIndex;
protected int sideListSelecte dIndex;
protected string hasFocus;
protected string softKeyboardTex t;

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

public delegate void MainListSelecte dEventHandler(s tring
sSelected, object Data);
public MainListSelecte dEventHandler MainListSelecte dEvent; //=
new MainListSelecte dEventHandler(m ainList_Selecte d);
public event MainListSelecte dEventHandler MainListSelecte d
{
add { MainListSelecte dEvent =
(MainListSelect edEventHandler) System.Delegate .Combine(MainLi stSelectedEvent ,
value); }
remove { MainListSelecte dEvent =
(MainListSelect edEventHandler) System.Delegate .Remove(MainLis tSelectedEvent,
value); }
}

public void mainList_Select ed(string sSelected, object Data)
{
// object tmpObject = new object();
//Data = tmpObject;
Sounds.PlaySele ctSound();
if (MainListSelect edEvent != null)
MainListSelecte dEvent(sSelecte d, 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.currentP age = this;
this.albumList = myForm.albumLis t;
this.detailList = myForm.detailLi st;
this.mainList = myForm.listNavi gation;
this.sideList = myForm.sideNavi gation;
this.softKeyboa rd = myForm.softKeyb oard;
this.theForm = myForm;
this.mainList.L eaveControl += new
dmControls.List Control.LeaveCo ntrolEventHandl er(mainList_Lea veControl);
this.mainList.S elected += new
dmControls.List Control.Selecte dEventHandler(m ainList_Selecte d);
this.mainList.S electionChanged += new
dmControls.List Control.Selecti onChangedEventH andler(mainList _SelectionChang ed);
"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.GetLen gth(0);
//base.mainList.S elected += new
dmControls.List Control.Selecte dEventHandler(m ainList_Selecte d);
}

private void HomePage_MainLi stSelected(stri ng sSelected, object
Data)
{
stuff in here........... ............... ..
}

public override void SavePageState(r ef frmMain myForm)
{
base.SavePageSt ate(ref myForm);
}

public override void DisplayPage(ref frmMain myForm)
{
base.DisplayPag e(ref myForm);
myForm.wmp.Visi ble = true;
myForm.SetUpHom eView();
}

}
}

Mar 20 '06 #1
7 1478

- You never attach a handler to the event MainListSelecte d.
- 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 MainListSelecte dEventHandler MainListSelecte d;
public void mainList_Select ed(string sSelected, object Data)
{
//........
if (MainListSelect edEvent != null)
MainListSelecte dEvent(.....);
}

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 MainListSelecte dEvent; You have an
event called Selected on something called mainList and an event called
MainListSelecte d on the page.

"c#2006user " <ht*******@hotm ail.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.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_Select ed(............ .)
method the MainListSelecte dEvent 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.Dis pose();
currentPage = null;
}
HomePage newPage = new HomePage();
currentPage = newPage;
MusicXP.frmMain X = this;
newPage.Display Page(ref X);
}
catch (System.Excepti on ex)
{
ErrorHandler.Ge nericHandler(ex );
}

}

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

public abstract class Page : IDisposable
{
protected dmControls.imag eList albumList;
protected dmControls.deta ilImageList detailList;
protected dmControls.List Control mainList;
protected dmControls.List Control sideList;
protected dmControls.VisK eys softKeyboard;
protected frmMain theForm;
protected int albumListSelect edIndex;
protected int detailListSelec tedIndex;
protected int mainListSelecte dIndex;
protected int sideListSelecte dIndex;
protected string hasFocus;
protected string softKeyboardTex t;

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

public delegate void MainListSelecte dEventHandler(s tring
sSelected, object Data);
public MainListSelecte dEventHandler MainListSelecte dEvent; //=
new MainListSelecte dEventHandler(m ainList_Selecte d);
public event MainListSelecte dEventHandler MainListSelecte d
{
add { MainListSelecte dEvent =
(MainListSelect edEventHandler) System.Delegate .Combine(MainLi stSelectedEvent ,
value); }
remove { MainListSelecte dEvent =
(MainListSelect edEventHandler) System.Delegate .Remove(MainLis tSelectedEvent,
value); }
}

public void mainList_Select ed(string sSelected, object Data)
{
// object tmpObject = new object();
//Data = tmpObject;
Sounds.PlaySele ctSound();
if (MainListSelect edEvent != null)
MainListSelecte dEvent(sSelecte d, 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.currentP age = this;
this.albumList = myForm.albumLis t;
this.detailList = myForm.detailLi st;
this.mainList = myForm.listNavi gation;
this.sideList = myForm.sideNavi gation;
this.softKeyboa rd = myForm.softKeyb oard;
this.theForm = myForm;
this.mainList.L eaveControl += new
dmControls.List Control.LeaveCo ntrolEventHandl er(mainList_Lea veControl);
this.mainList.S elected += new
dmControls.List Control.Selecte dEventHandler(m ainList_Selecte d);
this.mainList.S electionChanged += new
dmControls.List Control.Selecti onChangedEventH andler(mainList _SelectionChang ed);
"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.GetLen gth(0);
//base.mainList.S elected += new
dmControls.List Control.Selecte dEventHandler(m ainList_Selecte d);
}

private void HomePage_MainLi stSelected(stri ng sSelected, object
Data)
{
stuff in here........... ............... ..
}

public override void SavePageState(r ef frmMain myForm)
{
base.SavePageSt ate(ref myForm);
}

public override void DisplayPage(ref frmMain myForm)
{
base.DisplayPag e(ref myForm);
myForm.wmp.Visi ble = true;
myForm.SetUpHom eView();
}

}
}

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 MainListSelecte dEventHandler(s tring sSelected,
object Data);
public event MainListSelecte dEventHandler MainListSelecte dEvent ;

this.mainList.S elected += new
dmControls.List Control.Selecte dEventHandler(m ainList_Selecte d); //in
the DisplayPage method

public void mainList_Select ed(string sSelected, object Data)
{
if (MainListSelect edEvent != null)
MainListSelecte dEvent(sSelecte d, Data);
}

but it still jumps over MainListSelecte dEvent(sSelecte d, 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 MainListSelecte dEventHandler(s tring sSelected,
object Data);
public event MainListSelecte dEventHandler MainListSelecte dEvent ;
this.mainList.S elected += new
dmControls.List Control.Selecte dEventHandler(m ainList_Selecte d); //in
the DisplayPage method


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

this.mainList.M ainListSelected Event += new
MainListSelecte dEventHandler(m ainList_Selecte d);

Mar 20 '06 #5

"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
c#2006user wrote:
ok i tried reducing it to

public delegate void MainListSelecte dEventHandler(s tring sSelected,
object Data);
public event MainListSelecte dEventHandler MainListSelecte dEvent ;

this.mainList.S elected += new
dmControls.List Control.Selecte dEventHandler(m ainList_Selecte d); //in
the DisplayPage method


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

this.mainList.M ainListSelected Event += new
MainListSelecte dEventHandler(m ainList_Selecte d);


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

There has to be a line of code somewhere saying page.MainListSe lectedEvent
+= something. Where (and when) is it?
Mar 21 '06 #6
i done this and it seemed to work

MainListSelecte dEvent += new
MainListSelecte dEventHandler(m ainList_Selecte d);

thanks a million for your help

Mar 21 '06 #7

"c#2006user " <ht*******@hotm ail.com> wrote in message
news:11******** *************@j 33g2000cwa.goog legroups.com...
i done this and it seemed to work

MainListSelecte dEvent += new
MainListSelecte dEventHandler(m ainList_Selecte d);

thanks a million for your help


I don't see how that can work because mainList_Select ed calls the
MainListSelecte dEvent 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
1614
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 procedure. If there are no records returned then this would give me an indicator and I can re-direct the page somewhere more appropriate. Well this is the theory. I have never used datatables before and am not sure how to implemenet what I want so I was...
2
3123
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 degrees). So there might be say, 3 ellispses each one seperated from the next by 30 pixals along the x-axis but each ellipse should have the same y coordinate. However everytime I try and rotate the ellispe using the RotateTransform() method and...
8
10720
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 receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
10
14859
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 first method it is working fine. Then when I try to invoke the second method it return me an error, Just In-Time Debugging, with error message "An exception 'System.StackOverflowException' has occurred in WebServices"
5
1229
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: ----------------------------------------------------------------------------------------------- <!-- image1="smallpics/001-smallpic.jpg"
3
2791
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 in the list. Using SPY++ I can see that a "WindowsFormsParkingWindow" and ".NET BroadcastEventWindow1.0.5000.0.1" are associated to my app by looking at the Properties->Process Tab then selecting the ThreadID shows me my application in the...
2
2908
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 the site, I made : rpm -i MySQL-server-5.0.18-0.i386.rpm then i have errors that relate to many conflicts. I cannot figure out why -and- cannot upgrade. Please Help !
9
2103
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 10 coins you can reach a certain amoung, taken as a parameter. Here is the current program: coins = (100,10,5,1,0.5) anslist = def bar(fin, hist = {100:0,10:0,5:0,1:0,0.5:0}): s = sum(x*hist for x in hist)
2
2906
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 help? my script is below. thank you import java.awt.*; //import all java.awt import java.awt.event.*; //import all java.awt.event import java.util.*; //import all java.util import javax.swing.*; //import all javax.swing class Product...
1
13747
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 inconsistent in the detection of a vertical scrollbar. This involves situations when less than the client area is needed to hold the small amount of data. The inconsistency is when adjusting the panel size within or just below the bottom row....
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10569
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10315
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9140
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.