Programming in Any Language
C# Classes - Lessons
Introduction to Classes - Properties - Creating and Using a Class - Parameterized Constructors and Overloading - Store Class Type Objects in an Array - Store Class Type Objects in a List - Identifying Classes in a Problem - Creating Multiple Forms in a Project - Working with Multiple Forms - Static Class Fields - Static Class Properties - Static Class Methods - Static Classes
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.