Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MuZZy - 18 Nov 2005 03:38 GMT
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
JAL - 18 Nov 2005 06:43 GMT
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

> Hi,
>
[quoted text clipped - 25 lines]
> Thank you in advance,
> Andrey
Tomas Restrepo (MVP) - 18 Nov 2005 11:25 GMT
JAL,

> Muzzy... for the C# dll method:
>
[quoted text clipped - 15 lines]
> 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);
MuZZy - 18 Nov 2005 14:41 GMT
> JAL,
>
[quoted text clipped - 22 lines]
>    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);
Carl Daniel [VC++ MVP] - 18 Nov 2005 18:00 GMT
>> Actually, C++/CLI (it's not called MC++ 2.0, btw <g>) fully supports
>> managed vararg methods, so you can just call it:
[quoted text clipped - 3 lines]
> 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
JAL - 18 Nov 2005 22:23 GMT
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.

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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.