function pointer

Simple question from beginner

Page 1 of 1

3 Replies - 10372 Views - Last Post: 03 December 2008 - 03:19 PM Rate Topic: -----

#1 ruslan40  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 34
  • Joined: 13-September 08

function pointer

Posted 14 September 2008 - 07:14 PM

Hi. I am fairly new to C# and am doing my first project in it. I am emerging from C, which I programmed in for several years and am now trying to master C# and .NET.

I have a very simple question about function pointers (i.e. callback functions).

Background:
I have a class in which I have a public struct to hold data. One of the fields in that struct needs to point to a callback function.

Here is the code:

	class Menu
	{
		public struct menuitem
		{
			public string title;
			public delegate void callback();
			public int target;		// -1 IF ITS NOT A SUBMENU; IF submenu then this is the array # of the menu
			public int showwarning;	// -1 if no warning is to be shown (default); otherwise, array # of the warning message to show just before preceeding
			public bool show;		  // Show the menuitem? (default = true)
		}

...




The problem I am having is with the
 public delegate void callback(); 
. How do I assign a function to it? Have I defined it wrong? Or is there some kind of rule I must follow when actually defining a function and assigning it to that variable?

Thanks a lot in advance.


Addition
----------

To clarify myself:

What I am trying to do is build a library for menus (as a small part of my project). My project will further use this library to provide the user with a GUI for easing configuration.
Therefore, I need many different menus to accomplish this task. All of them will be instances of the generic Menu class.

Each menu item will have an associated callback function with it. That function will be (somehow) associated to that delegate variable in that menuitem struct.

So, for example


void callbackfunc(void) {
...
}

void main() {

Menu a = new Menu();

// Register callback function

a.callback = callbackfunc();   // This, nor any similar combinations I have tried (e.g. new Delegate(callbackfunc), etc.), does not work!

...

}

...




Please help me on this, because I have no idea how to make this work in C#. In C we just used pointers, but I would prefer not to use "unsafe" code in my C# project since to me it seems that this was included in the language as a "detour", and is not intended to be regularly used.

Thank you very much in advance,
Rus

This post has been edited by ruslan40: 14 September 2008 - 10:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: function pointer

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: function pointer

Posted 15 September 2008 - 08:03 AM

First, while it's fair to say a C# delegate is like a C function pointer, it's only like. Think of it as a typedef. Also, never use a struct unless you really mean it.

You can do something like this:
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1 {
    class Menu {
        public delegate void MenuCallbackType();

        public string title;
        public MenuCallbackType callback;
        public int target;		// -1 IF ITS NOT A SUBMENU; IF submenu then this is the array # of the menu
        public int showwarning;	// -1 if no warning is to be shown (default); otherwise, array # of the warning message to show just before preceeding
        public bool show;		  // Show the menuitem? (default = true)
    }

    class Program {
        static void callbackfunc() { }

        static void Main(string[] args) {
            Menu a = new Menu();
            // a.callback = new Menu.MenuCallbackType(callbackfunc);
            // or
            a.callback = callbackfunc;
        }
    }
}



Though, to be honest, if you're even considering something like that, you might do better to just make a base class.

abstract class MenuBase {
	  public string title;
	  public int target;		// -1 IF ITS NOT A SUBMENU; IF submenu then this is the array # of the menu
	  public int showwarning;	// -1 if no warning is to be shown (default); otherwise, array # of the warning message to show just before preceeding
	  public bool show;		  // Show the menuitem? (default = true)
	  public abstract void callback();
 }

class Menu : MenuBase {
	public override void callback() { /* */ }
}



Hope this helps.
Was This Post Helpful? 1

#3 j.deepesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-December 08

Re: function pointer

Posted 02 December 2008 - 04:44 AM

Even i m having the same issue. Did u find the solution? Please help me
Was This Post Helpful? 0
  • +
  • -

#4 beatles1692  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 62
  • Joined: 03-December 08

Re: function pointer

Posted 03 December 2008 - 03:19 PM

Hi
As it's been mentioned , a delegate is like a pointer.Using a delegate you can assign a function to a variable and then call it back using the variable.

You can provide an external method ,an anonymous function or a lambda expression as a callback function.

In .Net framework you can use delegates to define event handlers to handle events.

Here's what you should do:(using an external method as a calback function)
1.Define a delegate(or use an existing one):
delegate int OperationDelegate(int operand1,int operand2);


2.Define a variable of type of the delegate and assign a function (with the same signature as your delegate to it)
 OperationDelegate SumOpDelegate=SumOpFunction;

or
 OperationDelegate SumOpDelegate=new OperationDelegate (SumOpFunction);

3.then SumOpFunction should be defined
4.then you can call the function (like calling a method):
int result= myCalc.SumOpDelegate(2,5);

Since a variable of type of delegate is an object itself , its default value is null and throws a null reference exception if it's called without being assigned.
Was This Post Helpful? 1

Page 1 of 1