noob String::Format question

  • Thread starter Thread starter Donald
  • Start date Start date
D

Donald

I am trying print the length of a managed array, but I cannot figure out how
to correctly cast the result so String::Format will use it. I wrote a simple
test to duplicate the problem (below) -- can anybody explain to me why the
following generates an error?

int ai __gc[] = new int __gc[100];
Console::WriteLine(ai->Length);
Console::WriteLine(String::Format(S"Length of ai is {0}",
__box(ai->Length)));
The first Writeline returns the correct answer but the last line generates
an error of "Object reference not set to an instance of an object".

Thanks in advance,
don.
 
Donald said:
I am trying print the length of a managed array, but I cannot figure out how
to correctly cast the result so String::Format will use it. I wrote a simple
test to duplicate the problem (below) -- can anybody explain to me why the
following generates an error?

int ai __gc[] = new int __gc[100];
Console::WriteLine(ai->Length);
Console::WriteLine(String::Format(S"Length of ai is {0}",
__box(ai->Length)));
The first Writeline returns the correct answer but the last line generates
an error of "Object reference not set to an instance of an object".

ISTR there was a bug with the Length property in VC.NET 2002, which was
fixed in VC.NET 2003. Try using ai->get_Length() instead.
 
Thanks! I ended up doing

int i = ai->Length;

then using i in the WriteLine. Good to know that it is a bug... maybe I
should upgrade ;)

Doug Harrison said:
Donald said:
I am trying print the length of a managed array, but I cannot figure out how
to correctly cast the result so String::Format will use it. I wrote a simple
test to duplicate the problem (below) -- can anybody explain to me why the
following generates an error?

int ai __gc[] = new int __gc[100];
Console::WriteLine(ai->Length);
Console::WriteLine(String::Format(S"Length of ai is {0}",
__box(ai->Length)));
The first Writeline returns the correct answer but the last line generates
an error of "Object reference not set to an instance of an object".

ISTR there was a bug with the Length property in VC.NET 2002, which was
fixed in VC.NET 2003. Try using ai->get_Length() instead.
 
Back
Top