The Basics of Programming

Structures and Classes

In some computer languages, such as C, C++ and C# you can combine variables into groups called structures or classes. A structure is basically a grouping of values that have a common purpose such as the attributes of window on a computer screen, a character in a game, a customer in a database, a product item, and so on. In C, you would use the keyword struct. The types of variables can be mixed so you can have integers (int) character strings (char) and boolean (bool) variables in the same structure.

Examples,

struct Window {
 Int16 left_position;
 Int16 top_position;
 Int16 width;
 Int16 height;
 string window_title;
 };
struct customer {
 string customer_number;
 string title; 
string firstname; string lastname; string street; string city; string county; string country; string postcode; string telephone_number; string balance; };

To use a structure, you specify the word struct, the name of structure and the name of the variable e.g. struct customer customer1;
To use the individual values of a structure you would use the format variable.fieldname, where a dot seperates the variable and field name for example, customer1.street.
e.g.
customer1.customer_number = "100101";
customer1.title = "Mr";
customer1.firstname = "Simon";
etc.

Classes are similar to structure but classes are using in languages such as C++ that use Object Orientated programs that define the objects to use in a program and groups values and subroutines and functions (called methods) in to a class. Typical subroutines, would be to create an instance of a class or delete an instance or set or get values of an instance. An instance is when you create a specific entry such as a person that is a customer. The routine that creates an instance of a class is called a constructor, and one that deletes one is called a destructor (the method starts with a tilde or ~ character).

In C#, classes are usually defined within a namespace to organise and define the scope of related objects such as classes, interface, and structures.

Examples, (this is simplified code):

namespace CustomerNamespace
{
class
Customer { string customer_number; string title;
string firstname; string lastname; string street; string city; string county; string country; string postcode; string telephone_number; string balance; }
Customer(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11) {
   customer_number = p1;
   title = p2;
   firstname = p3;
   lastname = p4;
   street = p5;
   city = p6;
   county = p7;
   country = p8;
   postcode = p9;
   telephone_number = p10;
   balance = p11;
  }
 }
}

To create a new instance of a customer you would call the class' creation subroutine, called a constructor, and supply the necessary values.
e.g.

Customer mycustomer; /* declare instance of class customer called mycustomer */

mycustomer = new Customer("C000100", "Mr", "James", "Smith", "22 Lee Road", "Leeds", "West Yorkshire", "England", "LS1 1PQ", "0113 456821", "50.00"); /* set up a new customer using constructor */

where mycustomer is the instance of the class customer, the word new is used when creating an instance, and customer is the name of the constructor of the class, also called customer.

See C Sharp pages for more about namespaces and classes.

Go to C# Subroutines