Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Dr.Bob's Delphi Notes Dr.Bob's Delphi Clinics Dr.Bob's Delphi Courseware Manuals
 Dr.Bob Examines... #15
See Also: other Dr.Bob Examines columns or Delphi articles

Microsoft .NET and C# (Beta 1)
This month, I want to share with you my personal feelings regarding (and experiences with) Microsoft .NET architecture and the C# language. As a Delphi fan, I've always been able to keep Visual Basic out of the door, and C++Builder has always been my preferred C++ compiled (over Visual C++ for example). Somehow, I have this feeling that with the upcoming Microsoft .NET architecture, I won't be able to ignore the new C# language just as easily. For one thing, C# has been "invented" by the same man behind Borland Pascal and Delphi: Anders Hejlsberg (who was recently awarded with the Dr. Dobb's Excellence in Programming Award).

But let's take a little step back. What is .NET and how does it affect the future of Windows programming? As far as I can see (based on my reading and experience with it), .NET is an object oriented layer on top of existing Win32 APIs. And not just object oriented, but memory managed (i.e. it includes a garbage collector) and with exceptions built-in from the start! For developers, this means that instead of using the Win32 API (which is a bit different on Win95, Win98, WinNT, Windows 2000 and other flavours of Windows), we can now use a single, uniform, object oriented "API" to program the Windows platform.
This sounds nice, but apart from a uniform API it doesn't really mean much to me, yet. However, there's another benefit of using the .NET architecture, and that's the fact that any .NET compliant development environment can share .NET objects (in the form of so-called Intermediate Language or IL code). The .NET Common Language Runtime (CLR) will "execute" the IL code from the different Visual Studio.NET environments (such as Visual Basic, Visual C++ and C#). Finally, Microsoft has announced that all future versions of Windows will be based on the .NET architecture, and we can safely assume that at one point in time the "normal" Windows API will no longer be supported, meaning we then have to use the .NET "API".
The big question (to Borland) of course is: will there be a Delphi for .NET? (in time there will have to be some sort of .NET support). At this time, it seems possible to create ASP.NET objects with Delphi that can be used in a .NET environment (for example by C#).

How to get started with .NET
Getting started with .NET and C# involves more than just inserting a CD and running setup. In fact, it's a one-way process that I found to be best suited for a spare machine or a VMWare "guest operating system" installation. If you have one of those available, then you need to perform the following steps to get .NET and C# up and running:

After a few hours work (and a GByte of diskspace less), you have an installation of Microsoft's .NET architecture including Visual Studio.NET Beta 1; ready to start your first C# program.

Microsoft Visual C#
The classic application demo consists of a listBox, editbox (called textBox in C#) and Button. Drop these three components on a new form, and write one line of code for the button1_Click event handler:

  listBox1.InsertItem(0,textBox1.Text);
The result is an application where a click on button1 puts the text from textBox1 inside listBox1:

The complete source code is as follows (you only need to write one line of code yourself):

  namespace WindowsApplication1
  {
      using System;
      using System.Drawing;
      using System.Collections;
      using System.ComponentModel;
      using System.WinForms;
      using System.Data;

      /// <summary>
      ///    Summary description for Form1.
      /// </summary>
      public class Form1 : System.WinForms.Form
      {
          /// <summary>
          ///    Required designer variable.
          /// </summary>
          private System.ComponentModel.Container components;
          private System.WinForms.TextBox textBox1;
          private System.WinForms.Button button1;
          private System.WinForms.ListBox listBox1;

          public Form1()
          {
              //
              // Required for Windows Form Designer support
              //
              InitializeComponent();

              //
              // TODO: Add any constructor code after InitializeComponent call
              //
          }

          /// <summary>
          ///    Clean up any resources being used.
          /// </summary>
          public override void Dispose()
          {
              base.Dispose();
              components.Dispose();
          }

          /// <summary>
          ///    Required method for Designer support - do not modify
          ///    the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {
              this.components = new System.ComponentModel.Container ();
              this.textBox1 = new System.WinForms.TextBox ();
              this.button1 = new System.WinForms.Button ();
              this.listBox1 = new System.WinForms.ListBox ();
              //@this.TrayHeight = 0;
              //@this.TrayLargeIcon = false;
              //@this.TrayAutoArrange = true;
              textBox1.Location = new System.Drawing.Point (48, 128);
              textBox1.Text = "textBox1";
              textBox1.TabIndex = 2;
              textBox1.Size = new System.Drawing.Size (72, 20);
              button1.Location = new System.Drawing.Point (48, 48);
              button1.Size = new System.Drawing.Size (75, 23);
              button1.TabIndex = 1;
              button1.Text = "Hit Me";
              button1.Click += new System.EventHandler (this.button1_Click);
              listBox1.Location = new System.Drawing.Point (144, 40);
              listBox1.Size = new System.Drawing.Size (120, 199);
              listBox1.TabIndex = 0;
              this.Text = "Form1";
              this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
              this.Controls.Add (this.textBox1);
              this.Controls.Add (this.button1);
              this.Controls.Add (this.listBox1);
          }

          protected void button1_Click (object sender, System.EventArgs e)
          {
            listBox1.InsertItem(0,textBox1.Text);
          }

          /// <summary>
          /// The main entry point for the application.
          /// </summary>
          public static void Main(string[] args)
          {
              Application.Run(new Form1());
          }
      }
  }

A number of books are available as reading and/or reference material on the new C# language. If you're a member of MSDN, you can download Visual Studio .NET (otherwise you can order the CD-ROM).
And apart from that, there are a number of books on C# already available.

Although the Visual Studio.NET IDE is no Delphi IDE, and I still prefer Delphi (ObjectPascal) over C# as a language, I will explore the world of .NET and C# in the months to come. When Visual Studio.NET Beta 2 is available, we'll see a number of changes and things that will shape the future of Windows development. It can't be all that bad, and no matter what: it cannot be ignored.
And if it's possible to connect Delphi to the world of .NET, I'll show you how. So stay tuned, and watch Dr.Bob's C# Visions.


This webpage © 2001-2010 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.