Friday, September 17, 2004

The code that is automatically created when you start an application with C#

When you launch C# Builder you get an automatic code just exactly any Borland RAD tools like J Builder. However, their code is simply optmized. I just launched it and want to comment the code, this will help me and perhaps you to get it:

First, these imports make a call for the proper classes:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

The solution is to put your project in a namespace, rather than naming it after the class name. MS and SUN do it because they think applications lie in the web so that it is possible that two applications have the same name.

Note the comments using /// and XML code, I didn't know this was possible, but it makes sense. I usually document my codes using XML, in hope that an ALIEN in the future parses my code :-)

namespace Gui_Sharp
{
///
/// Summary description for WinForm.
///

public class WinForm : System.Windows.Forms.Form
{
///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

WinForm is derived from the class Form, which is basically the typical way of doing it
in MFC or Swing, you have a top container and derives it.

It looks like that you may free memory from unused components with the the last line.

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

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

It is cool to initialize most components on a method not in a constructor, as it will become a huge mess, when you start drag and dropping components over your form...

///
/// Clean up any resources being used.
///

protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

This code will released memory after the programming is closed.

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "WinForm";
}
#endregion

this points to the current class/object which is the form. With it you may set its properties.

///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new WinForm());
}
}
}


You need an static method to run the form as an application.

If you ever want to code using notepad cut this code and paste it. if Borland does it that way there's a possibility of being the best way of initializing a basic and simple form.

With C# you can create an executable, so despite performance issues, C# is much better than MFC ...

Lazy bastard I am ;-)


No comments: