473,473 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1230
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matt Hollingsworth | last post by:
Hello, Very new to python, so a noob question. When I've written stuff in JavaScript or MEL in the past, I've always adopted the variable naming convention of using a $ as the first character...
1
by: kevininstructor | last post by:
The following was copied from a web site for learning formatting. Dim myInt As Integer = 100 Dim FormatPrice As String = String.Format("Price = |{0,10:c }|", myInt) Console.WriteLine(FormatPrice)...
2
by: san | last post by:
Hello, all! I have question about String.Format method. There are two variants: public static string Format(string, params object); and public static string Format(IFormatProvider, string, params...
8
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
7
by: Roger Helliwell | last post by:
Hello Everyone, Has anyone found a quick reference for the many string formatting options for string.Format() ? I'm spending a ridiculous amount of time trying to write one line of code that...
9
by: Phill | last post by:
: string.Format("MyString {0}", var); : String str = "MyString {0}"; str.Format(var); In otherwords why is Format a method of the string class but it can't
7
by: Tommy Vercetti | last post by:
The first three of these statements work. The fourth doesn't. String::Format("{0}{0}{0}{0}", S""); String::Format("{0}{1}{0}{0}", S"", S""); String::Format("{0}{1}{2}{0}", S"", S"", S"");...
2
by: Dan McCollick | last post by:
Hi All, Noob question that I can not seem to figure out: I am trying to implement a screenscraper to pull data from two seperate websites, here is some test code so far: public static void...
6
by: Scewbedew | last post by:
Suppose I have the following code: string myFormat = "Line1/nLine 2"; string formattedString = string.Format(myFormat); ....that would produce a 2-line output as expected. But if I load...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.