A Quick-and-Dirty Programming Primer






A Quick-and-Dirty Programming Primer

Programming is complicated. Everything is so interrelated that it's difficult, if not impossible, to isolate each programming concept and then present the material in a linear fashion. Instead, while learning one subject, you often have to touch on elements of another subject before you've had a chance to learn about the secondary topic. I've made every effort to avoid such forward references, but there are some concepts with which you'll need to be at least slightly familiar with before proceeding. You'll learn the guts of each of these topics in their respective lessons, but you'll need to have at least heard of them before digging any deeper into this book.

Storing Values in Variables

A variable is an element in code that holds a value. You might create a variable that holds the name of a user or the perhaps the user's age, for example. Each variable (storage entity) must be created before it can be used. The process of creating a variable is known as declaring a variable. In addition, each variable is declared to hold data of a specific type, such as text (called a string) for a person's name or a number for a person's age. An example of a variable declaration is

string strFirstName;

This statement creates a variable called strFirstName. This variable is of type String, which means it can hold any text that you choose to put into it. The contents of a variable can be changed as often as desired.

The key primer point to remember: Variables are storage locations that must be declared before use and that hold a specific type of data.

Using Procedures to Write Functional Units of Code

When you write Visual C# code, you place the code in a procedure. A procedure is a group of code statements that perform a specific function. You can call a procedure from code in another procedure. For example, you might create one procedure that totals the items on an order and another procedure that calculates the tax on the entire sale. There are two types of procedures: procedures that don't return values and procedures that do return values. In addition, some procedures allow data to be passed to them. For example, the tax calculation procedure mentioned previously might allow a calling statement to pass a monetary total into the procedure and then use that total to calculate tax. When a procedure accepts data from the calling code, the data is called a parameter. Procedures don't have to accept parameters.

A procedure that doesn't return a value is declared using the keyword void, and looks like this:

public void MyProcedure()
{
   // The procedure's code goes here.
}

Notice the beginning and ending bracesall Visual C# procedures use them to surround the code of the procedure.

A procedure that returns a value is declared with a return data type specified before the procedure name, which denotes the type of data returned by the procedure:

public string AuthorName()
{
   return "James";
}

Notice the word string. This is a data type. In this example, the function returns text (the name "James"), which is a string.

If a procedure accepts a parameter, it is enclosed in the parentheses, like this:

public string AuthorName(string BookName)
{
   // procedure code goes here
}

MessageBox.Show()

You're almost certainly familiar with the Windows message boxit's the little dialog box used to display text to a user (see Figure). Visual C# 2005 provides a way to display such messages using a single line of code: the MessageBox.Show() statement. The following is a MessageBox.Show() statement in its most basic form:

MessageBox.Show("Text to display goes here");

18. Visual C# makes it easy to display simple message boxes like this.


You'll use message boxes throughout this book, and you'll learn about them in detail in Hour 17, "Interacting with Users."



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows