Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6216153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:11:01+00:00 2026-05-24T07:11:01+00:00

In my C# code, I have the following array: var prices = new[] {1.1,

  • 0

In my C# code, I have the following array:

var prices = new[] {1.1, 1.2, 1.3, 4, 5,};

I need to pass it as a parameter to my managed C++ module.

var discountedPrices = MyManagedCpp.GetDiscountedPrices(prices) ;

How should the signature of GetDiscountedPrices look like? In the most trivial case, when discounted prices are equal to prices, how should the C++ method GetDiscountedPrices look like?

Edit: I managed to get it to compile. My C# code is this:

    [Test]
    public void test3()
    {
        var prices = new ValueType[] {1.1, 1.2, 1.3, 4, 5,};
        var t = new TestArray2(prices , 5);
    }

My C++ code builds:

        TestArray2(     
        array<double^>^ prices,int maxNumDays)
    { 
        for(int i=0;i<maxNumDays;i++)
        {
// blows up at the line below
            double price = double(prices[i]);
        }

However I am getting a runtime error:

System.InvalidCastException : Specified cast is not valid.

Edit: Kevin’s solution worked. I also found a useful link:C++/CLI keywords: Under the hood

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:11:04+00:00Added an answer on May 24, 2026 at 7:11 am

    Your managed function declaration would look something like this in the header file:

    namespace SomeNamespace {
        public ref class ManagedClass {
            public:
            array<double>^ GetDiscountedPrices(array<double>^ prices);
        };
    }
    

    Here’s an example implementation of the above function that simply subtracts a hard-coded value from each price in the input array and returns the result in a separate array:

    using namespace SomeNamespace;
    
    array<double>^ ManagedClass::GetDiscountedPrices(array<double>^ prices) {
    
        array<double>^ discountedPrices = gcnew array<double>(prices->Length);
        for(int i = 0; i < prices->Length; ++i) {
            discountedPrices[i] = prices[i] - 1.1;
        }
        return discountedPrices;
    }
    

    Finally, calling it from C#:

    using SomeNamespace;
    
    ManagedClass m = new ManagedClass();
    double[] d = m.GetDiscountedPrices(new double[] { 1.3, 2.4, 3.5 });
    

    **Note that if your Managed C++ function is passing the array to a native function, it will need to marshall the data to prevent the garbage collector from touching it. It’s hard to show a specific example without knowing what your native function looks like, but you can find some good examples here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: var intrebari = new Array(); var i = 0;
I currently have the following javascript array: var stuffs = ['a', 'b']; I pass
I have the following json array of objects in my code var groups =
I have the following code: var x = Array(1,3,4,4,1,1,3) var m = Int.MaxValue x.foreach((x)=>(m
If I have the following code: var A = Array[Array[Double]]() // where A becomes
I have The following code for now. type TByte4 = array[0..3] of Byte; //
I have the following code: $postcode = $form->createElement('text', 'postcode'); $postcode->setLabel('Post code:'); $postcode->addValidator('regex', false, array('/^[a-z]{1,3}[0-9]{1,3}
I have following code class User attr_accessor :name end u = User.new u.name =
I have the following JavaScript array of real estate home objects: var json =
I have the following code in one of my personal projects: def allocate(var, value)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.