skip navigation
 
 
 
WebInSight
Previous Next

Examining HelloWorldBot

A program is a list of simple commands or statements. In C#, each statement is terminated by a semicolon. A program with multiple statements executes each of the lines in sequence, one after another. Let's look more closely at HelloWorldBot.cs line by line. The source code has about 14 lines of code and has been included on this page for convenience.

// Hello World Bot
// Returns the message Hello World!

using System;
namespace ChatBot 
{
	class HelloWorldBot : BasicBot 
	{
		public override string HandleMessage(string message, string user) 
		{
			return "Hello World!";
		}
	}
}
  • Lines 1-2: The top two lines are comments. Comments are optional and are completely ignored by the compiler. Single line comments begin with "//". Anything can be written after the "//" to help describe what the program does. All the source code files in this tutorial will have two beginning comments -- the name of the file and what the file does.
  • Line 3: This is a whitespace line. Whitespace is defined as spaces, tabs, and newlines. In C#, extra whitespace is ignored, including blank lines. So why do we include it? To make it clear the seperation between the comments and the source code.
  • Lines 4-6: For this tutorial, we will not modify these lines. You will need them for every chatbot but you will not need to know what they mean. If you would like to know what they mean, you are encouraged to ask an instructor.
  • Line 7: The only word you will change in this line is "HelloWorldBot". This word will be changed for every chatbot you use. It should match the name of your file. For example, if you save a file as "HiBot3.cs", you should change the word to be "HiBot3".
  • Line 8: Opening curly brace. Remember, all opening curly braces should be closed by the end of the file.
  • Line 9: This line declares the method "HandleMessage". You will not need to modify this line. Note that there are two parameters (seperated by a comma) -- "message" and "user" both of which are strings. More on parameters later.
  • Line 10: Opening curly brace.
  • Line 11: The return statement is always the last statement in a method! Your program will not compile if it is not the last statement in the method. Notice that the line ends with a semicolon, signifying that the statement is complete. Statements will always end with a semicolon.
  • Lines 12-14: Closing curly braces. They close up each of the opened blocks, including the method block.

There are a few important points to highlight before we move on:

  1. We will only modify what is inside the method "HandleMessage" block. You can mostly copy everything outside of the method (remember to change the name of the program on line 7). What is the "HandleMessage" block? It is everything within the curly braces after the method has been declared (lines 10-12).

    An analogy that may be helpful is to think of the source code like a textbook. Most textbooks have a cover, copyright page, table of contents, chapters, and an index. The "HandleMessage" method is like a chapter that we can edit. Even though the textbook has one chapter, it still needs a cover, copyright page, etc. For now, you can copy the rest of the code to make the rest of your textbook. When you get more programming experience, you will be able to edit the whole textbook.

  2. All curly braces will occur in pairs (i.e. "{" needs a "}" sometime after).
  3. All statements (like the "return" one) should have a semicolon at the end of it.
  4. Return statements will always be the last statement in your method. You should have a return statement for all your chatbots.
  5. The words in the source code are case sensitive! For example, the compiler sees "HandleMessage" as different from "handlemessage" because the H and M are not capitalized in the second version. As a general rule, most of the words we'll use will be all be lowercase.

Previous Next
 
This work is supported by the following National Science Foundation Grants: CNS-087508, CNS-0549481, IIS-0811884, IIS-0415273
Send comments to Richard Ladner