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.

This program has 8 lines of non-whitespace code. Whitespace is defined as spaces, tabs, and newlines. In C#, extra whitespace is ignored, including blank lines. Let's look at one of the lines we have already changed:

return "Hello World!";

This line says to return the message "Hello World!" to whomever asked for it. Notice that the line ends with a semicolon, signifying that the statement is complete.

Who asked for the message? To understand this, we should first understand curly braces: {}. An open brace { and close brace } define a block of code. Blocks of code are associated with the line of code that comes directly before the open curly brace. In HelloWorldBot, there are 3 blocks of code:

  • namespace Chatbot
  • class HelloWorldBot : BasicBot
  • public override string HandleMessage(string message, string user, FlatFile ff)

Try to find these blocks for yourself. The source code has been included on this page for convenience.

using System;

namespace ChatBot {
	class HelloWorldBot : BasicBot {
		public override string HandleMessage(string message, string user, FlatFile ff) {
			return "Hello World!";
		}
	}
}

We will ignore two of these blocks of code: namespace Chatbot and class HelloWorldBot : BasicBot. The other block of code is called a method. We will refer this block by its method name, which is the last word before the open paren: HandleMessage. Methods can be called (executed) by other code. In some other file that we'll never see, there is code that calls the HandleMessage method to get the message that will be sent back to the person chatting with the bot.

The important thing to take away here is that if we want to change the way our chatbot responds, we need to change the code inside the HandleMessage method. Since this is basically the only thing we want to do, we will be able to ignore the rest of the code outside of the HandleMessage method for the rest of the tutorial. With this mental filter, here is the code that you should limit yourself to seeing in HelloWorldBot.cs:

public override string HandleMessage(string message, string user, FlatFile ff) {
	return "Hello World!";
}

Strings, types, and variables

You may have noticed in the previous exercise that the only code that was included in the bot's response was between quotes. Like curly braces, a pair of quotes defines a region of code. The region between a pair of quotes is called a string, which is short for a string of characters. A string is a type of data that exists in C# programs. If we want to reuse a piece of data in our code, we can store it in a variable. The key terms here are:

data
Some information in a program.
type
In C#, all data has a type. The type of the data defines its function. For example, there is a type for words, string, and many types for number, including int and double.
variable
A piece of memory that can store data. If data is stored in a variable, the data can be referenced later in the code my using the variable.
string
A type of data that stores a sequence of characters.

To store a piece of data into a variable, we use this syntax:

<type> <variable name> = <data of the type mentioned>

For example, if we wanted to store the string "Quackers" into a variable named pet, we would write it like this:

string pet = "Quackers";

This line declares a variable named pet with the type string and stores the string "Quackers" inside of it. The advantage of storing data into variables is that later in the program we can use the variable's name to reference the data. For example, assuming that we had already written this line of code earlier in the program, we could print "Quackers" to the string with this line of code:

return pet;

Here are some more examples of storing strings into variables:

string lastName = "Smith";
string book = "Second Foundation";
string foo = "blah";

It is important to note that the variable name can be anything you want as long as it does not contain any spaces.

Strings, Types, and Variables Exercise

Try this exercise: Change your HelloWorldBot to respond to all messages with this response: "hey, I can use string variables!". Additionally, you must use one string variable and must not have this line of code:

return "hey, I can use string variables!";

Take your time on this exercise. You've learned all the tools necessary to do this, but now you need to use them to figure out a solution. Once you're finished with your code or have been completely stuck for awhile, you may compare your code against the solution: HelloStringsBot.cs.

Reassigning Variables

Let's look more closely at this line of code:

string pet = "Quackers";

This actually accomplishes two tasks: 1) Declares the string variable pet and 2) assigns the value "Quackers" to this variable. By declaring the string variable pet, it tells the computer to set aside a piece of memory labeled by the name pet that can store a string variable, and that this memory location may not be used for anything else. By assigning the value "Quackers" to the variable pet, it is actually putting that piece of data into the memory location. It is important to know that a variable may only be declared once, but may be assigned and reassigned as many times as necessary. Let's look at an alternative way to write this line of code:

