Wednesday, August 1, 2007

Conditions forever

Before starting AutoCAD data specific programs, I would like to put my hands on C# language specific statements like If, Switch etc. Here is a sample program covering very basic condition checking and some output formatting. Next post, I shall try to cover looping in C#.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
namespace AcadNetSamples
{
public class
Commands
{
[CommandMethod("CONDITIONS")]
public static void ConditionalOperations()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
int x = 1; int y = 2; int z = 3;
if ((x + y) == z) ed.WriteMessage("\n{0}+{1} is equal to {2}",x,y,z);
if ((z - y) != x)
{
ed.WriteMessage("\n{0}+{1} is not equal to {2}", z, y, x);
}
else
{
ed.WriteMessage("\n{0}-{1} is equal to {2}", z, y, x);
}
switch (x + y + z)
{
case 3:
ed.WriteMessage("\n{0}+{1}+{2}=3", x, y, z);
break;
case 4:
ed.WriteMessage("\n{0}+{1}+{2}=4", x, y, z);
break;
case 5:
ed.WriteMessage("\n{0}+{1}+{2}=5", x, y, z);
break;
default:
ed.WriteMessage("\n{0}+{1}+{2}={3}", x, y, z, (x + y + z));
break;
}
}
}
}

Try to experiment with the above code using different conditions. Also try to work with common logical operators like AND, OR etc. in your code.

No comments: