My knowledge of C# is sort of like my knowledge in Spanish after taking three levels of it in College followed by nothing for 8 years: I know C# code when I see it, I sort of know half the sentence and can figure it out if I look something up, but take away the crutches and ask me to code something in C#? Forget it. So, it’s time I brush up on the fundamentals.

Method of Learning: Pluralsight, C# Fundamentals by Scott Allen
image

Topics: Classes and Objects; Constructors; Reference Types; OOP Fundamentals
Software: Visual Studio Pro 2013 (the free Community Edition shouldn’t be much, if any different)

 

Classes and How They’re Defined

The point of programming something is to solve a problem. Classes logically group together the data and behavior involved in working with an entity that the program must work with.

Let’s say your problem is:

I need to create a grade book to store a student’s grades, and calculate the high grade, low grade and average grade.

Classes are typically named for the nouns in the problem. Here, a good class candidate would be “grade book”. This because you need to create a grade book, or maybe more than one, and then have that grade book do a bunch of stuff.

How Classes Relate to Objects

The tricky thing is that classes are just blueprints. A class will tell you what you’re allowed to call a “grade book”, and what sort of actions can be taken on this “grade book”.

In order to use a grade book, you have to use the blueprint to create one. What you will create using the class blueprint is called an object, or an instance of that class.

To use a hockey analogy, you could define “GoalieMask” as a class. The “GoalieMask” class might hold blueprints for standards like the shape, the length, the color, the weight. However, those are just standards; you don’t actually have a GoalieMask until you use the blueprint to create a GoalieMask object.

Creating a New Class

Defining a new Class in Visual Studio is pretty easy: In the “Solution Explorer” on the right side, right-click your project name, hover over “Add”, and click “Class…”. Type in the class name in the bottom text box, and click Add.

image

The default Class code looks like this:

image

Define Class Members

MSDN – C# Members

Members define the data and behavior of a class. The terms tossed around a lot in the tutorials I see, that can be easily confused, are: Parameters, Properties, Methods and Fields. Parameters aren’t class members, but I’ve confused the term “parameter” with a property and a field before, so it bears mention.

Classes contain methods, which house the code needed to carry out an action or behavior. Methods tend to be defined using the verbs in our programming problem.

In our grade book problem, for example, we need the grade book to store student scores, then calculate their high, low and average scores. These two verbs can be the basis for methods.

Here is a defined “GradeBook” class, that shows an example of a parameter, method and field:

class GradeBook {

public void AddGrade (float grade)

{

  grades.Add(grade);

}

 
List<float> grades;

string subject = “Not Defined”;

}</pre>

AddGrade is a method, which houses all of the code needed to add a grade to the grade book.

The “grade” variable next to it is a parameter. When AddGrade is called from elsewhere in the program, it will expect to be passed a value along with it. It’s expecting a number, as defined by the “float” type used. That value will be assigned to a variable called “grade”, and can be used anywhere within AddGrade.

Any variables declared directly inside the class are called Fields. A List field was created to store all of the incoming grades. A “subject” string field was created so that we can give it a subject name. I gave it a default value of “Not Defined”.

Creating a New Object From a Class

Here’s what really threw me off when learning this stuff.  Here is the code to create an object from the “GradeBook” class from the main portion of the program:

GradeBook book = new GradeBook();

Uh… Why is there a Gradebook on the left AND right side? Why do I have to list it twice? Let’s check it out.

The left side defines the variable name and type. The right side creates a new instance of an object.

Normally when you declare a variable, you give that variable a type. An integer variable would be called:

int MyNumber = 5;

You created a variable of type int called MyNumber, and gave it a value of 5. When you define a class, that class is actually a “Custom“ type. So if you’re creating a new instance of a class and calling it “book”, which we’re doing with GradeBook, the type it’s expecting is not int or float or string, it will be GradeBook, to match the name of the class.

GradeBook book;

The “book” variable now has a type defined, but as I’ve said earlier, classes are just blueprints to create data from. The class itself doesn’t have data in it, so “book” doesn’t refer to anything we can use yet.

What we have to do is create (instantiate) an object from that class blueprint. This is done with the new keyword:

GradeBook book = new GradeBook();

This statement creates a new object from the Gradebook class and assigns that object to the variable “book”, which is expecting data of that type GradeBook.

Object Constructors

A constructor is the method which runs when an object is created (“instantiated”). The Gradebook method on the right side of that statement is a constructor.

We didn’t create a Gradebook method in the main program class file, but there is one implied that’s sort of behind the scenes, which creates an instance of that object. If we create our own constructor though, we can add more instructions to this code that runs when that object is created.

So, in our Gradebook.cs file, we create a Gradebook method:

public GradeBook()
{
     grades = new List<float>();
}

public void AddGrade(float grade)
{

     grades.Add(grade);
}

List<float> grades;

With this code, every time GradeBook is created,

Tying This Into My ASP.NET MVC Goals

As I run through some ASP.NET MVC tutorials, I notice there are a lot of class files once the program gets even a little complex. Following the logic of the tutorial was easy enough, but I didn’t realize how much trouble I had creating my own classes until I took the training wheels off. I could wing it, but it really helped to know some underlying principles:

Classes are defined from nouns. Methods inside classes are defined from verbs.

This is something that sounds obvious now. When I started learning it though, it felt like when you know a sentence’s grammar looks wrong, and you know what looks right, but you can’t explain the rules why; you just know it’s wrong or right.

It helped me quite a bit to know the fundamental way these things are defined.