473,847 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create an array of delegates using managed C++? (within aWindows Form class)

I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that
form I'm trying to invoke some static functions by using an array of function pointers (delegates). I assume I need to
use the array< T keyword to allocate an array of delegates, and then initialize the array by setting each array
element to the pointers (handles) of the functions I'll be invoking.

I've been trying to figure this out for several hours, and can't find any examples of how to create and initialize an
array of function delegates in managed C++. I'm hoping someone can post a tiny example that actually works. :-)

Below is an example of what I'm trying to do. I want to be able to invoke the functions TestFunc1(), TestFunc2() and
TestFunc3() through an array of delegates to those functions. In the example, I show that I'm able to create a single
delegate TestFunc that points at TestFunc1(), but when I try to create an array of delegates, I get compiler errors
shown in the comments below:

- - Start of code excerpt - - - - -

using namespace System;
using namespace System::Compone ntModel;
using namespace System::Collect ions;
using namespace System::Windows ::Forms;
using namespace System::Data;
using namespace System::Drawing ;

namespace MyApp {

public ref class MyForm : public System::Windows ::Forms::Form
{

// ...snip... Designer-created initialization code for the Windows Form

private:

// Declare the delegate to a test function that takes two integers and returns nothing:
delegate void TestFuncDelegat e (int x, int y);

// As a demonstration, instantiate one delegate to TestFunc1():
// (This compiles correctly.)
static TestFuncDelegat e ^TestFunc = gcnew TestFuncDelegat e( TestFunc1 );

// Now try to instantiate an array of three delegates to TestFunc1(), TestFunc2()
// and TestFunc3():
//
// At compile time, this gives error C3374 and three error C2440s:
// error C3374: can't take address of 'MyApp::MyForm: :TestFunc1' unless creating delegate instance
// error C2440: 'initializing' : cannot convert from 'void (__clrcall*)(in t,int)' to
MyApp::MyForm:: TestFuncDelegat e ^'
//
static array<TestFuncD elegate ^^TestFuncArray = gcnew array<TestFuncD elegate ^{
TestFunc1,
TestFunc2,
TestFunc3
};

// Define the three functions we'll call using the delegate:
static void TestFunc1( int x, int y) { return; }
static void TestFunc2( int x, int y) { return; }
static void TestFunc3( int x, int y) { return; }

// ...snip...

} // class Charting
} // namespace MyApp
- - End of code excerpt - - - - -

So I get the error "can't take address of 'TestFunc1' unless creating delegate instance". Maybe I'm wrong in trying to
use the array<keyword? Or do I have some dereferencing error?

As a 2nd attempt, I'm able to successfully allocate an array of delegates this way:

static array<TestFuncD elegate ^^TestFuncArray = gcnew array<TestFuncD elegate ^>(3);

....but I can't figure out how to initialize the TestFuncArray[] at run-time. When I try something like this in the
initialization code for my form:

TestFuncArray[0] = &TestFunc1;

....I get these two compiler errors:

error C3867: 'MyApp::MyChart ::TestFunc1': function call missing argument list; use '&MyApp::MyForm ::TestFunc1' to
create a pointer to member
error C2440: '=' : cannot convert from 'void (__clrcall MyApp::MyForm:: * )(int,int)' to
'MyApp::MyForm: :TestFuncDelega te ^'

Any help is appreciated,

David K in San Jose
Sep 19 '08 #1
3 4850
Sorry, I made a minor screw-up in my posted example; below is a correction. (I had changed the names of my namespace and
class for the posting).

Near the end of my post, I had written:
...I get these two compiler errors:

error C3867: 'MyApp::MyChart ::TestFunc1': function call missing
argument list; use '&MyApp::MyForm ::TestFunc1' to create a pointer to
member
error C2440: '=' : cannot convert from 'void (__clrcall
MyApp::MyForm:: * )(int,int)' to 'MyApp::MyForm: :TestFuncDelega te ^'

Any help is appreciated,
The first error message should have said, "MyApp::MyForm" , not "MyApp::MyChart ". The corrected error msg is:

error C3867: 'MyApp::MyForm: :TestFunc1': function call missing argument list; use '&MyApp::MyForm ::TestFunc1' to
create a pointer to member

Thanks,

David K in San Jose

Sep 19 '08 #2
Problem fixed! I refused to give up, read some online C++/CLI tutorials (!), and figured out what I was doing wrong. I
was incorrectly instantiating the delegate array elements by not using the gcnew keyword.

For any other C++/CLI (i.e. "managed C++") newbies out there, here's the way I got the array of delegates initialized
properly:

In my original attempt, I was using this to instantiate the elements in my array of delegates:

static array<TestFuncD elegate ^^TestFuncArray = gcnew array<TestFuncD elegate ^{
TestFunc1,
TestFunc2,
TestFunc3
};

