skip navigation
 
 
 
WebInSight
Outline Next

Introduction

How computer programs are (generally) made

People write the source code for programs in the programming language of their choice, which uses a simple vocabulary of human-readable words to express commands for the computer to follow.

This source code file is then given to a special computer program called a compiler that converts the source code to a language understandable by a computer, usually machine code represented in binary (zeros and ones).

Our programming language (for this project)

We're going to use the programming language C# (pronounced C Sharp) to program chatbots, although it was an arbitrary choice. No programming language is inherently better than any other - they all have their strengths and weaknesses.

File types and extensions

In Windows, each file type has its own file extension, which is identified by the characters following the dot in the file name. For example, somefile.txt has the file extension txt. You'll encounter several file types while coding in C#:

cs
A C# (human-readable) source code file. You'll manually edit these files.
exe
An executable program. Most programs on windows have this file extension.
bat
A Windows batch script. These are simple programs that compile your C# source code.

HelloWorldBot: Your first bot

Open TextPad and open the file HelloWorldBot.cs, which is inside the "Your Bots" subfolder of the chatbots folder. Before trying to understand this code, let's compile this code and get a chatbot running.

When compiling your chatbot, make sure that the code file is in the current front window in TextPad. Press CTRL+1 to compile your chatbot code. Once compilation is complete, open Windows Live Messenger and log into a different account than you're using for your chatbot, which you specified in Setup. Add your bot's account as a contact. Now, switch back to TextPad and press CTRL+2 to run the chatbot.

Switch back to Windows Live Messenger. Your chatbot should now be logged in, so go ahead and message it. No matter what you type, the chatbot will respond:

Hello World!

Not very exciting, but this is a good start. Let's go ahead and change the message that chatbot sends back. Switch back to TextPad, press CTRL+TAB until you arrive at the Command Results window, and press any key to kill the chatbot program, which signs the chatbot offline. You can also kill the program from any window in TextPad by pressing CTRL+BREAK. We'll need to do this every time that we want to change the code and relaunch the bots. Now, use CTRL+TAB to switch to HelloWorldBot.cs in TextPad. Find the line that says:

return "Hello World!";

This is the line of code that caused the chatbot to say "Hello World!" for every response. Change the code between the quotes to another message so the chatbot will say something else. Make it clever so you won't get tired of hearing it over and over again. Once your changes are done, compile (CTRL+1) and run (CTRL+2). Switch back to Windows Live Messenger. Message the bot to verify that its response has changed.

Debugging in TextPad

Unfortunately, no programmer writes mistake-free code 100% of the time, so it is useful to learn how to debug your programs. Here, we will purposely place a bug in our code and then use feedback from the compiler error and TextPad to fix it.

  1. Open HelloWorldBot.cs in TextPad.
  2. Go to line 6, which, before you changed the code, used to say:
    return "Hello World!";
    
    Remove the semicolon from the end of the line.
  3. Press CTRL+1 to compile. You should hear a different sound than when the code properly compiled. The Command Results should also be the front window, which reads:
    HelloWorldBot.cs(6,25): error CS1002: ; expected
    
    Tool completed with exit code 1
    
    This is an error message telling you that the file on line 6, column 25 in HelloWorldBot.cs, a semicolon is expected.
  4. Press CTRL+TAB to switch back to HelloWorldBot.cs from the Command Results window.
  5. Go line 6 (the same line we changed above), and add a semicolon to the end of this line.
  6. Press CTRL+1 to compile HelloWorldBot.cs. You should hear different sound than the error sound, which indicates that compilation was successful. Since there were no error messages, the Command Results window is not brought to the front.

Sometimes compiler error messages can be very cryptic, unlike this straightforward one. Interpreting error messages is a skill gained with experience. Also, the compiler is also not always correct about where the error occurs; the error often occurs on the line before or after the mentioned line. Also, it is not uncommon to have multiple error messages on one compile. In these cases, it is usually best to try and fix the first error message, and then recompile to see if any of the others are corrected by the same change.

Outline Next
 
Comments to Jeffrey P. Bigham