471,594 Members | 2,045 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

noob String::Format question

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.

Nov 16 '05 #1
2 1179
Donald wrote:
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.
--
Doug Harrison
Microsoft MVP - Visual C++
Nov 16 '05 #2
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 [MVP]" <ds*@mvps.org> wrote in message
news:b8********************************@4ax.com...
Donald wrote:
I am trying print the length of a managed array, but I cannot figure out howto correctly cast the result so String::Format will use it. I wrote a simpletest to duplicate the problem (below) -- can anybody explain to me why thefollowing 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 generatesan 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.
--
Doug Harrison
Microsoft MVP - Visual C++

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Matt Hollingsworth | last post: by
1 post views Thread by kevininstructor | last post: by
2 posts views Thread by san | last post: by
8 posts views Thread by Ivan Shevanski | last post: by
7 posts views Thread by Roger Helliwell | last post: by
9 posts views Thread by Phill | last post: by
7 posts views Thread by Tommy Vercetti | last post: by
2 posts views Thread by Dan McCollick | last post: by
6 posts views Thread by Scewbedew | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.