PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/rel-1-3-24/SWIG/Examples/csharp/class/runme.cs

#
C# | 66 lines | 34 code | 17 blank | 15 comment | 1 complexity | 0868d3610ff08de6ef2d88e81a294d9a MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. using System;
  4. public class runme
  5. {
  6. static void Main()
  7. {
  8. // ----- Object creation -----
  9. Console.WriteLine( "Creating some objects:" );
  10. using (Square s = new Square(10))
  11. using (Circle c = new Circle(10))
  12. {
  13. Console.WriteLine( " Created circle " + c );
  14. Console.WriteLine( " Created square " + s );
  15. // ----- Access a static member -----
  16. Console.WriteLine( "\nA total of " + Shape.nshapes + " shapes were created" );
  17. // ----- Member data access -----
  18. // Notice how we can do this using functions specific to
  19. // the 'Circle' class.
  20. c.x = 20;
  21. c.y = 30;
  22. // Now use the same functions in the base class
  23. Shape shape = s;
  24. shape.x = -10;
  25. shape.y = 5;
  26. Console.WriteLine( "\nHere is their current position:" );
  27. Console.WriteLine( " Circle = (" + c.x + " " + c.y + ")" );
  28. Console.WriteLine( " Square = (" + s.x + " " + s.y + ")" );
  29. // ----- Call some methods -----
  30. Console.WriteLine( "\nHere are some properties of the shapes:" );
  31. Shape[] shapes = {c,s};
  32. // for (int i=0; i<shapes.Size; i++)
  33. for (int i=0; i<2; i++)
  34. {
  35. Console.WriteLine( " " + shapes[i].ToString() );
  36. Console.WriteLine( " area = " + shapes[i].area() );
  37. Console.WriteLine( " perimeter = " + shapes[i].perimeter() );
  38. }
  39. // Notice how the area() and perimeter() functions really
  40. // invoke the appropriate virtual method on each object.
  41. // ----- Delete everything -----
  42. Console.WriteLine( "\nGuess I'll clean up now" );
  43. }
  44. // Note: when this using scope is exited the C# Dispose() methods
  45. // are called which in turn call the C++ destructors
  46. Console.WriteLine( Shape.nshapes + " shapes remain" );
  47. Console.WriteLine( "Goodbye" );
  48. }
  49. }