noob String::Format question

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.
 
D

Doug Harrison [MVP]

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.
 
D

Donald

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top