Connecting Tech Pros Worldwide Help | Site Map

Having problems initializing a managed array

raylopez99
Guest
 
Posts: n/a
#1: Dec 28 '06
Hi all,

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?

RL

// OverL2_simple.cpp : main project file.

#include "stdafx.h"
#include "AClass.h"

using namespace System;

int main(array<System::String ^^args)
{
Console::WriteLine(L"Hello World");

AClass ^A10 = gcnew AClass(); //see AClass.h, .cpp below for
declaration/definition
for (int j=0; j<(A10->a1->Length); j++){
Console::WriteLine("a1 is: {0}", A10->a1[j]);
}

// Unhandled Exception: System.NullReferenceException: Object
reference not set to an instance of an object.
return 0;
}
//////////////////////////////////////////////////////////////////////////////////
// Aclass.h
#pragma once
using namespace System;

ref class AClass
{
public:
AClass(void);
int pubint;
array<int>^ a1; //this array is causing problems
private:
int privint;

};
//////////////////////////////////////////////
// Aclass.cpp
#include "StdAfx.h"
#include "AClass.h"

//
AClass::AClass(void): pubint(1),privint(1)
{
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
}

/*
//compiles but give runtime error:
Unhandled Exception: System.NullReferenceException: Object reference
not set to an instance of an object.
*/

Bruno van Dooren [MVP VC++]
Guest
 
Posts: n/a
#2: Dec 28 '06

re: Having problems initializing a managed array


I'm getting a runtime error for the below program, though it compiles
Quote:
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
Quote:
>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
--

Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"


raylopez99
Guest
 
Posts: n/a
#3: Dec 28 '06

re: Having problems initializing a managed array


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
Bruno van Dooren [MVP VC++] wrote:
Quote:
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
--
>
Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"
Tamas Demjen
Guest
 
Posts: n/a
#4: Jan 2 '07

re: Having problems initializing a managed array


raylopez99 wrote:
Quote:
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
Closed Thread


Similar .NET Framework bytes