Thursday, 14 February 2013

Moving on to C# 1

Moving on from Small Basic to C#

One of the great things about Microsoft's Small Basic is the Graphics Window; it's one of the built-in objects that let the beginning programmer produce attractive and worthwhile results while learning to program.
Small Basic is a simple language that is reasonably easy to learn but due to its simplicity it lacks some of the more sophisticated features of professional languages like C#. These more advanced features help to make larger programs easier to write and reduce the number of programming errors. However, they sometimes take a while to get your head around.

The aim of this project is to help you to learn C# by taking advantage of what you already know from SB. If you've been working with SB for a while you already know the basics of programming; you know about variables, loops, branching and subroutines. You also know how to use the built-in object like the Graphics Window. Here we are going to provide you with the ability to use the SB library in C# programs. You will use the same built-in objects as you are used to from SB but you will learn the new syntax of C# and the new features of that language.

Visual C# Express 2010

SB has it's own programming environment, or IDE. So does C#. So first of all you need to download and install Visual C# Express from the Microsoft web site. It's free but you will probably have to register with an email address. There is a 2012 version as well, but you need to be running Windows 8 to use this. So, on the assumption that most people will still be running an older version of Windows we are going to use the 2010 version (this works on XP, Vista and windows 7).

OK, so off you go to the Microsoft web site and search for Visual C# Express. Download it and follow the instructions to install it. It takes a while but it's quite straightforward.

Setting up Visual C# Express 2010

Before we begin programming we need to set up Visual C# 2010 Express so that we can more easily create a C# program that uses the Small Basic library.

Visual C# 2010 Express comes with some built in project types that allow you to easily create, for example, a Windows program. What we are going to do is to create a new project type to let us create C# programs that use the Small Basic library. There are a few steps to go through but it will only take a few minutes, you only have to do it once, and it will make life easier in the future.

Let's get started.

Start Visual C# 2010 Express and from the file menu select New Project.

You will get the following window which allows you to select the type of project that you would like to create.

We are going to base our new project type on a console application, so, from the window shown above select Console Application and click OK. You will see that a new project has been created with the program in an editor window, on the left, and a list of things that make up the project, on the right.

We are going to use this project as a template for our new project type. However, we need to do a bit of customisation, first.

The first thing is to add a reference to the Small Basic library so that our programs will be able to use it.
From the top, right window, right click on References and select Add Reference.

Now you'll see the Add Reference dialogue. Select the Browse tab and you'll get an explorer window.

Now you need to navigate to the directory where you installed Small Basic (for me that was C:/Program Files/Microsoft/Small Basic – it will probably be the same for you, if you did a normal installation, if not... well you'll just have to find it!). Now select SmallBasicLibrary.dll and click OK.

Right, that is the first step completed. The project now knows where the Small Basic library is located. (I'm sure I don't need to say this, but just in case: don't uninstall Small Basic, otherwise the library will be lost!)

The next step is to include a reference to the library in the program.

In the code window, at the top, you will see a number of lines of code beginning with the word using; this is a list of standard libraries that our programs can use. The Small Basic library is not there, so we need to add it. Add the following text at the end of the list:


Don't forget the semicolon at the end of the line.

(By the way, you probably noticed hints being displayed as you type. You can accept these hints by pressing the tab key, or you can ignore them and carry on typing.)

OK we are nearly there, now.

We initially set up the project to be a console application because if we had specified a Windows application Visual C# Express would have added in all of the Windows components, by default. We didn't want that because we'll be creating our own TextWindows and GraphicWindows using the Small Basic Library.

However, a Console Application will also have its own window and we don't want that either.
So, the next step is to right click on the application name and select Properties.

In the form that pops up we can see that the Output Type is set to Console Application; this means that there will be a console window automatically added to the application. To prevent this happening, change this to Windows Application.

One last step and we are there.

From the file menu select Export Template.

You will then be asked if you want to save the files you've been changing. Select Yes, we do want to save the files.

You will then be present with the following form:

Just make sure that Project Template is selected and click Next.

Now we have a form to fill in.

There are two fields we need to fill in: the name of the template and the description. Call the template whatever you like but I would suggest something like “CS SB Lib App” and for the description “Small Basic Library Application” would do nicely.

Click Finish and your done. Phew!

Now let's try it out!

Writing your first C# program using a Small Basic Graphics Window.

From the file menu select new project, (if you are asked whether you want to save or discard the previous project, discard it - we've already saved what we need) and from the list of project types, select the one you just created and give it a name (in the field at the bottom of the window).

We are now going to write a very simple program using the Small Basic library. In the code editor insert the following line


Again, don't forget the semicolon!

You can see where it goes in the screen shot below; it's between the braces following the line that begins

static void Main.

You'll know, of course from your Small Basic programs just what this line of code will do. I'm not going to explain the rest of the code that has been automatically created; that will have to wait for another day. Just accept that, for the time being, any new code that we write will go between these braces.

Now, locate the green triangle in the toolbar and click on it. This will start the program.

How about that! You've just written you first C# program. If you feel like it you can add some more GraphicsWindow code; it should be exactly the same as in Small Basic, except that you need to end each line with a semicolon (;).

The Paddle Game

If you are eager to see something a bit more exiting delete all of the code from the editor window, copy the code below, and paste it into the editor.

 using System; 
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Microsoft.SmallBasic.Library;

 namespace CSSB1
     class Program
         static Primitive paddle;
         static void Main()
             GraphicsWindow.BackgroundColor = "DarkBlue";
             paddle = Shapes.AddRectangle(120, 12);
             Primitive ball = Shapes.AddEllipse(16, 16);
             GraphicsWindow.MouseMove += OnMouseMove;
             int x = 0;
             int y = 0;
             int deltaX = 1;
             int deltaY = 1;
             int gw = GraphicsWindow.Width;
             int gh = GraphicsWindow.Height;

             while (y < gh)
                 x = x + deltaX;
                 y = y + deltaY;
                 if (x >= gw - 16 || x <= 0)
                     deltaX = -deltaX;
                 if (y <= 0)
                     deltaY = -deltaY;
                 int padX = Shapes.GetLeft(paddle);
                 if (y == gh - 28 & x >= padX & x <= padX + 120)
                     deltaY = -deltaY;
                 Shapes.Move(ball, x, y);


             GraphicsWindow.ShowMessage("You Lose", "Paddle");
         static void OnMouseMove()
             int paddleX = GraphicsWindow.MouseX;
             Shapes.Move(paddle, paddleX - 60, GraphicsWindow.Height - 12);


Press the green start button and you see this:

It is, of course, the Paddle game from the Small Basic tutorial.

Take a look at the code. You'll see some familiar statements that are almost identical to the ones you would use in Small Basic. However, some things are rather different: the loops, branches and subroutines, for example. Also, notice that the variables are preceded (mostly) with the word int. This means that they are integer numbers. In C#, unlike Small Basic, we have to say what sort of variable we want to use.

OK, that's it for this first tutorial; you are now set up to write C# programs. There's plenty more to do, though, and in the next tutorial I'll be taking you through the basics of C#.


  1. Replies
    1. A belated 'thank you', Rick. Glad it was helpful.

  2. Great stuff... although a programming veteran using a variety of languages according to need, I have never used c# , so these tutorials are very useful... Thanks!

    1. Thank you, Chris. I, too, have used a number of languages over the years. Glad this was useful.