string pet;
pet = "Quackers";

These two lines of code are equivalent to the previous line of code. Most people prefer to declare and assign in one line since it is shorter to type, but what about reassigning variables? Consider this example:

string pet;
pet = "Quackers";
pet = "Woofers";

These 3 line of code declare the variable pet, assign the value "Quackers" to pet, and then assign the value "Woofers" to pet, thus wiping out the old "Quackers" string. Let's combine all of this into one more example:

string pet = "Quackers";
pet = "Woofers";

This accomplishes the same task as the previous example in two lines of code. This is probably the most common syntax to do this. Realistically, these lines would never be directly one after another like this, but may exist in separate ends of a program. We'll look at reassigning variables more later.

The HandleMessage Method: Using Parameters

Clearly, to make a better chatbot, we will need to able to examine the message received before issuing a response. From the last section, we know that string data can be stored in variables of type string. Luckily, the HandleMessage method already provides some pre-assigned variables for us, called parameters.

Look at the following HandleMessage method from HelloWorldBot. Can you find the parameters?

public override string HandleMessage(string message, string user, FlatFile ff) {
	return "Hello World!";
}

Three parameters are defined on the same line as the method name. This line is called the method header. The parameters are contained inside the parens just following the method name, and are separated by commas:

string message
Contains the most recent message sent to the chatbot.
string user
Contains the username of the person that most recently sent a message.
FlatFile ff
This variable has a strange type: FlatFile. This is a powerful type will allow us to make very complex bots, but we'll talk more about this later.

The parameters are different than other variables we have seen because we cannot know their values beforehand. However, this is the true power of parameters. Let's try an example:

Instead of having the bot say the same thing for every response, let's have it reply back with the message that was received. To do this, change this line:

return "Hello World!";

to this:

return message;

This will send the person's message to the bot right back to them. Compile, run, and switch back to Windows Live Messenger to test out your new bot. If you're having trouble getting this bot to work, here's an example solution: BouncerBot.cs.

This code is very similar to the HandleMessage method in HelloStringsBot.cs from the previous example. In HelloStringsBot, we created a string variable, assigned it a value, and returned the value stored inside the variable. In BouncerBot, we just returned the value stored inside the parameter, which is a variable that already has a value.

String Concatenation

String concatenation is a feature of C# that literally means to "chain together strings". We can connect two or more strings and/or string variables to form a new string with the + operator. Here are three examples of HandleMessage method bodies that use string concatenation. Try them out for yourself:

String Concatenation Example 1

HandleMessage method body:

return "Hello World" + " again";

Response:

Hello World again

Full chatbot source: ConcatenationExample1Bot.cs

String Concatenation Example 2

HandleMessage method body:

string firstName = "Piston";
string lastName = "Honda";
string fullName = firstName + " " + lastName;
return fullName;

Response:

Piston Honda

Full chatbot source: ConcatenationExample2Bot.cs

String Concatenation Example 3

HandleMessage method body:

string fluffyStuff = "snow";
string compoundWord = fluffyStuff + "man";
return compoundWord;

Response:

snowman

Full chatbot source: ConcatenationExample3Bot.cs

String Concatenation Exercise

Try this exercise: Using string concatenation, change BouncerBot.cs so that instead of just replying with the user's message, it says "You said: " and then the user's message. For example, if the bot received the message: "Hey, I know you're a bot!", it would reply:

You said: Hey, I know you're a bot!

Once you have finished this step, change the output so that it says: "Hey <user>, you said: " before the message, where <user> is the user who messaged the bot. For example, if Don Flamenco sent the message "Do you like dancing and boxing?" to the chatbot, the chatbot would respond:

Hey Don Flamenco, you said Do you like dancing and boxing?

To write the code for this program, you'll need to use two of the three parameters. When you are done with the exercise, you can compare with this sample solution: ConcatenationExerciseBot.cs.

Previous Next
 
Comments to Jeffrey P. Bigham