Programming in Any Language

Introduction to Classes

Click to enlarge

This first video will show you how and where to make your own classes. This section is the first of several that will show you how to make your own classes --- and, in turn, instantiate your own objects. PDFs: Slides, CoinToss1, CoinToss2, CoinToss2 Class


Properties

Click to enlarge

This video shows you how to set up properties and how to give access to programs that use these properties. You'll see the classic approach to setting up classes. You will also see shorthand versions using the get and set accessors. It is important you understand all the parts to classes before using the shorthand versions. PDFs: Slides, Form Code for Examples 1-6, Class EX1, Class EX2, Class EX3, Class EX4, Class EX5, Class EX6, Pet Form Code, Hobby Class, Pet Class, Animal Class.


Creating and Using a Class

Click to enlarge

In this video we create a class that has three properties. We then create a form that uses class by allowing the user to enter data into an object using the set accessors of the class. Then, using the get accessors of each property the entered data is displayed. PDFs: Slides, Class Code, Form Code.


Parameterized Constructors and Overloading

Click to enlarge

This video shows you how to set up a class to accept numeric data as either a double or a decimal. It also shows you how to set up multiple constructors so a program can pass data or nothing to the object. The reason for this is you want to be able to use your class in multiple programs. Those programs can be written with any data type without you needing to worry if the class can handle that. PDFs: Slides, Class Code, Form Code.


Storing Class Type Objects in an Array

Click to enlarge

You can create multiple occurrences of a class and store the resulting objects in an array. This gives you access to all the objects using a simple index. And that means you can step through an array of objects using a loop. Consequently, a banking application could be used to update the balance of thousands of customers. Likewise, we can use an array of objects to track the brand, model and price of many products we might be selling.

In this video we are using the espresso class we saw a few videos ago. But this time we are setting up an array to store many different espresso objects. And, because of the array, we have access to the brand, model and price of every espresso machine in our inventory.

You will learn that we can easily modify the espresso application we wrote previously allowing it to handle multiple objects.

PDFs: Slides, Class Code, Form Code.


Storing Class Type Objects in a List

Like the array, you can create multiple occurrences of a class and store the resulting objects in a list. And, again, via a simple index you can step through a list of objects using a loop.

We go a step further in this video by not limiting the list to a specific number of objects. This one is wide open. We can add as many objects as we wish. A Save button and a Load button were added to this example to show you how you can save your object data and later bring that data back to the list.

PDFs: Slides, Class Code, Form Code


Identifying Classes in a Problem

You know how to code a class. But what should be a class? That is sometimes a difficult thing to identify. Well, this video will help simplify that process. And the key here is understanding what the problem actually is. If you do not understand the problem, it is very difficult to create the classes that will help solve the problem.

PDFs: Slides, Car Class, Customer Class, Service Quote Class, Form Code


Creating Multiple Forms in a Project

Forms can be confining when developing applications. Panels and Group boxes help. But when you need a lot of real estate for your applications nothing beats multiple forms. Yes, you can have an unlimited number of forms in your project, each the size of your computer screen. You probably won’t go to that extreme but several large forms in an application is not unusual. This video examines how we can create multiple forms in an application.

PDFs: Slides, MainForm code, MessageForm code


Working with Multiple Forms

In this video we will examine the concept of modal and modeless forms – basically, when should a form have the focus. In addition to that we will learn how to place controls on a second and third form and even more importantly, how to access those controls from other forms.

PDDFs: Slides, Main code, Directions code, Details code


Static Class Fields

In this video we are going to examine instance variables which are created every time you create an instance of a class. Then we are going to go one step further and create a static variable – a variable that stands alone and belongs to the class, not the instances. And we’ll show you why you might want to use such a thing.

PDFs: Slides, SomeGame code, RollDice code, Dice Class code


Static Class Properties

In this video we are going to examine instance variables which are created every time you create an instance of a class. Then we are going to go one step further and create a static property – a property that stands alone and belongs to the class, not the instances. Any and all instances share the contents of this static property.

PDFs: Slides, SomeGame code, RollDice code, Dice Class code


Static Class Methods

Click to enlarge

Click to enlarge

Static Methods are convenient for many tasks because they can be called directly from the class as they are needed. They are frequently used in utility classes that perform operations on data but have no need to collect and store data. In this video we again use the game playing dice roll app but with a difference. The static method will accept the number of players playing the game and divide that number into a fixed dollar amount. Each player will then be given a specific amount of cash at the beginning of the game.

This class can be used in any game and provides the dice roll function and a specific dollar amount for each player.

PDFs: Slides, SomeGame code, RollDice code, Dice Class code


Static Classes

Click to enlarge

In this video we are going to create a static class that will be used as a logging utility. A user will enter a password in an application. That application will contain an array of legitimate passwords. The static class will be called by the application program to write an entry to a log file every time it is called. What is actually created is a log file detailing every password attempt in the program whether successful or unsuccessful.

PDFs: Slides, Form Code, Log Class


Returning Objects from Methods

Click to enlarge

We can return random numbers from a method. Objects we create are no different. We can write methods to return an int, double, decimal or any other data type. If we create a class to create contact information and call it Contact, we can return a “contact” from a method. That’s what this video is all about.

PDFs: Slides, Form Code, Class Code


Writing a ToString Method

Click to enlarge

Every class you create automatically has a ToString method. It is there as a convenience for you to override. In itself, it does nothing but display the namespace and the class name when called. When overridden, which is its reason for being, it can display the contents of instance of a class as you direct. This video should make this clear to you.

PDFs: Slides, Form Code 1, Class Code 1, Form Code 2, Class Code 2, Form Code 3, Class Code 3, Form Code 4, Class Code 4


Adding a ToString Method to a Class

Click to enlarge

In this video we take an application we have already created and modify it to use a ToString method to display the properties of an instance of the class. If you remember the Expresso class used in the lesson “Creating and Using a Class” we displayed the properties of an instance in a set of labels on the form. This time we change the application by eliminating the labels and letting and override of the ToString method do all the work.

PDFs: Slides, Form Code, Class Code


Null Values

Click to enlarge

Creating instances of objects takes up memory. C# provides a garbage collection routine that runs periodically and removes objects that are no longer being referenced in your program. How does it know what objects aren’t being referenced? Well, any instance that is set to null is no longer being referenced in your program. You, the programmer, don’t control the garbage collections routine but you do control when an instance is no longer needed.

This video shows you how to set instances to null and how to check for instances that might be null.

PDFs: Slides, Form Code 1, Class Code 1, Form Code 2, Class Code 2, Form Code 3, Class Code 3, Form Code 4, Class Code 4


The THIS Reference Variable

Click to enlarge

The fields in the constructor of a class have names. If you place “this” in front of any variable in your class methods, it is making a reference to the fields in the constructor. What does that mean?

Our applications are often using the set and get methods of our classes. When we want to pass data to a class field we use “set” and we pass the data in a parameter of our class call.

In the class method itself that data is captured in a parameter variable of that method. The name of that variable could be anything. It’s best to make that name something that relates to the field in the constructor. The “this” keyword allows us to actually name it the same as the field in the constructor. The “this” keyword simply means “this object’s” field in the constructor.

Grasping this concept becomes clearer when we see it in action. And the thing is, you don’t have to do this. This is simply a technique that allows programmers to use the same name when referring to data coming into a class from the outside.

PDFs: Slides, Espresso Class 1, Espresso Form 1, Espresso Class 2, Espresso Form 2, Espresso Class 3, Espresso Form 3, Radius Class, Radius Form, Contact Class, Contact Form