Testing a Managed Class with a Managed Test Harness

The managed test harness in this chapter tests the same unmanaged class, Order , as the unmanaged test harness didjust to prove that managed code doesn't have to use managed data. In addition, a simple managed class demonstrates how to work with managed data from managed code.

Start by copying (use Windows Explorer) order.cpp and order.h into the project folder of the ManagedConsole project. Then right-click the ManagedConsole project in Solution View, and choose, Add, Add Existing Item. A file dialog box appears, displaying the project folder. Select order.cpp and order.h , and then click Open.

Because the Order class overloads the << operator but does not have any other way of displaying its contents, the test harness will have to use cout and the << operator rather than Console::WriteLine() . There is no problem combining the two ways of writing to the screen. (Although I recommend you use the endl or flush manipulators liberally when using cout , to ensure that none of your output is left in a buffer when WriteLine() starts writing to the screen.)

Copy the main() function from FirstConsole to ManagedConsole , and add the #include statement at the top that brings in order.h . Compile and run the application to make sure everything has been copied correctly.

In Solution view, right-click the ManagedConsole project and choose Add, Add Class. Select Generic C++ Class on the right, and click Open. In the Generic C++ Class wizard, name the class Purchaser and click Finish.

The class that is generated for you is not garbage collected. In Purchaser.h , change this line

 class Purchaser 

to this

 public __gc class Purchaser 

Note that the __gc keyword starts with two underscores. Now Purchaser is a garbage-collected class, as discussed in Chapter 1.

The definition of the class looks like Listing 2.3, and the implementation is in Listing 2.4. Notice that instead of an overload of the << operator, Purchaser includes an overload of the ToString method, which all managed classes inherit from Object . (Even when you don't specify a base class, all managed classes inherit from Object , and Object declares the ToString method.) This makes it simple to pass a Purchaser instance to Console::WriteLine() , because that method will try converting the instance to a String by calling ToString() . Notice that the constructors use initializer syntax: This is still C++, so you have all the capabilities and conveniences of the language.


If you're working along, don't forget to include any #using and using statements you see in this sample code. Without them, this code won't compile.

Listing 2.3 Definition of the Purchaser Class
 #pragma once using namespace System; public __gc class Purchaser { private:     String* name;     int account; public:     Purchaser(void);     Purchaser(String* n, int a);     ~Purchaser(void);     String* ToString(); }; 
Listing 2.4 Implementation of the Purchaser Class
 #include "StdAfx.h"  #include ".\purchaser.h"  #using <mscorlib.dll> Purchaser::Purchaser(void) : name(S""),account(-1) { } Purchaser::Purchaser(String* n, int a) : name(n),account(a) { } Purchaser::~Purchaser(void) { } String* Purchaser::ToString() {     return String::Concat(name, " ", account.ToString()); } 

Adding these lines to the main function exercises the Purchaser and Order classes:

 Purchaser* blankpurchaser = new Purchaser(); Console::WriteLine(blankpurchaser); Purchaser* filledpurchaser = new Purchaser(S" Joe Customer", 456); Console::WriteLine(filledpurchaser); 

The output from these lines looks like this:

 -1 Joe Customer 456 

Notice that the ToString() method was called by Console::WriteLine() .

This simple managed test harness proves that the unmanaged class can be called from managed code, and works just as it did when called from managed code. It also proves that the constructors for the managed class work correctly. Of course, you don't need to delete the instances of Purchaser ; the garbage collector will take care of them.

Because ManagedConsole is a managed application, you can copy the .EXE file (the assembly) to any other machine with the .NET Framework installed, and it will run there without any problem. The only libraries it needs are those installed with the framework. If you created a more complex test harness, you would need to deploy any assemblies, but typically they would just be copied into the same folder as the test harness. Large managed applications are much easier to deploy than their unmanaged equivalents. And of course, it can even be possible to the copy an executable to a non-Windows machineas long as that machine has the .NET Framework. As of this book's writing, the versions of the .NET Framework for non-Windows machines are severely limited, but they do exist and would be able to execute the test harness shown in this chapter on a Mac or UNIX machine without recompiling the C++ source.

Microsoft Visual C++. NET 2003 Kick Start
Microsoft Visual C++ .NET 2003 Kick Start
ISBN: 0672326000
EAN: 2147483647
Year: 2002
Pages: 141
Authors: Kate Gregory

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net