But I should have had this:

static array<TestFuncD elegate ^^TestFuncArray = gcnew array<TestFuncD elegate ^{
gcnew TestFuncDelegat e(TestFunc1),
gcnew TestFuncDelegat e(TestFunc2),
gcnew TestFuncDelegat e(TestFunc3)
};
....So now the error messages I was getting made perfect sense, but I was too green to understand how to fix the problem
until I studied up on managed arrays<and delegates. :-)

Thanks to any of those who started to look into this for me.

David K in San Jose
Sep 20 '08 #3
David K in San Jose wrote:
Problem fixed! I refused to give up, read some online C++/CLI
tutorials (!), and figured out what I was doing wrong. I was
incorrectly instantiating the delegate array elements by not using
the gcnew keyword.
For any other C++/CLI (i.e. "managed C++") newbies out there, here's
the way I got the array of delegates initialized properly:

In my original attempt, I was using this to instantiate the elements
in my array of delegates:
static array<TestFuncD elegate ^^TestFuncArray = gcnew
array<TestFuncD elegate ^{ TestFunc1,
TestFunc2,
TestFunc3
};

But I should have had this:

static array<TestFuncD elegate ^^TestFuncArray = gcnew
array<TestFuncD elegate ^{ gcnew TestFuncDelegat e(TestFunc1),
gcnew TestFuncDelegat e(TestFunc2),
gcnew TestFuncDelegat e(TestFunc3)
};
...So now the error messages I was getting made perfect sense, but I
was too green to understand how to fix the problem until I studied up
on managed arrays<and delegates. :-)
Yup. There's no implicit conversion from the name of a function to a
delegate type in C++ (there is in C# though), so you have to explicitly
write each constructor call.
>
Thanks to any of those who started to look into this for me.

David K in San Jose

Oct 9 '08 #4

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

Similar topics

2
2043
by: AJ | last post by:
Ok, I have a "form" class, which I am going to use to generate HTML forms. I also have a form_input, which I use to generate the individual HTML input elements. What I want to do is some how manage the instances of the form_input class, within the form class. There should be no problem with this, but I can't work out how I can store an unspecified number of instances of one
3
2148
by: M.D. | last post by:
Hi, I am pretty new on php, and have been trying to solve a problem I am having for the past 2 days. I have a HTML page which is going to have a form and some of the fields in the form will work like a table to allow the user to enter up to 15 lines on information on 4 columns, this application is to allow someone to enter a recipe into a database, so we will have 15 lines to allow people to enter: Quantity, Mesasure, Ingredient, and a...
3
6791
by: CES | last post by:
All, Is their a way of iterating thru each tag within a form and returning the value given in the id property, by that I mean the below html would return the values idBoxOne, idBoxTwo, idBoxThree from the FormXX form. <form name="FormXX" method="post" action="default.htm"> <input id="idBoxOne" name="bxOne" type="text"> <input id="idBoxTwo" name="bxTwo" type="text"> <input id="idBoxThree" name="bxThree" type="text">
4
4873
by: Ron Rohrssen | last post by:
I want to show a dialog and when the form (dialog) is closed, return to the calling form. The calling form should then be able to pass the child form to another object with the form as a parameter. For example, FormOptions formOptions = new FormOptions(); if (formOptions.ShowDialog(this) == DialogResult.OK) {
1
4452
by: Kevin | last post by:
All samples in Micoroft just mention that if a managed type is used as a member in nongc class, for example: gcroot<String*> is used to define a String managed member variable. What if the managed object in unmanaged class is just defined in a member function instead of a member variable. For example: // managed imports #using <mscorlib.dll> using namespace System;
5
1736
by: Andrew | last post by:
I want to use a managed c++ class from an unmanaged class. Here is my code: // *** Unmanaged Code // .h file class UnmanagedClass { public: // Other stuff here
3
2975
by: Bob Day | last post by:
VS 2003, vb.net, sql msde... The help is pretty empatic that you cannot pass parameters to a thread. The sample below is from help, showing you to set up variables in the TasksClass, and assign their value from sub DoWork where you start the thread. This approach works, of course. Why can't you simply add a Sub New to the TaskClass below, and then in sub DoWork modify the threading line with the sub new arguments needed like this: ...
9
2077
by: Herby | last post by:
Is possible to have a managed method within a Native(un-managed) class within a \clr project? E.g. class myClass { public: #pragma managed void myMethod(void);
9
3126
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for this, I want to use it. Is it possible to call Managed class functions from Unmanaged class? How to do it? I did something like this. I declared a managed class (in C++ CLI) called as MyManagedClass whose
0
10982
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...
0
10645
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10706
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,...
1
7880
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7053
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
5909
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4523
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
4116
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3160
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.