You are here:   Units 1 to 4 > 2. Programming
  |  Login
Welcome To Learning Unit 2


In this section you will be shown a lot of information about programming. The problem here is that in order to write ASP.NET programs you need to know how to program. While you will get a good basic foundation here taking a programming language like C# or VB.NET will only improve your ability to write robust ASP.NET web pages.

We have put together a great deal of information on the C# programming language. Most of this unit will simply be links to that content but how it adapts to ASP.NET will be added.  If you are a Visual Basic person and prefer to write your programs using the language please feel free. As of the writing of this page we have not put together any VisualBasic lectures. So you are on your own to use the text or find one that you like on VB.

To see the entire C# Tutorial click here
Variables and Data Types

 In order to do anything real interesting in programming you are probably going to want the ability to store data. For instance, if you were computing sales tax on an item you would probably need a place to hold the price and the current sales tax at the very least. This is where a variable comes in to play. A variable is nothing more than a place in memory where you can store information. The beauty of it is that you can give your variable any name that you wish; well almost. There are a few restrictions on what you can call your storage locations (variables).

What you should know is that variables come in many different types. For instance if you were to compute sales tax you are probably going to want to use a floating point number. This is a number that has a decimal like 8.75. So you have some choices but you should choose a type that holds floating point values.

To understand this in better let's look at variables and data types in more detail:

Data Types and Variables
C# Data Types and Variables
C# Data Types and Variables
Operators



Most programming languages have several different types of operator. While you will see some of these later on here we are concerned primarily with the math operators. These operators allow you add, subtract, multiply, divide, and concatenate.

Basically there are two types
1. Unary operators which operate on one operand. This means it effects only one variable. These are easy to spot because they look like this: x++.

2. Binary these operators are like what you have been using since grade school: x + y

To get a good understanding of these operators view the following:
Basic Operators
Examples of basic operators and expressions in C#
Examples of basic operators and expressions in C#
Relational Operators



These operators are called relational because they measure the relationship between two values. Basically they ask a question. For instance, is x equal to y? What you should know is that these operators produce only the values true or false. These true or false values are called Boolean values named after George Boole who pioneered Boolean logic.

For some reason people have trouble with the bool value but it is a key concept to understanding how loops and decisions work in code. Basically something loops while the result produced by a relational operator yields true. Same thing holds true for decisions. If the result produced by the relational operators is true then do this.

Read through following carefully and try to get an understanding of just how these operators work:
Relational Operators
The C# relational operators
The C# relational operators
Decisions



If you write enough code you are going to be faced with having it execute some code based on the results of a decision. For instance, if the gross sales is greater than 10,000 then apply a bonus of .01. These kinds of decisions come up all the time so getting a grasp on how decisions work and how to apply them is a must for anyone who writes code.


if statements



The simplest decision is through the use of the if and the if / else. The following shows how to use them in the C# programming language:
if and if / else
Simple c# decisions using if and if / else
Simple c# decisions using if and if / else
Operators, Operators, We Need More Operators!!



Sometimes you need the ability to make multiple decisions in one statement. For instance, if Joe is older than 21 and Jim is younger than 30 then let them have access. This could definitely be done using multiple if statements but sometimes doing this makes the code a little clumsy. To clean things up a little bit we have the Logical operators that allow us to create compound decision statements:
Logical Operators
The C# Logical Operators
The C# Logical Operators
The Switch Statement



Many time you need to select one value from a list based on a value. This is where the switch really shines. It kind of works like the old phone operator who would plug your line into the appropriate spot so that you could get connected to the correct party:
The C# Switch Statement
Using the switch statement to select one of many
Using the switch statement to select one of many
Repetition



Many times in your code you will need to repeat a set of instructions a number of times. It may be as simple as counting a number of things or printing a number of things to the screen. Repetition also called loops are structures that you use to do this kind of thing. There are basically 2 types of loops.

  1. Counting loops
  2. Indefinite loops

Determining which to use and where is the trick. The following will give you the foundations of each loop so that you can make a decision on which one to use and where:
C# for loop
Counted loops in C# using for
Counted loops in C# using for
Using the while loop
counted, indeterminate, and indefinite loops using the while and do while
counted, indeterminate, and indefinite loops using the while and do while
Break and Continue



Sometimes it is desirable to get out of a loop or go back to the top of a loop early. This is where the break and continue statements come in:
break and continue
How to branch within loops using c#
How to branch within loops using c#
Arrays



An array is like a list of items that can be accessed using one name and an index. This allows you to store or group like information. An example of this might your grades.  The array name (or list if you choose to think of it that way) would be assignments. If you have 10 assignments then you would create a list that would hold ten grades. This list could then be accessed using and index from 0 to however many values you have stored.

