This video is geared towards teaching you about object oriented programming and more specifically classes
objects methods and properties.
I've already created the project and it is called classes and objects so you can pause here and go ahead
and create this new project under your solution and resume when you are done.
Now let's get started with what a class is and a class is a very very important keyword when talking
about object oriented programming languages.
I would have used that expression multiple times and so this is the explanation.
Now our class is a blueprint for a data type right.
So for a bit more context we have data types we have into a string with boolean we have char right.
And so there are times when we probably need our own data type because we need to define something instead
of for a program that has unique properties and maybe even a mixture of those other base data types
that I just mention.
And so our class allows us to define our own data type which can be a combination of these other types
and properties and methods that we want.
All right so that was a very unofficial explanation of it and I hope it made sense.
But even if it is still unclear as to what it is where a bullet creates a class and I want to explain
in detail everything as we go along.
So let's just say we wanted our own class called box no note.
We already have a class.
We have the keyword class that tells us well this is a class and it's called program but I want another
class that is not programmed so I can't define a class or well it is generally discouraged to find a
class instead of one other class.
But I do need it inside of the namespace.
So instead of the curly braces for my namespace I'm just going to create another class.
Just press enter twice and then I will see class and this class is going to be called Box.
So I want a class called Box I want a data type that looks like a box.
No I did say it's a blueprint.
So when you're defining a class you need to see it yourself.
Okay I've said any class I need a new data type.
I've given it a name.
What does it usually look like.
What are the properties.
And when I say properties they are the defining characteristics of this new data type.
Well I can tell you that a box is a 3D object generally speaking and a box usually has Length Breadth
and Height.
Right.
So we know three properties that this box will have.
So did it find properties I will need the access modifier.
So we looked at one in when we're doing methods I would call it static.
Well there's another one you have well public.
You also have private.
So in the case of a class we can define it as public.
Then we give it to data types so the data type can be of another class or it can be off base data type.
So once again this is me defining identities.
So if I add another class somewhere else I could use it as a data type.
No however I'm keeping it simple so public and then my data type for a length would probably be something
like double.
And then I'm going to call it length.
Then I'm just going to say get come on sit.
All right.
And that's pretty much what that looks like.
So public double length.
And then they get there and sets are meaning this is allowing me to retrieve the value.
And this is allowing me to set a value.
All right so I can get the value from my property call the length and I can set a value and as he visits
it is kind of hinting at me that you know I'm violating the naming convention so let me see what they're
going to tell me.
And they're just suggesting that properties off classes should get capital letters All right.
So let's try that again.
No this I'm going to show you a keyboard shortcuts when defining properties instead of a class in C
sharp and Visual Studio you can write the word prop and then w top tab and then it will kind of complete
that line for you.
So he's going to highlight the data type I can just press.
All right w then press tab and then move on to the next one which is my property on this one I wanted
to see Brett and I'm using the suggested naming convention this time.
Then I press enter and then it adds it to the property.
All right so I want to try that again prop w tab and then double and then tab and then this one is going
to be height for us enter and we're done all right.
And then congratulations you just defined your first user ID find it to type or your first class.
Know that I have this user defined data type.
What do I do with it.
How do I use it.
All right.
So let us think about how we use regular data to sort of one the regular data type.
We just specify our data type and then give it a name and then maybe give it some value right.
So like when we wanted a number we would've said something like int.
That's our data type or name and then give it a value.
True.
No we want to define box as a variable or want a variable of type box so box is a data type.
No.
And we want a variable that represents a box.
So in order to initialize a new variable or what we'd call an object.
So I'm just going to write a comment here.
Create object of class type.
Box then I'm going to say something like.
BOX.
SO I'M JUST GOING TO CALL IT THE SAME WAY WE CALLED IT IS A C and where I'm just calling in the data
type and then I'm going to give you 10 name someone to give it.
Come on B box so it's very case sensitive if I define the class with I can meet with a capital B then
it's going to be seen differently.
Come on B but it could have been anything I could have said Box B box to be one the same naming convention
that we would have used to go for and how we need more variables would apply.
Here are the only exception is that they can do the case really matters a lot.
So I have my data type box.
I notice that it's got a color coded just like program.
So that's a class and then box is color coded.
Come on B box is color coded just like gnome so that's my variable are in this situation my object and
that I'm going to initialize it to a new instance of box and this step is very important.
All right so with all this step then well it would be it wouldn't work essentially.
So if I just said Box B and semicolon visual studio would kind of accept it but then nothing that we
do afterwards would work it would just throw arrows all right.
So every time you're using a new class or a new user defined object.
Sorry.
A new user defined data type which is in the form of a class that you would have created.
Then you need to initialize it by saying that class name some object name is equal to a new instance
off that class name again and open and close braces.
There are times you may see contents in the braces but this is not one of those times so I'm not going
to bring up that scenario just yet and we'll just press ahead with our objective.
So no that we have our box object or our box variable.
Know I'm going to start calling it object from no on right.
I keep on calling it variable sake and can run them into parallel but it's really an object wants itself
a class type and it's no initialized it's no one object.
All right so I can see a box and then a very very special operator that I know you've seen quite a few
times and it probably just never will mean much to you.
We have our full stop so box dot and then look at what the intelligence is doing.
It's telling me that I have access to a property you see that range that range means property called
breadth I have access to a property called height a property called length of three off which I just
defined inside of my class.
All right.
So they're telling me that I can get and I can set.
Which means that I can print box that length if I need to and I can also see a box that length is equal
to some value.
Someone to do that somewhere to set some values or box that length.
So box that length is 10 and then I'm going to say box dot width or sorry breadth is equal to 15 and
then I'm also going to say a box dot height is equal to 5.
So I'm setting values here.
So C sharp makes it very easy for you to create an object and then set values for these objects.
So when I'm putting or assigning values that is me sitting when I am printing.
That is me getting so I can actually do a console dot right line.
All right so you see that dot operator again.
And notice the color coding on the word console it looks just like the color coding on box and on program
and every other class that we would call it.
So that means the creators of the language see sharp created a class and then this class.
And I know this because after the dot upper until the sensation of everything that I can look at in
the class it has many properties and methods so the cube represents a method right.
So I have a method called right.
I also have a property called Oh it's a property called era property called this a property called that.
My class only has three properties.
This one has so many other properties that I may may never use in all honesty I have never used some
of these things in here but the tools are there so I'm just trying to show you that when you're creating
a class it is up to you.
This is a blueprint but then it gets initialized and then everything becomes real because they can actually
interact with the properties and methods you would have defined someone to say a console.
That's right.
Line and then I would just want to print out box dimensions are box length box thought breadth and box
dot height.
Right so I'm just printing about the volume.
So like I said when you are trying to view the values you're really doing I get but when you're assigning
the values you're doing I sit and look out what happens if I remove the set from length and I remove
the get from height.
All right.
So now I have an arrow because I'm trying to set a value on a property that only allows me to get right.
Yes it's public but it's seeing that you're not allowed to place a value inside this property.
And then it's giving me that arrow because it's saying oh that's illegal it's read only you can only
retrieve the value.
So it's not giving me an error when I'm printing it because I'm only retrieving here.
All right.
And then just the same way for the the height.
I only put it to set.
So yes I can put a value there but I cannot retrieve it.
All right.
So I'm just pointing that out too because when you're going to be interacting with other classes.
Well one when you're defining your own classes you're going to be very careful if you know if necessary
to know what level of access you're giving to what property.
So human being probably is.
Well let's say you would meet any human being unless you give birth to this human being.
You would have met them with a name so you don't just go ahead and sit.
Name you don't set first name on set last name but you can get that information right.
So if you were ever to define a class that maybe you call a person that looks like a human being you
know you're defining with properties of a human being on a property of a human being would be a first
name another property would be a last name.
Right.
You probably would have a first name with only get and last name we only get.
All right.
But then you probably have something is that can get full name and you would never probably sit sit
put saris put us sit on the first name on the last name.
All right so I put about my sister and might get her on my my property is in my class and I'm going
to execute this program so let me just quickly switch over they start a program all right so a very
simple program does spring to the box them Injuns are 10 15 and 5.
Just like all we define them 10 15 5 and then we printed out our sentence.
So we've been doing a lot of input output programs so you could actually modify this to receive the
three numbers as inputs and then set the values and then do the print.
So I'm going to do it.
I want you to pause this video right.
No and attempt to do yourself.
And then you on pause and then we go through it together.
All right so I've completed that exercise.
So I put it in some comments and kind of rearrange the code.
So it's always nice to have your declarations kind of one place.
I mean I do accept that sometimes you don't know what variables you need until you get to the point
but when you can just try and put all of them in one area and then I prompted for the three numbers
and then I set them and then I got them.
All right.
So let's just run that and see how that works.
So I'm getting the length and this is going to be a box that's 10 by 15 by a 5.
And then it's just into print.
The dimensions are 10 by 15 but by 5.
No.
What if I wanted to do something with those values.
What if my I wanted my box to tell me its volume so I know it's length.
I know it's breadth.
I know it's height but then I wanted to calculate the volume based on the properties know what I can
do is define a method so I can define a function inside of my class.
And just for context notice we have the class called program and it has a function.
So we went through all hold to define functions and I'm just going to use the same principles except
this time I'm inside of a class a new class body.
Right.
So the losses of functions we defined them instead of the class program but I don't want these methods
inside of program I want them inside of my class called the box and this method is going to return the
volume.
So I'm want to call it get volume.
All right.
So I want to say public and then I wanted to return a value server meal and its value returning we have
to specify a data type.
Some would you say double and then I'm want to call it get volume and then it is not taking any parameters.
Now explain why it's not taking parameters it doesn't need to take parameters because it is a part of
the class that has the properties so as as our results they're all like siblings.
The properties are siblings than normal each other.
The methods are siblings with the properties so you can actually just reference them directly inside
of the method.
You don't have to pass in any parameters because they're all inside of that box.
All right so I mean like I said this is a data type definition.
So if I have a box that says a length with a height it's also as I value all of those are still related
to this one box.
So you can think of a ladder you define the class you gave it properties maybe you gave it a method
or two functions whatever it is but they are all a part of any one object of this box.
All right.
So I'm going to say and then notice the red line because its value returned so I need to return.
And then I'm returning length times red times height.
All right so that's so we get the volume of any 3D objects.
So no that I have this value I can go back to my main phone Sean or let me put it in the area where
I'm getting the properties and then I'm just going to see it volume so I did say it's good to declare
variables in one area on this you you realize you needed somewhere else say it as they cleared so into
volume is going to be equal to our object call box when I said Dot you're going to notice that I have
get volume no available to me.
So when I say get volume it will meet that function call.
So when it's executing it gets the values from our users and then it sets the values and then we get
the volume but then notice of an error here because it's and I am returning a double but I'm storing
it inside of an int so bad that needs to be double volume is equal to get volume and then what I'm going
to do is say box volume is and then I'm just printing back the value that is inside of my variable called
volume.
All right so let's go through this again.
We have a box we declared a data object called box.
We're filling it with values and then we're calculating a volume based on those values no look I'll
need this code looks imagine if you were to write all of those things one place and it can get messy
so inside of your class you can have as many functions I could even see public maybe I have another
function that gets the area so I'm going to say get area this time of any one side and then area is
usually length by breadth so it's not the heights right.
So I have another function so I can do another one where I see double area is equal to box dot get area.
So as many functions as I will need or want I can define them inside of my class and then on on you
know on a need need by need basis I just declare my object made my function call and then get the value
and you can have void functions also.
So it's not always value returns I'm just going to duplicate this line using control D and print out
the area that's I'm getting back all right so let's try and run this program and I'm going to do something
that I know is going to throw an arrow noticed that I carelessly have been using double.
So I'm in suggesting that my length breadth and height can be double.
However I am only doing an inch that pass to the Reid line.
So this is the arrow.
If my length was one point five and I press enter then I'm getting an on handled exception error.
All right.
So we did talk about the exception handling where it's good to wrap it up in the try catch to avoid
these kinds of errors.
But just for simplicity's sake I'm just going to choose this I'll say if we have inquires fires then
guess what we also have.
We have double dot pass art and if we're doing a double dot pass we also need to have the value that
is being passed going into a double.
All right.
So let's just changes all to doubles.
Like I said is very strict.
And then we just change them all to double.
So we have double anyone doing a double pass and double double double double double.
And everything is no relative to doubles.
So let's try this again so the length is one point five and the breadth is three point four and the
height is nine.
Just some random dimensions.
So now I'm seeing that the dimensions are those three values the area are the volume sorry is forty
five point nine.
So that's one point five times three point four times nine.
And then the area is five point one because that's one point five times three point four.
So now we have some data tab that represents a box.
Well guess what.
What if we had two boxes or more than one boxes or we had a collection of boxes.
The fact is that we can define as many boxes as we need.
So I'm going to say this is box.
And then there's a second box that I need to be aware of for work or dimension as far as I can see.
Box do.
So there is nothing stopping me.
It's just that all we can have multiple integer variables multiple string variables.
You can have multiple objects and misspellings defeating me.
You can have multiple objects of the same data type.
As long as the names are not the same.
Right.
So I can have box box to box five box ten.
No.
Imagine trying to fill in the dimensions of a warehouse full of boxes.
So you know that there are 10 boxes and you have to write a program to allow them to enter the dimensions
for 10 boxes.
Then you're going to have box box to box three box for a box five and then you went off problems per
box because then for Box 2 and went to have to repeat all of these steps just to make sure that I'm
getting 4 box 2.
So there is an easier way.
I'm just giving an idea of how tedious it might get but I am just saying that that is what at this point
we would have to do.
So I'm just going to proceed with Box 2.
So we have the promise for the vies for the box dimensions and we're sitting and printing the values
already sitting and calculating the values for our box.
So I'm just going to duplicate all of this and I'm going to do it again underneath.
Right but then this time I'm going to change these zones.
I'm not going to read it clear in 1 and 2 and 3 so like I said if you have to reuse a variable you can
actually just forensic by name after it has been declared right.
So we declared it here.
We're not declaring target.
We're just using it again.
So I'm just making reference to it and then I'm changing these old to box one.
So it's is it Box 1 box 2 Sorry.
So we're changing these references out to Box 2 and then once I'm done this I'll just explain quickly
exactly what is happening here and then I'm going to duplicate all these print lines we're all right.
Where I'm going to be printing bucks to dimensions and then notes I'm going to have to overwrite.
So I'm not going to do an override what I'm going to show you that you can actually print directly from
our return.
Right.
So open to know what I've suggested that you put the return value sermon but I get volume is a function
that returns something so in it returns that value.
I notice I didn't use a variable this time I just returned the result of this calculation and in that
result of the calculation is going to get stored inside volume no don't hear I'm printing volume and
I'm printing era which we know are for box the first box I went to remove those declarations for the
second box so all I'm sitting is the height the breadth and the length but then I'm printing box to
dots and then I'm just going to call a function right there get volume and then I'm going to print again.
Box dots.
Sorry.
Box 2 dot get area.
All right.
So then I'm just showing you that you can print the variable you can do the return call get it storage
in a variable and then manipulate the variable which might be more desirable if you have you know all
the things you need to do with the value but then you can also just return it and have it print directly
inside off your print operation.
Right.
So you have those two.
And of course you have to know when you're using which method.
So let's run this program once again where have two boxes.
So have two boxes of of the data type that I just created called box.
I'm going to print prompts to enter values for the first box and then I'm going to calculate the volume
and the area that I want to prompt again to enter values for the second book.
So I mean I can modify the prompt to say four box 1.
All right so I'll just do that and then repeat that for the problems for Box 2 and then after getting
the virus for Box 2 we're just sitting the dimensions accordingly and then we're just going to print
box one dimension box one area box one volume and then box two box two and box.
So I'm just adding these so that we have some visual cues as to which values our full box one and which
one is our full box.
So let's run this and see how that works.
All right so I've already entered some of the values 5 6 and 3 4 dimensions for box 1 and then I want
to see if 5 4 and 10 4 dimensions will box to press enter and then we can see here Box 1 dimensions
are these 3 and then Box 2 dimensions are those 3 and those are the calculations issue that I'm still
getting the area and the volume even though I'm just printing directly into the console or at line the
return value.
All right.
So make sure don't get confused by the curly brace and that that we're making a function call instead
of one sold out right line here.
So when it gets to this line and it starts writing the text you realize is that all I have to make a
function call.
Let me go to the function and then that's going to return some value and then whatever it returns is
what gets concatenated into the string that is being written to the console so like I keep on saying
there are many ways to do things.
I think that you know getting it back answering it in a variable is much cleaner much more much easier
to read than something looking like this.
But once again you have that method available to you know as a final item when to display it to you.
The best way to handle your classes.
So you would have had to go through the painstaking explanation of of seeing why we should not define
another class instead of this class.
Right.
So we have class program and to go through you know quite a bit of record restructuring to make sure
that I was not inside.
It's fairly obvious what's inside of the namespace brace.
And it is actually best practice it leads to much cleaner looking project files when I go to court and
just court in general when you create a new file per class file so notice that we have a class called
program.
We have a corresponding file in our project called program.
Yes.
Which then means any class that you create it is best practice to create a new file.
Call it that class name dot see s and please your definition of a class inside that file.
So I'm actually going to do that right.
No someone to take this class that we just created and I'm going to cut it.
So all of the code associated with the class I'm just going to do control X and I'm cutting it from
inside me.
And then we if we give visual studio a few seconds and everything is going to go haywire it's going
to be low definition for a bit the type period that doesn't exist.
And then we're going to see a visual studio it's fine we know what we're doing so we're going to go
over to our project classes on objects right click and then click add.
And then what we're doing is adding a new class you see that class.
So we click us.
And then once again whatever the name of the class is it is best practice to give the file that name
someone to call this glass box.
So then it's going to go ahead and create a new file and it's going to even go as far as filling in
the name of the class the box because contextually it's already naming the class.
So I'm just going to replace this predefined code and notice it seeing for references and that's because
just by me creating that class and box not properties are anything in my mean it automatically takes
that in the data type is box so I know what I'm doing but then it has no properties it has no function.
So everything is still haywire.
So I'm just going to replace this block of code with the class that I just created.
And then when I go back over to my main then everything is fine.
So once again when we're able to create the class is actually better to just go to our project right
click go to add click class your class name so let's say I created a class called Person.
All right.
And then do all of our definitions all of our properties.
And I just filled the three properties quickly I didn't want to bore you with writing them so you can
pause and write them off if you need to but their first name last name and age.
And then I'm going to declare another one and this time I'm using a different access modifier and this
one is going to be called private.
So private would be celery for argument's sake.
So stubble and SA right.
All right.
So I'm defining these as public and these as private.
Know when something is private it means that nothing outside of the class can access it you cannot directly
access this property from the object.
All right.
So let me just put that into context for it.
So I want to go back to program.
I'm not going to do too many modifications because person is coming on the tail end of this lesson.
But let's just see what happens someone to say a person I notice it takes person because it's in a class
file it's in the same namespace so they know about each other and their siblings.
So person and then as usual I have to declare a new object off person someone to say capital P person
that's the name of the class.
Come on peepers and that's the name of the object is equal to a new instance of the person.
All right.
So I can say a person the object and say dot and then in tennis consistently me exactly what I can access
I can access the age because that was public I can access the first name and last name because those
two are also public.
But I cannot access the salary because it is private.
Right.
So if I go as far as writing salary because I'm defying visas through the N C sharp I I'm going to say
right.
And I want to see salary then is going to say that it cannot it is not accessible because of its protection
level.
So because I said it's private.
That's private information.
I can just go on and say I want to see salary.
All right.
So usually when you have private properties or a private member feels inside of a class then you mean
what you call get their function.
So where are already defined that we can have a default get a default set.
Once we define our public property.
But then when it's private you probably don't want to do it like that.
So you can't actually do something like defining a function.
I'm just going to call this public and call it double and then when to say get salary and then this
public function that I should be able to access when I say person dot will return salary so return salary.
All right.
And then what if I needed to set the value for a salary.
Remember it's private and it doesn't have any getter and setter so usually they would actually create
another function and they call this one set.
So you can see public double get salary and this is going to return.
But then this one can be void because I don't need to return anything when I'm sitting the salary.
However I'm going to require that you tell me home What salary I'm setting for this person.
So I need a value from use.
I want to see.
Send me a parameter and I'm going to say give me salary.
Come on s and then no return.
But I'm sitting the capital S salary which is my private member field to have the same value as the
salary that is coming in through the parameters.
So salaries equals a salary.
So a good naming conviction actually when it comes to private members is that you use on the score and
not capital letters.
So the public accessible ones would have the capital letter but once it's private to use an underscore
and come on.
So let me just change that's out and I mean you saw that it worked either way.
So it's not so much of it's working versus not working is just a matter of.
This is the standard so somebody else comes along looks out to your code they know exactly what they're
looking at at a glance.
All right.
So private double salary that his or one private member field and we need a function to get the value
that is there how we need a function to sit of value in there.
So back in the program I can see a person Dot and I will only have access to any methods or properties
that have the public identifier or access modifier sorry of indicator on their declaration
so that's it for this very very
so that's it for this quick introduction to classes and objects in C sharp.
I'm going to encourage you to just think of anything.
Think of a pit and then define a class called pit and then start writing all the different properties.
And I like to think of the properties as the knowns and the methods as the verb right so the known would
be like something that is there.
So a box is always wins out but then it's always lands after birth.
It's always good to have a height but then getting the volume and getting the area or in the air in
the student of the person getting the salary or setting the salary or even getting full name.
So let me just type that one.
I want you to complete this string.
Public string.
Get full name.
So I want you to define this function to get this person's full name and I want you to share your code
with me.
And then we can go through any difficulty you had together.
So once again the methods are more like verbs because this is an action getting the full name is an
action but the name is always there.
That's more like unknown.
All right so that's a challenge for you.
Let me know if you have any.