In the previous blog entry we went into our SQL Database and created a table to hold our tasks data. Now we need to move back into Visual Studio so that we can create a “class” within our module project. This “class” will serve as a definition for the objects that will represent tasks within our module’s code. You could also think of each task object correlating with one row (a task) in our “Tasks” table in our SQL Server database.
If you’ve got knowledge of OOP (Object Oriented Programming) this information is probably not new to you. Though, if you don’t have a background in OOP hopefully this blog will help you understand some of the concepts and I’ll provide some helpful links to OOP resources at the bottom. Also, as you get more into code you will pick it up faster than you think so stick with it even if things get a little fuzzy!
So we’re storing tasks in the database and we need to represent these tasks as “objects” before we send them to the database to get stored.
Classes & Objects
To actually create the class in code isn’t that difficult, but to understand what’s going on is the main point here. Classes provide the definition of our objects. That is a class defines the properties & functions that our objects have. In our case, the same data points that we hope to capture and save to SQL will be properties of the objects that will exist in memory before being sent to the database. Likewise when we retrieve data from the database we will create new objects and populate their properties with the data that comes back from SQL. So, to be repetitive for good measure, the term “class” is the definition or blue print for our objects. Whenever we create a new task object it will be created with the properties and functions that the object’s blue print (its class) has defined.
Creating the Task Class
To create a new class in Visual Studio we simply right click on our module project and select “Add” then “Class”. Side note: there are classes for nearly everything so get comfortable with these concepts and don’t worry if your module has dozens of classes.
Let’s create the “Tasks” class in Visual Studio. If you take a step back and think about organization, since there are (or can be) lots of classes, we should probably create a folder to store these files. So before we even create a class let’s create a new folder so that we can store these classes in an organized manner and keep things tidy. To do this just right click on the project and choose “Add” then “New Folder”. Now we need to name the folder and in other tutorials online I’ve seen them named a lot of different things like “ojbects”, “models”, “classes”, ”repository”, etc. You can name the folder whatever you like I’m just giving you this information in case you download a module from the forge and see various folder names that hold all these class files.
I’m going to name the folder “models”, but you can name it whatever you would like. Create a folder and once you create the folder then right click on the folder and click “Add” then “Class”. Then give the class name the name of “Task.cs”. Congratulations, you’ve just created your first class! Now the class currently doesn’t have any functionality, but nevertheless it is a class. As you would imagine we now need to add in the properties of our task class. So we need to add the same data points that we are capturing in SQL to our class as properties. Your class should look similar to the following:
Public & Get; Set;
As I’ve mentioned in the video below the class consists of a few items that may look odd to you if you don’t have a strong OOP background. The terms “public” and then that “get; set;” thing may throw you for a loop. I’ve included some specific links below about class modifiers, properties, accessors, and auto-implemented accessors that will help you understand more.
Now that we’ve created our class we are ready to create task objects for use in our module and we are getting close to being able to actually retrieve some tasks from the database and show them through our module, but we have a few more steps left. So hang in there because we are indeed making progress.
OOP Helpful Links:
Go to the Next Blog Entry: Creating the GetTasks Stored Procedure