Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
I have some old c code that use callback functions, actually the callback function calls itself several time . Since I am moving platform from VC6 to .Net I would like to hear what might be the easiest way to solve the problem about callback by only using C++ or C#. As far as I know callback function are not used in OOP like C++ or C#. I am aware of using virtual function, but virtual function can not make recursive calls. Anyone got some ideas ?
Posted

Callbacks are perfectly usable in C++ (through function pointers or functor classes). In C# we have delegates. What do you mean virtual functions cannot make recursive calls (I don't see why not)?

 

How is this windows forms related?

 

-- Pete

Posted

Are delegates in C# similar to callbacks ?

 

I do not like function pointer. They force your callback functions to be static, so I can not inherit the function. I rather like virtual function

 

void CForm::ExOfCallback(BYTE id)

{

while ( id++ < MAX )

if (AcceptedId(Id))

break;

 

send(id, ExOfCallback);

}

 

Lets take this example. ExOfCallback is a virtual function. As long as id < MAX is must continue call itself. How do you do that ??

Posted

You don't like function pointers.. and yet your example seems to be using them? I can't see any other way ExOfCallback could be passed to the send method. I would like to see the signature of the send method to be sure.

 

Delegates in c# are quite different (there's lots of info on the internet).

 

It would be a good idea to let us know exactly what you are trying to achieve here. What does the code do?

 

-- Pete

Posted

so you mean that a callback function will always end up to be static even that you are NOT using function pointer. Let say you are using function objects instead ?

 

And callbacks can only call itself by using function pointer ?

  • *Gurus*
Posted

Sure. Here's a rather useless expansion of the example you gave earlier in this thread. Since the send function calls the callback function this code will result in a stack overflow exception, but you can see how to define the delegate and pass it to the other function.

 

	private bool AcceptedId(byte id)
	{
		return (id >= 10);
	}

	private const int MAX = 100;

	private delegate void MyDelegateType(byte id);

	protected virtual void ExOfCallback(byte id)
	{
		while ( id++ < MAX )
			if (AcceptedId(id))
				break;

		send(id, new MyDelegateType(ExOfCallback)); 
	}

	private void send(byte id, MyDelegateType myCallback)
	{
		// Example of calling the callback function
		myCallback(id);
	}

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

Which language is preferable when writing a program containing many callbacks C++ or C# ?

 

 

Could I for example write the API part as a dll in C++, and the GUI part in another language without giving any conflict ?

  • *Gurus*
Posted

Personally I'd use C#, but that's because I don't like C++.

 

You could write it in both - writing a standard C dll and using platform invoke to interop with it, or using managed C++ to create a .NET class with it. .NET delegates can interop with unmanaged callbacks just fine. I think there's a sample in the .net sdk of using delegates with the win32 function EnumWindows.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

  • Administrators
Posted

Easier than C++, no need for pointer arithmetic, Type Safe, Delegates, Interfaces, supports strings as a data type. Easier integration with other .Net code for starters.

 

Are there any reasons why you prefer C++ or need to mix two languages.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Most of all because it is the language that I have used for decades. I like the features that C++ have like polymorphism, encapsulation and inheritance. But I heard more people complaining that so many things have to be programmed that is automatically done for you like in BASIC. C++ is just in many ways much more proffessional than BASIC. But I agree that make C++ interact with other interface can be problematic. Also issues about Pointers can make your hair grow before finding out what goes wrong. I am just starting at .NET and of course unconscious

I choose C++ because that is the language I know. I have no idea of what language is the most potential. What do people prefer to code in future C# or C++. As long as I do not have to put myself into alot of new things it does not really matter whether it is C# or C++.

  • Administrators
Posted

C# gives you things like Polymorphism, Encapsulation and inheritance.

Inheritance is currently limited to single inheritance (although MI may make a future version of the framework)

Polymorphism is supported through Inheritance and interfaces (which remove a lot - but not all of the reasons for Multiple Inheritance).

If you are familiar with C++ then a lot of C# will be familiar (if statements, loop constructs, variable declaration etc)

C# can't do everything C++ can but how many people actually need to do everything C++ can?

C# on the other hand gives the developer an easier ride with system provided strings, arrays, collections etc as well as memory management (via Garbage collection)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

One problem I sometimes see, is when you have several classes

that use same function like ex. InheritTxtFunction, you can no longer inherit the function because of the windows handler is probably not point at the right window. Take for example SetWindowText. To what window should it put the text. The result is that you for every class must copy this function. How annoying. Or are there any trick to solve this problem ?

 

 

like:

 

void CDlg::InheritTxtFunction(BYTE answer)

{

char txt[10];

 

switch (answer)

case 0: MessageBox("App","Error"); break;

case 1: sprintf(txt, "Ready"); break;

default: SetwindowText("Pause);

break;

}

  • Administrators
Posted (edited)

Not sure what you mean with 'Several classes that use same function'

If this is a function common to a group of related classes then declaring it in a base class means it's available to all sub-classes (and can be over ridden if needed).

If it is more generic functionality then it could be a delegate or part of an interface.

 

Using SetWindowText as an example - this would either take a window handle as a parameter to tell it which window to put the text on, or would be a method in a class that encapsulates a window.

 

edit: typo (faulty keyboard)

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
I guess you are right that you can make such a function as an inherited one. It only require that the handler is pointing to the right window. But how would would you do that ?
Posted
Okay. I am not sure If I have understood it right but I see that the source file is missing when you make forms. You only have the forms, choose view code and here you write all your source code. What had happened with the cpp file ??
Posted

I C++ you will have a source file and a header file .Cpp and .h.

 

But in C# the source file has the extension .cs. What about the header file, I can not see any ?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...