The following will give you a good foundation on creating and using arrays:
Arrays in C#
How to create and use arrays in C#
How to create and use arrays in C#
Resizing An Existing Array



On occasion you may find that you have not made your array big enough to hold all of the data that you need to store in it. Although this may not happen very often, it may be a good idea to know how to do it when it is needed. The following shows how to dynamically change the size of your array:
Resize Arrays
Shows how to resize dynamically allocated arrays
Shows how to resize dynamically allocated arrays

Namespaces


Namespaces in .NET  are program elements designed to help you organize your programs. They also provide a means to avoid naming clashes between two sets of code. Implementing Namespaces in your own code is a good idea because it will help save you from problems later on when you want to reuse code you have previously written. For example, if you created a class named Data, you would need to put it in your own namespace to ensure that there wasn't any confusion about when or where the System.Data class should be used. Generally , it would be a bad idea to create a class named Data because it already exists in the  .NET Framework Class Library but enclosing it in a namespace certainly would allow you to do it.

Namespaces don't correspond to file or directory names. If naming directories and files to correspond to namespaces helps you organize your code, then you may do so, but it is not required.

Creating a namespace is as simple as putting the namespace key word in front of the name you want to give it and then placing the code that belongs to the new namespace within its braces.


namespace msjc
{
    namespace glenn
   {
        public static void Main()
        { 
           
         }
     } 


The above example shows how to create a namespace. It is created by putting the word namespace in front of msjc. Inside the msjc namespace a nested namespace has been created called glenn. Notice that each namespace is a block of code. Meaning it is delineated by curly braces. If you want something to be a member of a particular namespace, you put it inside the opening and closing curly braces.


Importing Namespaces

Once you have created a namespace and want to use it in your code you simply use the import statement. For example:

imports System.Collections;

As it turns out this namespace contains the code for the ArrayList. If we didn't import this namespace we would have to qualify the ArrayList name using the dot operator:

System.Collections.ArrayList myArrayList = new System.Collections.ArrayList();

This is a pretty cumbersome thing to do


Creating Regions


Sometimes your code can start getting really big and hard to maintain. VisualStudio has created the region directive that will allow you to define areas within your code. Then you can simply expand the region when you want to work on the code or contract it when you want it out of the way.

The following images show a region I have defined both expanded and contracted:

Region Definition
Regions are defined in VisualStudio using the #region  #endregion  directives
Regions are defined in VisualStudio using the #region #endregion directives
Contracted Regio
Selec the + icon to expand and - to contract the region
Selec the + icon to expand and - to contract the region

Objects


 

The advent of Object Oriented programming has really advanced the way software is written. It promotes code reuse, encapsualtion and data abstraction. The following will help you understand these terms:

Introduction to Objects

Introduction to the ArrayList



An ArrayList is an object that mimics an array. The beauty here is that it has code to handle the resizing  which means you don't need to do anything but add items to the list.

The ArrayList has many methods. The following are the most popular:

Method  Syntax Description 

Add

 ArrayList.Add(object)

 Adds an object to the end of the list

Insert

 ArrayList.Insert(index,object)

 Inserts an object at the specified index

Remove

 ArrayList.Remove(object)

 Removes the matching object from the list

RemoveAt

 ArrayList.RemoveAt(index)

 Removes the object at the index location

 Sort

 ArrayList.Sort()

 Sort the list in ascending order


The ArrayList also has several properties but there are two that are most commonly used:

 Property Description
 Capacity  Sets or gets the number of elements an ArrayList can contain
 Count  Gets the number of elements actually contained within the ArrayList


Once you have given the ArrayList some space it can be used just like an array. The difference is that when you take something out of an ArrayList it needs to be cast to the base type type your are assigning to. The reason for this is that the ArrayList stores the object type. As it turns out everything is C# is derived at some level from object. Since the object class is the parent of all classes the ArrayList can store any type. Think of it like this, an int is an object, a string is an object, an ArrayList is an object, and so on.

Here is a simple example:

ArrayList numbers = New ArrayList();
numbers.Add(1);
numbers.Add(3);
numbers.Add(5);

int num1 = (int) numbers[0];
int num2 = (int)numbers[1];

The following example uses the Button and Label controls to print the contents of an ArrayList. At form load the ArrayList is filled. When the Button is pressed the contents of the ArrayList is printed to the Label.
Simple Array List
Fill ArrayList in OnLoad event and write the contents out to a Label with a Button click
Fill ArrayList in OnLoad event and write the contents out to a Label with a Button click