> I'm getting a runtime error for the below program, though it compiles
> fine. The culprit seems to be the managed array--it doesn't exist.
> What am I doing wrong?
> AClass ^A10 = gcnew AClass(); //see AClass.h, .cpp below for
>AClass::AClass(void): pubint(1),privint(1)
> {
> array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
> }
Subtle. in your constructor, you declare a local variable that has the same
name as your class member. I assume thatthis is copy and paste error.
you initialize it properly, but it hides your a1 member variable, which
remains uninitialized.
Then -in your test code- you dereference a1. since that is an unitialized
pointer, you will get an exception.
Change
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
to
a1 = gcnew array<int>(5);
for(int j=0; j<a1->Length; j++)
a1[j] = j;
to solve the problem

Signature
Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"
raylopez99 - 28 Dec 2006 08:55 GMT
Muchos gracias Bruno van Dooren! Indeed this is the case--I have never
used managed arrays before and this being the first time, I did not
know the right format.
RL
> Subtle. in your constructor, you declare a local variable that has the same
> name as your class member. I assume thatthis is copy and paste error.
[quoted text clipped - 16 lines]
> bruno_nos_pam_van_dooren@hotmail.com
> Remove only "_nos_pam"
Tamas Demjen - 02 Jan 2007 20:46 GMT
> Muchos gracias Bruno van Dooren! Indeed this is the case--I have never
> used managed arrays before and this being the first time, I did not
> know the right format.
In C++/CLI, you can initialize the array at dynamic construction, which
is a really nice feature:
a1 = gcnew array<int>(5) {1,2,3,4,5};
Tom