There are plenty of other useful classes contained in sub-namespaces of the System namespace. Some are covered elsewhere in this book. Four are covered here because almost every .NET developer is likely to use them: System::IO , System::Text , System::Collections , and System::Threading . The System::IO NamespaceGetting information from users and providing it to them is the sort of task that can be incredibly simple (like reading a string and echoing it back to the users) or far more complex. The most basic operations are in the Console class in the System namespace. More complicated tasks are in the System::IO namespace. This namespace includes 27 classes, as well as some structures and other related utilities. They handle tasks such as
This snippet uses the FileInfo class to determine whether a file exists, and then deletes it if it does: System::IO::FileInfo* fi = new System::IO::FileInfo("c:\test.txt"); if (fi->Exists) fi->Delete(); This snippet writes a string to a file: System::IO::StreamWriter* streamW = new System::IO::StreamWriter("c:\test.txt"); streamW->Write("Hi there" ); streamW->Close(); Be sure to close all files, readers, and writers when you have finished with them. The garbage collector might not finalize the streamW instance for a long time, and the file stays open until you explicitly close it or until the instance that opened it is finalized.
Check the documentation to learn more about IO classes that you can use in console applications, Windows applications, and class libraries. Keep in mind also that many classes can persist themselves to and from a file, or to and from a stream of XML. The System::Text NamespaceJust as Console offers simple input and output abilities , the simplest string work can be tackled with just the String class from the System namespace. More complicated work involves the System::Text namespace. You've already seen System::Text::StringBuilder . Other classes in this namespace handle conversions between different types of text, such as Unicode and ASCII. The System::Text::RegularExpressions namespace lets you use regular expressions in string manipulations and elsewhere. Here is a function that determines whether a string passed to it is a valid US ZIP code: using namespace System; using namespace System::Text::RegularExpressions; // . . . String* Check(String* code) { String* error = S" OK"; Match* m; switch (code->get_Length()) { case 5: Regex* fivenums; fivenums = new Regex("\d\d\d\d\d"); m = fivenums->Match(code); if (!m->Success) error = S" Non numeric characters in 5 digit code"; break; case 10: Regex* fivedashfour; fivedashfour = new Regex("\d\d\d\d\d-\d\d\d\d"); m = fivedashfour->Match(code); if (!m->Success) error = S"Not a valid zip+4 code"; break; default: error = S"invalid length"; } return error; } The Regex class represents a pattern, such as "five numbers " or "three letters ." The Match class represents a possible match between a particular string and a particular pattern. This code checks the string against two patterns representing the two sets of rules for ZIP codes. The syntax for regular expressions in the .NET class libraries will be familiar to developers who have used regular expression as MFC programmers, or even as UNIX users. In addition to using regular expressions with classes from the System::Text namespace, you can use them in the Find and Replace dialog boxes of the Visual Studio editor, and with ASP.NET validation controls. It's worth learning how they work. Regular Expression SyntaxA regular expression is some text combined with special characters that represent things that can't be typed, such as "the end of a string" or "any number" or "three capital letters." When regular expressions are being used, some characters give up their usual meaning and instead stand in for one or more other characters. Regular expressions in Visual C++ are built from ordinary characters mixed in with these special entries, shown in Table 3.2. Here are some examples of regular expressions:
Table 3.2. Regular Expression Entries
The System::Collections NamespaceAnother incredibly common programming task is holding on to a collection of objects. If you have just a few, you can use an array to read three integers in one line of input. In fact, arrays in .NET are actually objects, instances of the System::Array class, which have some useful member functions of their own, such as Copy() . There are times when you want specific types of collections, though, and the System::Collections namespace has plenty of them. The provided collections include
One rather striking omission here is a linked list. You have to code your own if you need a linked or double-linked list. The System::Threading NamespaceThreading has been a difficult part of Windows programming from the very beginning. It's quite a bit simpler in .NET. The vital classes for threading are in the System::Threading namespace. These include classes such as Mutex , Thread , and ThreadPool , which developers with experience in threaded applications will recognize instantly. A thread is a path of execution through a program. In a multithreaded program, each thread has its own stack and operates independently of other threads running within the same program.
A thread is the smallest unit of execution, much smaller than a process. Generally each running application on your system is a process. If you start the same application twice (for example, Notepad), there are two processes: one for each instance. It is possible for several instances of an application to share a single process: For example, if you choose File, New Window in Internet Explorer, two applications appear on your taskbar, and they share a process. The unfortunate consequence is that if one instance crashes, they all do. Writing a multithreaded application is simple with classes from the System::Thread namespace. First, you need to think of some work for a new thread to do: some slow calculation that you want to run without slowing the responsiveness of your application. This work will go into a function, and the function will be executed on a new thread.
Your thread proc must be a member function of a garbage-collected class. For example public __gc class Counter { public: void Countdown() { String* threadName = Thread::CurrentThread->Name; Console::Write(S" This is the current thread: "); Console::WriteLine(threadName); for (int counter = maxCount; counter >= 1; counter--) { Console::WriteLine(S" {0} is currently on {1}.",threadName, __box(counter)); } Console::WriteLine(S" {0} has finished counting down from {1}.", threadName, __box(maxCount)); } }; The heart of this function is the for loop that counts down from maxCount to zero. It uses the Name property of the thread that is executing the function, because the sample that uses this class calls the function on two different threads. Normally, the function that does the asynchronous work would not communicate with the user ; it would be quietly working in the background, doing some long slow work. It might, however, update a progress bar, or an icon that indicates a background task is in progress. This code calls the thread proc on a new thread and also on the application's main thread: Console::Write("Enter a number to count down from: "); maxCount = Int16::Parse(Console::ReadLine()); Thread::CurrentThread->Name = "Main Thread"; Counter* counter = new Counter(); ThreadStart* SecondStart = new ThreadStart(counter,Counter::Countdown); Thread* secondThread = new Thread(SecondStart); secondThread->Name = "Secondary Thread"; secondThread->Start(); counter->Countdown(); This code keeps the number entered by the user in a global variable called maxCount . Because ReadLine returns a pointer to a System::String instance, the static Parse() method of Int16 is used to convert a string to an integer so that it can be stored in maxCount . To execute a particular function on a new thread, you create a Thread object and call its Start() method. To create a Thread object, you need a ThreadStart object, and the constructor for ThreadStart needs a reference to an instance of a garbage-collected class ( counter in this example), and a function pointer (just the name of the function without the parentheses) to a member function of that garbage-collected class. Because this code calls Countdown on a new thread and also on the main thread, the output shows the two threads taking turns, as in Figure 3.1. Figure 3.1. A multithreaded application demonstrates how threads take turns doing their work.
|