- Libraries Shared Across Languages
- Namespaces in C++
- The System Namespace
- Other Useful Namespaces
- In Brief
You'll use the classes in the System namespace in almost every .NET application. All the data types are represented, for example. Two classes in particular deserve special mention: System::Console and System::String.
The System::Console Class
System::Console, called System.Console in the documentation, represents the screen and keyboard in a simple console application. After you add a using statement to your file so you don't have to type System:: every time, the Console class is simple to use. To write a line of text to the screen, you use the static function WriteLine():
Console::WriteLine("Calculations in Progress");
If you want to write text without a following line break, use the Write() function instead.
To read a line of text from the keyboard, first you should write out a line to prompt the users, and then read the entire line into a System::String object with the static ReadLine() function:
Console::WriteLine("Enter a sentence:"); String* sentence = Console::ReadLine();
If you want to read in something more complicated than a single string, there really isn't any support for it within the Console class, nor the classes in the System::IO namespace covered later in this chapter. You can read it into a string and then use string member functions to separate it into the pieces you want.
If you want to write formatted output, there's an overload of WriteLine that's reminiscent of printf(), except that you don't have to tell the function the type of each parameter. For example
Console::WriteLine("The time is {0} at this moment", System::DateTime::Now.ToShortTimeString() );
You can write out a number of parameters at once. Use the placeholders, the things in brace brackets in the format string, to call for the parameter you want. As you can see, the count is zero-based. Here's an example:
Console::WriteLine("{0} lives at {1}", name, address);
The System::String and System::Stringbuilder Classes
The String class represents a string, such as "Hello" or "Kate Gregory". That's familiar ground for any programmer. But working with .NET strings can be quite strange for an experienced C++ or MFC programmer. They are certainly very far removed from the arrays of characters that you might be used to working with.
If you've ever worked with the MFC class CString, you've probably written code like this:
CString message = "Value of x, "; message += x; message += "is over limit.";
You might guess that the .NET equivalent would be
String* message = "Value of x, "; message += x; message += "is over limit.";
This just gets you a lot of strange compiler errors about illegal pointer arithmetic. The message variable is a pointer to a String instance, so you can't use the + operator. You can't do it in a single line either, like this:
String* message = "Value of x, " + x + "is over limit.";
The bottom line is that you can't treat .NET strings like C++ or C strings. So how do you build a string from several substrings, or from several pieces in general? If you want to build it so you can write it out, forget building the string, and use formatted output as described in the previous section. Or use the Format() method, which is reminiscent of sprintf(), and of the Format() method of the old MFC class CString. But if you need to build a string in little bits and pieces, your best choice is a companion class called StringBuilder, from the System::Text namespace. You use a string builder like this:
String* name = "Kate"; System::Text::StringBuilder* sb = new System::Text::StringBuilder("Hello "); sb->Append(name);
Using a string builder is more efficient than modifying a string as you go, because .NET strings actually can't be modified; instead, a whole new one is created with your changes, and the old one is cleaned up later. StringBuilder has all sorts of useful methods like Append(), Insert(), Remove(), and Replace() that you can use to work on your string. When it's ready, just pass the string builder object to anything that's expecting a string:
Console::WriteLine(sb);
The framework gets the built string from the string builder and passes it to the function for you.
The String class has its own useful methods too. Consider the problem mentioned earlierreading something other than a single string from the keyboard. The easiest way for you to tackle the problem is to write code that reads the line of input into a string, and then works with it. Here's a simple example:
Console::WriteLine("Enter three integers:"); String* input = Console::ReadLine(); String* numbers[] = input->Split(0); int a1 = Convert::ToInt32(numbers[0]); int a2 = Convert::ToInt32(numbers[1]); int a3 = Convert::ToInt32(numbers[2]);
This code uses the Split() member function of the String class. It splits a String into an array of strings based on a separator character. If you pass in a null pointer (0), as in this example, it splits the string based on whitespace such as spaces or tabs, which is perfect for this situation. The ToInt32() method of the Convert class converts a String to an integer.
If you already know how to manipulate strings, you might appreciate a quick "cheat sheet" for the String class. Table 3.1 is just such a summary.
Table 3.1 String Functions
C Runtime |
MFC CString |
System::String |
strcpy |
operator= |
operator= |
strcat |
operator+= |
Append |
strchr |
Find |
IndexOf |
strcmp |
operator == or Compare |
Compare |
strlen |
GetLength() |
Length |
strtok |
n/a |
Split |
[] |
[] or GetAt() |
Chars |
sprintf |
Format |
Format |
n/a |
Left or Right or Mid |
Substring |
If you've worked with strings in other languages, you'll appreciate System::String functions such as PadLeft(), PadRight(), Remove(), and StartsWith(). If those names aren't familiar to you, check the Visual C++ documentation. You might be able to do what you want with a single function call!
The System::DateTime Structure
The DateTime structure represents a date or a time, or both. It has a number of useful constructors to create instances using a numeric date and time, or a number of ticks (useful when you're working with older C++ code). Here are some examples:
DateTime defaultdate; Console::WriteLine(defaultdate.ToLongDateString()); DateTime Sept28(2003,9,28,14,30,0,0); Console::Write(Sept28.ToShortDateString()); Console::Write(S" "); Console::WriteLine(Sept28.ToShortTimeString()); DateTime now = DateTime::Now; Console::WriteLine(now.ToString());
It can be intimidating to remember the parameters to the seven-integer constructor, but it's simple when you realize they go from largest to smallest: year, month, day, hour, minute, second, and millisecond. The millisecond parameter is optional and if you want, you can omit all the time parameters completely.
On September 17, 2003, this code produces the following output:
Monday, January 01, 0001 9/28/2003 2:30 PM 9/17/2003 1:17:41 PM
It matters that DateTime is a structure, not a class, because it is managed data. Managed classes can only be allocated on the heap with new; managed structures can only be allocated on the stack as in these examples.
To get the individual parts of a date, use these properties:
Day: The day of the month
Month: 1 to 12
Year: Always four digits
Hour: 0 to 23
Minute
Second
DayOfWeek: 0 means Sunday
Format: Creates a string based on the time and date, using a format string
The format string passed to Format() is either a single character representing one of a number of "canned" formats, or a custom format string. The most useful canned formats include
d: A short date, such as 12/19/00
D: A long date, such as Tuesday, December 19, 2000
f: A full time and date, such as Tuesday, December 19, 2000 17:49
g: A general time and date, such as 12/19/00 17:49
s: A sortable time and date, such as 2000-12-19 17:49:03
t: A short time, such as 17:49
T: A long time, such as 17:49:03
If none of the canned formats has what you need, you can make your own by passing in strings such as "MMMM d, yy" for "December 3, 02" or whatever else you desire. You can find all the format strings in the help installed with Visual Studio.