PageRenderTime 30ms CodeModel.GetById 22ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/csharp/class/runme.cs

#
C# | 66 lines | 34 code | 17 blank | 15 comment | 1 complexity | 0868d3610ff08de6ef2d88e81a294d9a MD5 | raw file
 1// This example illustrates how C++ classes can be used from C# using SWIG.
 2// The C# class gets mapped onto the C++ class and behaves as if it is a C# class.
 3
 4using System;
 5
 6public class runme
 7{
 8    static void Main() 
 9    {
10        // ----- Object creation -----
11
12        Console.WriteLine( "Creating some objects:" );
13
14        using (Square s = new Square(10))
15        using (Circle c = new Circle(10))
16        {
17            Console.WriteLine( "    Created circle " + c );
18            Console.WriteLine( "    Created square " + s );
19
20            // ----- Access a static member -----
21
22            Console.WriteLine( "\nA total of " + Shape.nshapes + " shapes were created" );
23
24            // ----- Member data access -----
25
26            // Notice how we can do this using functions specific to
27            // the 'Circle' class.
28            c.x = 20;
29            c.y = 30;
30
31            // Now use the same functions in the base class
32            Shape shape = s;
33            shape.x = -10;
34            shape.y = 5;
35
36            Console.WriteLine( "\nHere is their current position:" );
37            Console.WriteLine( "    Circle = (" + c.x + " " + c.y + ")" );
38            Console.WriteLine( "    Square = (" + s.x + " " + s.y + ")" );
39
40            // ----- Call some methods -----
41
42            Console.WriteLine( "\nHere are some properties of the shapes:" );
43            Shape[] shapes = {c,s};
44            //            for (int i=0; i<shapes.Size; i++)
45            for (int i=0; i<2; i++)
46            {
47                Console.WriteLine( "   " + shapes[i].ToString() );
48                Console.WriteLine( "        area      = " + shapes[i].area() );
49                Console.WriteLine( "        perimeter = " + shapes[i].perimeter() );
50            }
51
52            // Notice how the area() and perimeter() functions really
53            // invoke the appropriate virtual method on each object.
54
55            // ----- Delete everything -----
56
57            Console.WriteLine( "\nGuess I'll clean up now" );
58
59        }
60        // Note: when this using scope is exited the C# Dispose() methods 
61        // are called which in turn call the C++ destructors
62
63        Console.WriteLine( Shape.nshapes + " shapes remain" );
64        Console.WriteLine( "Goodbye" );
65    }
66}