Sunday, August 5, 2007

Iterations in C#

As I mentioned in the last post, in this session, we are going to cover the loop structures in C#. Our primary aim is to concentrate more on the syntax of loops. Here is a sample which shows iteration results in AutoCAD prompt using different looping methods.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
namespace AcadNetSamples
{
public class Commands
{
[CommandMethod("LOOPS")]
public static void LoopOperations()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
string[] strs = new string[10];
int i;
for (i = 0; i < 10; i++)
{
strs[i] = "String No. " + (i + 1).ToString();
}
ed.WriteMessage("\n\nUsing 'foreach' Loop\n");
foreach (string str in strs)
{
ed.WriteMessage("\n\t\t" + str);
}
ed.WriteMessage("\n\nUsing 'While' Loop\n");
i = 0;
while (i < 10)
{
ed.WriteMessage("\n\t\tString No. " + (i + 1).ToString());
i++;
}
ed.WriteMessage("\n\nUsing 'Do While' Loop\n");
i = 0;
do
{
ed.WriteMessage("\n\t\tString No. "+ (i + 1).ToString());
i++;
} while (i < 10);
}
}
}

Next session, I am planning to start with AutoCAD prompt operations. Like an online streaming buffer, I have to keep a little ahead of my posts :-). So the blog post interval will depend on how quickly I learn new things. I highly appreciate any valuable contributions from the readers.

No comments: