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

  • SEARCH
  • Home
  • 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 8490759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:13:09+00:00 2026-06-10T22:13:09+00:00

I have an unmanaged class, which I’m calling from a C++ Windows Form (managed

  • 0

I have an unmanaged class, which I’m calling from a C++ Windows Form (managed class). However, I would like to re-write this class as a ref class, but I’m not sure how to deal with global array members that are declared within the unmanaged class.

As an example I wrote a very simple class, that shows somehow what I need to do.

public class test {

private:
    int myArray[5][24]; 

public:
int assign(int i){
    test::myArray[2][4] = i;
    return 0;
}

int dosomething(int i){
    return test::myArray[2][4] + i;
}

In here I have a global member array, which I want to be able to access from all the functions within the class.

In the windows form I have a button and a combo box. This way when the button is pressed it just calls the functions in the class and displays the results.

private: System::Void thumbButton_Click(System::Object^  sender, System::EventArgs^  e) {

    test my_class;

    my_class.assign(5);
comboBox1->Text = my_class.dosomething(6).ToString();
}

Now if I try to change the class to a ref class, there is an error because of the global array being unmanaged. I tried doing this with std::vectors, which is a better approach than using the array directly, but get the same error. Therefore, I would really appreciate if someone can point me to a way to rewrite this class as a ref class. Thank you!

  • 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-06-10T22:13:11+00:00Added an answer on June 10, 2026 at 10:13 pm

    I don’t think ‘global’ is the right word for your unmanaged array, as it is contained within the unmanaged class definition. The unmanaged array doesn’t have the static keyword either, so it’s an instance variable, which is nowhere near global.

    Regardless, it seems the issue you’re having is with the array definition. int myArray[5][24] is an unmanaged ‘object’, which can’t be directly included in your managed class. (You can have pointers to unmanaged objects, but not inline unmanaged objects.) You could switch that to a pointer to an integer array, and deal with malloc & free, but it’s much simpler to go with a managed array instead.

    Here’s the syntax to declare that array as managed:

    public ref class test
    {
    private:
        array<int, 2>^ myArray;
    
    public:
        test()
        {
            this->myArray = gcnew array<int, 2>(5, 24);
        }
    
        int assign(int i)
        {
            this->myArray[2,4] = i;
            return 0;
        }
    
        int dosomething(int i)
        {
            return this->myArray[2,4] + i;
        }
    };
    

    The array class is templated on the data type and the number of dimensions, so for a 2D array of integers, array<int, 2> is what you want.

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

Sidebar

Related Questions

I have an unmanaged library which I want to use from a managed class.
I have an unmanaged COM object, and I would like to call it from
I have a managed class which contains unmanaged class pointer: class Managed { public
I have an unmanaged DLL which I'm currently calling from C# using a COM
I have an unmanaged (C/C++) DLL which I need to call from a C#
I have an unmanaged C++ class which has a com map inside of it.
In my code, I have a class which create a new thread. This new
I have a class library which is called through COM from a Third Party
I have a C++ unmanaged class NativeDog that needs to be used from C#,
I have a Status class in C#, used like this: Status MyFunction() { if(...)

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.