So how does one handle saving a function and then calling it later from where it is saved? “Delegates” is what they are called in C# and they are essentially function pointers. The problem is that they may be more than that and there is no way to know.
So far, all of my debugging in from within the Windows Phone emulator. I have not tried to find more information on the problem but here is that happened to me that prompted this post:
- From inside of a member function in a class, save a delegate function that “points” to a member function of the class.
- Navigate to a different page of the user interface thus destroying the object that held the code from step 1 above.
- Navigate to the page that was active in step 1 thus creating a new object of the same class as was used in step 1.
- Call the delegate that had been saved. The code fails to function as expected.
What I think is happening is the delegate function has knowledge of the object related to the function that I save. When the delegate function is called, the function is called in the context of the original object, not the new object.
This makes some sense but also makes no sense at all. I want to save a pointer to a function in a class so that I can call that function when I return to the page the second time. I don’t want to get back or operate on the object for the page that is gone.
I did not try to find more information about the delegate function. I don’t know if the failure was as I described and it is possible that I screwed up the code. On the other hand, this is fairly simple code. There is no reason for it to fail to operate properly but to also not crash.
The class is a PhoneApplicationPage and it has a function that handles a button press. When the button is pressed, I set the delegate function to “point” to the function that I need to execute when this page is displayed again later. I use the NavigationServices.Navigate() function to navigate to a different page and the object for the page is destroyed at some point. Or maybe not. Upon successful navigation back to the original page, I call the delegate and all changes to the user interface DO NOT HAPPEN. The code runs fine so it is not a matter of there being no page or no user interface controls. The changes just don’t show up.
My only explanation is that the delegate is operating on the original page object. Since this is C#, objects persist longer than I think I need them and any reference to an object should keep it around in memory and usable.
Managed code that has no pointers protects me from making mistakes but it also hides mistakes by letting my code run when I would rather have it crash. I need to figure out how to fix this but I have a project that has a deadline so I worked around this by not saving a delegate. I decide what function to call based on other information upon successful return to the original page.
And after one minute of searching today, I find the answer. No amount of searching yesterday yielded useful results. Here is what Microsoft says:
“Unlike C++ function pointers, however, delegates are fully object oriented, and unlike C++ pointers to member functions, delegates encapsulate both an object instance and a method.”
What then is the way to solve this? The immediate solution seems to be to create a static member function and have it accept an argument that is the object to use for a call to the non-static function I need. Saving a delegate for a static function means it will not have any object information in it.
I see no other solution which makes me thing that someone really missed the mark when designing delegates in C#. On the other hand, there is then no way to accidentally call the wrong function in the wrong place in C#. Still, I prefer the flexibility of C++ even if the risks are higher. I can write less code in C++ to do what is tricky in C#.