How to Program in C# – Classes (E07)

How to Program in C# – Classes (E07)
Spread the love

– Congratulations, you’ve made it to the eighth  and final video in this seizure programming course.  In this video, we’re going to be talking about classes.  Classes are extremely useful  because they allow us to build our own data structures.  Now, what do I mean by that?  Well, C sharp is an object-oriented programming language.  This means that we try to use code to describe objects  and we mostly do this using classes.  Say for example, we’re creating a video game  and would like to add some cute cats to the game.  Well, in this case, we would create a cat class  that tries to describe what a cat is.  Think of it like we are creating a blueprint for a cat.  Then when we want to spawn a cat into our world,  we do this based on the blueprint.  This is called creating an instance of the class.  Now that might sound very abstract, but I promise you,  it’s going to make sense in just a second.  In fact, let’s just have a look  at how to create a class right away,  but first this video is sponsored by the Unity Asset Store.  Soon, the Asset Store will be launching a new feature  called, Support the Creator.  This feature allows users to give the creator of an asset  more than the original asking price.  This also includes free assets  and a hundred percent of the additional amount  will go directly to the creator.  So keep an eye on the Asset Store and Unity socials  for this announcement.  This is such a great feature,  as it’s the perfect way to thank the creators  for taking the time to make assets  that help you with your projects.  And to celebrate this new feature,  we’ve just launched a free 2D Mega Pack on the Asset Store.  The pack includes all 2D Assets we’ve ever created  for tutorials or game projects,  everything from environments to characters,  to various items sprites and UI,  plus a few sounds to get you started.  And of course the pack will use the support  the creator feature as soon as it goes live.  So I definitely encourage you to go check out the pack  on the Unity Asset Store.  Simply click the link in the description,  and of course stay tuned for the new feature  also specially thanks to Mastro2k  for his support on Patreon.  So to create a cat class, meaning the blueprint for our cat,  we first need to think about how we want to define a cat  in a programme and then try to describe it through code.  In my case, I think a cat should at least  have a name and an age.  And these are things that we can describe using variables.  We probably also want our cat to be able to do some things.  In my case, I would like it to be able to say meow,  and this is something that we can describe using a method.  So let’s try this out in code.  To create a class in code, we first need to go outside  of the programme class we’ve been using so far.  Then we write class followed by the name.  In my case, I’m just going to write cat  and then some curly brackets.  And that’s it, we’ve now created a cat class.  Now we can add some code in here that describes a cat.  So we’ll create variables for the name and age  and note that I’m putting public  in front the variables here.  We’ll talk about why later in the video,  then we can create some methods as well.  Let’s create a meow method  that simply prints out that the cat says meow.  Not that I’m putting public  in front of the method here as well.  Again, we’ll talk about why later  and that’s it for the blueprint of our class.  If we just run our programme now  nothing is going to happen though.  This is because after creating the blueprint,  we also need to make a cat object based on that blueprint.  Again, we call this creating an instance of the class,  and this is also what makes classes so powerful  because we can create many instances from the same class.  In our case, we could create two different cats  using the same cat class.  To do this in code,  we simply go to where we would like to create our cats.  In this case, let’s just do it inside of Main.  And from here, it’s just like creating a variable.  We write the type of the variable first,  In this case, it’s a type that we’ve created ourselves  called cat, then the name I’m just going to call it cat one.  Then we can set it equal to a new cat,  open and close some parentheses and put in as semi colon.  And there we go, we’ve now created  an instance of the cat class called cat one.  From there, we can access properties and methods  inside the class by using the dot.  This allows us to set the name and the age  and to call in the meow method.  As you can see, cat one is a kitten named Cat Stevens,  and we can easily create another cat  with different properties.  Cat two is an older cat named Meowly Cyrus. (laughing)  So that’s the basic idea.  I know that this is probably a bit hard  to wrap your head around at the moment,  but don’t worry, you’ll get the hang of it in no time.  So let’s jump into VA’s code and try it out ourselves.  So let’s imagine that we’re making a game in the fantasy  (mumbles) and we want to create a wizard.  Well, in this case we would probably create  some kind of wizard class.  So to do that, we go outside of our default programme class.  I’m just going to go to the top here we write class,  then the name of the class in this case,  it’s going to be wizard.  And then we open and close two curly brackets.  And in here we can start to describe our wizard  through code, so first off let’s create some variables.  So I’m going to create a public string,  storing the wizard’s name.  I’m also going to create a string which is going to store  the wizard’s favourite spell.  I’m also going to create an integer,  which is going to keep track  of how many spell slots we have left, and finally,  different wizards might have different experience levels  and might be able to level up as we proceed in the game.  So I’m also going to create a public float  called experience.  And this is actually already enough first to start using  this wizard class.  We’ve now declared this class  and are ready to instantiate it.  So let’s go inside of main here  and try to create a wizard based on this class.  I’m going to type in wizard, then the name of the variable,  in my case, I’m just going to go wizard one  and I’m going to set it equal to a new wizard.  And at this point we can use the dot.  So wizard one dot to access to different properties  that we’ve created for this class.  So I’m going to first offset the name to something like  Parry Hopper.  I’m also going to fill out the other variables.  So we’ll do the favourite spell.  I’m going to do Unexpecto Patronum.  We can also give this wizard some spell slots.  So we’ll go wizard one dot spell slots equals,  and let’s just default this to two.  And finally we can set the experience level  of our wizard to zero.  And remember whenever we are changing a float,  it’s a good idea to put an F right after the number.  When we’re just setting it equal to zero,  this doesn’t matter,  but if we were using a decimal place here,  it is going to give us an error  unless we remember to do that.  But again, I’m just going to set it equal to zero.  So we’ve now declared a wizard class.  We’ve created an instance of it  and set a bunch of the properties,  but the wizard is still pretty boring  because there are no methods for us to use.  So let’s go ahead and create some methods  that describe what we want a wizard to be able to do.  So we’ll go inside of our wizard class here  and we’ll create a public void  and let’s give him the ability to cast a spell.  So I’m going to write cast spell here,  and let’s just print out that we cast a spell.  So we’ll go console dot writeline,  then the name of the wizard plus casts,  and then the name of the favourite spell.  But remember casting a spell requires a spell slot.  So right after we’ve cast a spell,  let’s going in and decrease our spell slots by one.  So we can go spell slots minus equal one,  or just spell slots minus minus four short.  So now every time we cast a spell,  we’re going to decrease our spell slots.  In fact, let’s try this out.  So inside of our Main here, we’ll go and access wizard one,  we’ll call the cast spell method  and just show that this changes are spell slots,  let’s just print out how many spell slots  we have before and after.  So I’m going to go console dot writeline spell slots,  and then wizard one dot spell slots.  And I’m going to take this line and past it  after we cast a spell as well.  So if we now run this programme,  we can see that we start out with two spells slots,  then Parry Hopper casts, Unexpecto Patronum,  and now we only have one spell slot left, awesome.  And we can actually use this to determine  whether or not we’re even able to cast a spell.  So inside of our cast spell method here,  let’s create an if statement that says  that we only want to cast a spell  if our spell slots variable is greater than zero.  So we need to have at least one spell slot  in order to cast a spell.  And if this is the case, well,  then we can go ahead and put in our spell casting code.  And if it’s not the case,  so we’ll go ahead and put in an else here.  Well, then we can write out some things like  console dot writeline.  The name of the wizard is out of spell slots  and therefore we can’t cast the spell  and even cooler, whenever we cast a spell,  we are probably gaining some valuable wizard experience.  So let’s go ahead, when we cast this spell,  other than just reducing the spell slots,  let’s also increased our experience a bit.  So I’m going to increase so plus equals our experience  with something like 0.3.  And if we test this out now, we can go into main here  and let’s remove these console dot writelines here,  and let’s just try casting, say three spells,  and then print out the amount of experience we’ve gained.  So console dot writeline experience,  and then wizard one dot experience.  So now we’re just casting three spells  and checking the experience.  And if we run the programme, we can see that Parry Hopper  casts Unexpecto Patronum two times,  and then he’s out of spells slots.  And that’s right, because if we go ahead and check,  we set the spell slots to two.  Then we cast two spells and the third time  it simply says that he’s out of spell slots.  And therefore we only increased the experience  by 0.3 two times,  and so our overall experience gained is 0.6, awesome.  Of course it would make sense for us to create  a way that our wizard can gain back spell slots.  So to do that, let’s go ahead and create another public void  create another method here and let’s call it meditate.  And this allows our wizard to sit down  and chill out for a bit to regain some spell slots.  So in here we ride console dot writeline  then the name of the wizard meditates to regain spell slots.  And then we set the spell slots back to two.  It’s that easy, so now we can try casting these spells here,  and then we can maybe try meditating  and then casting some more spells  and seeing how much experience we end up with.  So let’s try and run this programme.  We can see that we cast two spells,  then we run out of spell slots.  Then we meditate to regain them  and we’re able to cast two more spells.  And our overall experience is now 1.2, awesome.  Now while this code is working,  we’re currently using a lot of lines to create a new wizard.  So at this point it would be a good idea  to create a class constructor for our wizard.  Constructors basically allows us to choose  what happens when a class is created.  Most of the time we use this to set some variables,  right When the class is instantiated,  just like we’re doing here.  For example, when creating a wizard,  we always want him to have a name and a favourite spell.  It doesn’t make any sense to have a wizard  where these variables haven’t been assigned a value.  Also, we probably want to make these spell slots  and experience a default value instead of having to set it  every time we’re creating a wizard.  And we can make it quick and easy to do these things,  using a constructor, so to create a constructor,  we go to our class and I’m just going to create this  right after our variables.  The syntax for this is actually pretty easy.  We go public, then we write the name of the class.  So in this case wizard, then we open and close  some parentheses and some curly brackets.  So notice that the syntax here is almost the same  as if we were creating a method.  We’re just not writing the return tab.  And when writing a constructor,  I like to think of it like we’re writing a method  that is called write when the class is created.  So what does this allow us to do?  Well, it allows us to simply set our spell slots  equal to two right in here,  and we can set our experience to zero as well.  So now we don’t need to do that down here  because it’s automatically done  when we’re creating a new wizard,  but I’d also like us to be able to put in a name  and a favourite spell inside of these parentheses  instead of having to do it on separate lines like this,  where we might forget.  So to do that, we go up here  and just like when we’re creating parameters for a method,  we can create parameters for constructor as well.  So I’m going to type in string here,  and I’m going to allow ourselves to type in a name.  However, if I call this variable name,  then both the variable up here  and the parameter is going to be called name.  So instead, it’s pretty standard  to just use an underscore here.  Two is the differentiate between the two.  So I’m going to create an underscore name  and a string called underscore favourite spell.  And now inside of our constructor,  we can set our name equal to the name that was passed in.  So name equals underscore name and our favourite spell  equal to the favourite spell that was passed in.  So favourite spell equals underscore favourite spell.  And what this allows us to do now is down here  when we are creating our wizard, instead of these two lines,  we pass in this data as parameters.  So the name is going to be Parry Hopper,  and the favourite spell is going to be  Unexpecto Patronum, awesome.  And you can see just how much cleaner  that makes creating a new wizard.  And if we run our programme, it works exactly the same way.  Now I’m just going to get rid of some of the code here  to simplify it a bit.  So we’re creating a wizard and we’re casting one spell,  and we can easily create a second one.  So we’ll go wizard, wizard two equals a new wizard,  for the name here, I’m going to type in Glindalf Marlinson.  And the favourite spell here is going to be Abracadabra.  And we’re going to have him cast a spell as well, awesome.  And again, while this code is working,  so far, all the variables and methods  we have created have begun with the word public.  If we have a look at our class here,  all of our variables start with public  and all of our methods do as well.  Well, this is called an access modifier  and it defines where we can access  the variable and methods from.  So far, there are two access modifiers  that you should know, public and private.  Public means that we can access it from outside the class.  This means that we can change the variables  and code the methods by using the dot,  just like we’ve been doing inside of Main.  We can go wizard one dot  and see all of the methods and properties.  Private on the other hand means that we can only access  the variable or method from inside the class itself.  Notice how our spell slots aren’t appearing here.  That’s because I just changed that to private.  And so you can’t see it from outside of the class,  but we can still access it from the inside.  So all of our methods changing this variable  inside the class will be just fine.  And because the spell slots and experience  should only be changed by casting spells and meditating,  it would probably be a good idea to change  the access modifier of these variables to private.  So I’m going to do that.  One thing to note though,  is that we can also simply remove this keyword.  This is the same as writing private.  If nothing is here, it just defaults to private.  So for now I recommend that you do this explicitly  just to be really clear about what’s going on,  but you can of course do the shorthand version  and not write anything.  Now, another thing that we used a lot  in our previous video on methods, was the static keyboard.  We currently see this down here before our main method.  Now the static keyword can be used  in addition to public and private.  When we add static to a variable,  it means that the variable belongs to the type itself  rather than to a specific instance.  So what does that mean?  Well, I like to think of it this way.  When we add static variable,  that variable is now shared  by all the instances of the class.  This can be really handy.  Say we want to keep track of how many wizards we’ve created.  In this case we could use this static variable to do this.  So inside of our wizard class,  let’s create a public static integer  and call it something like count.  It’s standard practise to always capitalise  the first letter in a static variable.  Of course, whenever we create a wizard,  the constructor is called,  and so we can go into the constructor here  and increase our count variable by one.  So every time we are creating a wizard,  our count is going to be increased by one.  And whenever we’d like to know how many wizards  we’ve created, we can simply access count  by using the wizard type, let’s try this out in Main.  So if I go down here after we’ve created the two wizards,  I can simply write out how many wizards we’ve created  by going console dot writeline  and then passing in wizard dot count.  See how we’re not referring to a specific wizard instance,  we’re just accessing the general wizard class.  This is what a static variable allows us to do.  And the same thing applies to methods.  When we use console dot writeline,  we’re accessing a console class  and calling a static method called writeline.  We don’t need to create an instance of console to do this.  So you’ll definitely encounter the static keyword  a lot when programming,  and it does take a bit of getting used to,  but now you know what the general idea is when you see it.  And if we just try and run this programme now,  we can see that Parry Hopper casts Unexpecto Patronum,  Glindalf Marlinson casts abracadabra,  and we have created two wizards, awesome.  Now, since this is the last video,  this week’s challenge is going to be a bit different.  I of course, encourage you to play around with classes.  In fact, I think you should expand on the idea  of a wizard class to include functionality  like drinking potions, casting more spells,  taking damage and so on.  But most importantly, I challenge you to think of a project  that you would like to make, and then get started making it.  It doesn’t have to be too complicated.  In fact, I recommend that you keep it fairly simple,  but you now have the skills  to start creating your own programmes.  There is of course, so much more to learn,  but it’s time that you start putting your skills  to good use.  So whether you want to continue making console applications  like calculators, text-based RPGs, or to do lists,  or you have a specific software you want to get into  just get started.  And of course, if you want to make video games,  I really recommend you check out our video series  on creating your first game from scratch using Unity.  It uses C sharp, and you should be more than ready  to start that journey.  Well, of course, I have a link for that in the description.  So good luck on your future development endeavours.  And remember that the most important thing is to have fun  and that’s pretty much it for this video and this series,  if you enjoyed it, make sure to subscribe  and ring that notification bells,  so don’t miss the next one.  Also, don’t forget to check out our 2D Mega Pack  on the Unity Asset Store,  by using the link in the description.  And remember to keep an eye out  on the support the creative feature.  Other than that, thanks for watching  and I will see you in the next video.  Thanks to all the awesome patron supporters  who donated in August and especially thanks to Dante Sam,  Lost to Violence, Loved Forever, NiftyliuS, Scott McKee,  Faisal Marafie, Replica Studios, Leo Lesetre, Nubby Ninja,  Jason Uritescu, Piano Sathornlak, Bobby Reynolds,  Donatien Gascoin, Marc-Antoine Girard, Jacob Stanford,  Michail Korobov, Naoki Iwasaki, Gregory Pierce, Owen Cooper,  TheMightyZeus, Alison the Fierce, Erasmus, I Love Brackeys,  SiriusWolf, Fred Mastro, Hassaan Sher, Stormed Daniels,  Dennis Solomon, Game Dev Tutorials and Max Randolf.  You guys rock.  

admin

Leave a Reply

Your email address will not be published. Required fields are marked *