C# function with 'params' not working in managed C++?

  • Thread starter Thread starter MuZZy
  • Start date Start date
M

MuZZy

Hi,

Maybe it's a known issue but i couldn't find any answer so far...

Consider this c# function:

// ----------------------------------
public int SumUp(params int[] iArgs)
{
int i = 0;
foreach (int iArg in iArgs)
i += iArg;
return i;
}
// ---------------------------------

So this function accepts variable number of arguments.
Now, i try to call it from a c++ project with 7 (for example) arguments and i get an error saying
that SumUp() doesn't expect 7 arguments. Basicly it doesn't work with any number of arguments...

I specifically created that helper c# project as i wanted to use functions with variable-number of
arguments in c++ and c++ (managed) itself doesn't support that. I tried v_list but it's not working
with managed c++.

Any solutions/workarounds?

I would really apreciate any ideas!
Thank you in advance,
Andrey
 
Muzzy... for the C# dll method:

namespace ClassLibrary1
{
public class Class1
{
public int TestArgs(params int[] args) {
return args.Length;
}
}
}

You can do in MC++2.0

int CallVarArgs() {
array<int,1>^ ai= gcnew array<int,1>(3){1,2,3};
ClassLibrary1::Class1^ c= gcnew ClassLibrary1::Class1;
return c->TestArgs(ai);
}

Have fun,
JAL
 
JAL,
Muzzy... for the C# dll method:

namespace ClassLibrary1
{
public class Class1
{
public int TestArgs(params int[] args) {
return args.Length;
}
}
}

You can do in MC++2.0

int CallVarArgs() {
array<int,1>^ ai= gcnew array<int,1>(3){1,2,3};
ClassLibrary1::Class1^ c= gcnew ClassLibrary1::Class1;
return c->TestArgs(ai);
}

Actually, C++/CLI (it's not called MC++ 2.0, btw <g>) fully supports managed
vararg methods, so you can just call it:

Test^ t = gcnew Test();
Console::WriteLine(t->SumUp(1,2,3,4,5,6,6));

The original MC++, however, doesn't support them, so there you *do* have to
create an array to call it:

Int32 params __gc[] = { 9,1,3 };
t->SumUp(params);
 
Tomas said:
JAL,

Muzzy... for the C# dll method:

namespace ClassLibrary1
{
public class Class1
{
public int TestArgs(params int[] args) {
return args.Length;
}
}
}

You can do in MC++2.0

int CallVarArgs() {
array<int,1>^ ai= gcnew array<int,1>(3){1,2,3};
ClassLibrary1::Class1^ c= gcnew ClassLibrary1::Class1;
return c->TestArgs(ai);
}


Actually, C++/CLI (it's not called MC++ 2.0, btw <g>) fully supports managed
vararg methods, so you can just call it:
Test^ t = gcnew Test();
Console::WriteLine(t->SumUp(1,2,3,4,5,6,6));
I'm a bit confised - is it (above) supported in .NET 1.1 or only from .NET 2.0?
The original MC++, however, doesn't support them, so there you *do* have to
create an array to call it:

Int32 params __gc[] = { 9,1,3 };
t->SumUp(params);
 
MuZZy said:
Tomas Restrepo (MVP) wrote:
I'm a bit confised - is it (above) supported in .NET 1.1 or only from .NET
2.0?

Only VC++ 2005/.NET 2.0.

-cd
 
You are right, of course. I swear I tried it and I got a compiler error, but
of course now it compiles just fine :).

Personally I like MC++2.0 over C++/CLI. I find it descriptive and useful. It
implies managed over unmanaged C++ and it implies targeting the NET 2.0
framework as opposed to 1.0 or 1.1.

:
 
Back
Top