Time to learn a bit more about how the structuring of C# works. What are all these silly namespaces, classes and methods? Let’s find out.
Please note the examples are displayed were originally created by Scott Allen, Pluralsight.com C# video extraordinaire. This will also be pretty long as this will be my first time ever diving into classes and having a multi-file program of any level. I’m going to try to be pretty specific about how things work as I’m figuring it out myself and hopefully I’ll be able to convey everything in an acceptable fashion. This is a HUGE learning experience for me and… well… to be honest…
Classes are used to implement features in software. Some classes are already created for you (in the Framework Class Library)but you’ll most likely be needing to create your own for your own needs. Classes are created based on nouns, such as the item of which you need it for (So if you needed to create a blahlog for blah reason, the class would be named blahlog).
Creating a class
When creating a class, it’s good practice to create the class in a file of it’s own and then reference it in your main program. A class can be created by Right Clicking on your solution in Visual Studio, Hover on Add and click Class from the options that appear.
Something to notice when the class is created – The namespace is the same as the Program.cs file. This is because they’re both under the same project and the namespace is how we’re able to group a bunch of files together for one complete package..
A New Item window will appear, which should look pretty familiar. All that’s needed to be done is to give the class a name.
If you take a look over at the Solution Explorer you will also see that a new file has been added to the Solution – the new class.
So now I have a new class called GradeBook and a class needs methods! The class will act as our organization tool for all the nit picky things we’d need to do regarding that topic. The method to be added to this class will be AddGrade, which as you might be able to tell – will add grades into the GradeBook!
Once the skeleton for the method has been created, we’ll need to add a parameter into the methods (). This will actually allow us to pass a value into the method when it’s called. The parameters must have it’s type declared just as a variable does.
Adding the meat to the bones
So, the class is called GradeBook… this means that there should be multiple different grades inside, right? Of course! But how? COLLECTIONS! Collections? COLLECTIONS!! Take a gander at the previous image and the using statements. The second using statement references to System.Collections.Generic, this namespace contains all sorts of wonderful ways to create compare and do other magical things to multiple values. The method that’ll be used here from the System.Collections.Generic library is List<>().
Please be aware that the you don’t have to write out the entire namespace when calling the method because of the using statement up top. It’s done here simply to show the method being listed by Intellisense and the info provided.
Alright, Check it out! Under the Class scope I declared grades as a List that takes float values, but, if we had just
List<float> grades = new List<float>();
After creating the List called grades, we can now add actual grades to it. This is done by calling the list and invoking the method Add, which is a built in method to the List class and then passing in the parameter of the method, grade.
So for now, that does it for the custom class GradeBook. Back to program.cs. It’s time to utilize the class that was just created. Classes can be used to Define a type of variable as well as create objects specific to that type.
On line 13, a variable named HighSchool was declared with the type GradeBook. Just like the List<> from earlier, we’ve got a thing there that says it’s a thing, but nothing to back it up. Therefore the variable must be further defined with the new keyword again and say to make a GradeBook() object.
Now that there’s a variable to manipulate, grades can start to be added to the new GradeBook object – HighSchool. Notice how HighSchool is invoking the AddGrade() method? Look Familiar?? It shoulldddddd.
Look right above the method, it’s a bird, it’s a plane, no, it’s a REFERENCE! The little gray text indicates that there is one reference. This is because HighSchool.AddGrade is referencing the AddGrade method in GradeBook.
And there we have it! A simple class for a simple use. I know if you recreate this little experiment that it won’t actually produce any results on the screen or anything, but that’ll come in due time. For now, personally, I’m just happy to be able to drastically comprehend classes better than before. Not to mention actually create one. The walls of text don’t make it seems so simple, but after a few tries it starts to click. Something to take into consideration – think about metaphors when coding to help better explain what’s going on. I didn’t completely understand why the new keyword had to be used at first and then I thought about the silly little metaphor about the guy on the street that SAYS he’s a lawyer but what MAKES him a lawyer? and that totally helped me grasp the concept. Props to Code Complete V2 by Steve McConnell for presenting the idea. Props also must be given to Scott Allen’s tutorials on PluralSight.com which I’ve been following along with to learn all of this!