Advertisement

Managed Arrays in .NET

Started by March 13, 2002 11:38 PM
9 comments, last by newerakb 22 years, 6 months ago
Just picked up C++ .NET, and while trying to familiarize myself with it, I attempted to build a simple calculator. Since it''s tedious to individually make each number button, i attempted to use an array. The array was within a managed class, and I used the following syntax: Button* number[] = new Button*[10]; Using that syntax the error said that only static variables can be cast in a managed class. HELP! I''ll need to learn sometime, so does anyone know the correct syntax I should use to create this array of buttons? Thanks
You could try this:


  Button* number = new Button[10];  


I don''t know if this solves your problem as I don''t know .NET, but it works in VC 6.
Problems with Windows? Reboot! - Problems with Linux? Be root!
Advertisement
Imagine this :
TYPE* p = new TYPE[10];

If TYPE is Button*, then it should be :
Button** p = new Button*[10];

----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com
What I wrote previously is for normal C++. I hope it works the same way with managed C++.

----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com
Just curious, what''s a "managed array"? Don''t know much about .net.

I see you''re doing:
new Button[10];

but that just looks like a plain old array.

-M
In managed C++ (C++ for .NET), all dynamic memory allocation is handled by the garbage collector, including any reference counting etc.

What it means is that you don''t have to delete your memory yourself. (Like in VB, for example)

I like the idea, but I prefer traditionnal C++. But this is off topic.

----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com
Advertisement
quote: Original post by Blaster
In managed C++ (C++ for .NET), all dynamic memory allocation is handled by the garbage collector, including any reference counting etc.

Garbage collection and reference counting are two different things(although they both aim towards the same goal). The CLR is garbage collected, it is not reference counted.

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Thanks for the tips. I don''t really need a managed array, specifically. The class that the array is created in is managed, which is what causes the error. The syntax I used works in a normal class, but not in a managed one. I tried using that syntax you guys suggested, as follows:

Button* blah = new Button*[10];

But again, I got the following error:

: error C3845: ''CalcForm::blah'' : only static data members can be initialized inside a __gc class or value type

So does this mean I''m screwed, or could I possibly get around this. Thanks
I dont think your problem is related to the declaration as such - looks like you are trying to initialize it inline in the class declaration. Try putting the initialization in the class constructor instead.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
The following compiles for me:

  __gc class Test{	Button* blah[];	Test()	{ 		blah = new Button*[ 10 ];	}};  

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement