In this video, we're going to look at another member of a class.
And the member is called methods.
So there are member methods or member functions.
So functions are like methods.
They do the same thing.
The only difference is that functions are not part of a class.
But once a function is inside of a class, it's called a method.
Okay.
So this is just if you ever hear those two words combined or whatever, or connected or in other programming
languages, they are very often just referred to as functions.
But as soon as it's in a context of an OOP.
So object oriented programming project, then they are called methods.
Let's go ahead and create a method inside of our customer CS file.
We know that we can have a default constructor called customer where we just set default values.
But what if we want to set values after the fact?
So let's say we want to set those values like name, address and contact number after the fact.
So after the object already has been created, because the constructor is only called once and that
is when the object is created.
So whenever we use the new keyword.
So whenever we have this here new customer that's when the object is created.
But we cannot use the constructor after that for that same customer object okay.
The constructor is called only once during the creation.
Now what if we want to set the details after that and we don't want to do it like we do here, but we
do it with one method.
Well then we can create a method public void, set details for example.
And then for that customer we could have all of the details.
So we could have the name, the address, the contact number and so forth.
And what we want to do is we want to basically do the same thing as we did with our custom constructor,
where we have gotten the name, we've gotten the address, the contact number, and we basically stored
it inside of the properties name, address and contact number.
So those properties that we set up here, we would just use the default getter and setter.
We didn't override how it was used.
We didn't say okay if you well if the name is empty, don't accept that name.
Or if the address is empty then just set it as a random value or a default value or something like that.
We could do that as well.
You know how to do it.
You've seen it when we were looking at properties, but coming back to our new method that we created,
it's called Set Details.
Now, this method here is now a method that belongs to the customer class.
And whenever we use that method, it will modify the customer object of on which we are calling that
method.
So let that sink in.
What does that mean.
Well let's look at it step by step.
Therefore we now have this method.
And we made it public so that we can access it from another class okay.
So this is the access modifier public which allows us to use it from the program.cs file.
Otherwise if it were private for example, we couldn't do that.
We couldn't use the set details from another class.
So we're going to look at that later on.
But just wanted to quickly go through the entire line of code here.
Then we say the return type is void, which means that this method will not need any return here.
Okay, no return keyword or anything like that.
Then we give it a name and again we start with a capital letter and we use camel case here.
After that we do have our parameters and we have the method body okay.
So everything inside of those curly brackets is the method body.
Now let's go ahead and use this method inside of the program CS file onto our customer.
My customer okay.
So what we're going to do now is we're going to say my customer Dot.
And you can see it gives us all of the properties that we can access address contact number and name.
And there is this new thing set details which is our method.
So we can now use this set details method.
And we can pass the name the address and the contact number.
So let's do just that.
Let's add a name.
Let's call this one Frank.
And then the address will be Main Street two.
And the contact number will be five five, five and so forth okay.
Any given number.
And then we end with a semicolon.
So now this here is how we called the set details method.
And we now are going to override the values for my customer who is my customer.
My customer is an object of type customer that we have created.
Now this will not modify how the class itself works.
So obviously this method will modify it because now we have a functionality that we give.
The class.
So this class can not do something.
It can now set details in this particular example, but it's not going to modify neither URL nor Frank
the tank.
And let's have a look at that.
Let's use Console.WriteLine statement.
So my customer is and then we get for example the my customer dot name plus and he and he lives in plus
customer or my customer dot main street or Dot address okay.
So I'm not going to go further.
But that's going to be enough.
And now let's do the same thing for our URL okay.
So URL is going to be URL dot name.
And here it's going to be URL dot address.
And we comment this line out as well.
So I just want to show you what's happening here.
Because here you see my customer Frank and he lives in Main Street two.
And then it says URL is URL and he lives in empty because we never defined the address for URL.
And we also didn't use the default constructor where the variable would have been assigned.
Okay, good.
So this is a very simple example as to how we can use methods.
We're going to look at methods again at a slightly different angle with our car, because I want to
really make sure that you understand how methods work in the context of classes, because they work
differently to what we have used before.
Before they were just there to just take a bunch of code and make it reusable.
And now it's we're taking a bunch of code that is reusable, but we are framing it into or putting it
into a class.
So it only modifies whatever is inside a class or does something in combination with that class.
So whenever we're using an object of that class, so URL is an object of our customer class.
And now we can use URL dot and then the name of our method to do something with it.
The same goes for my customer.
It's an object of our customer class.
So now I can use my customer dot.
And whatever the method name is that we have defined and do something with it, modify my customer with
it, or make my customer do something with it.
Okay, so that's it for this video.
See you in the next one.