We'll come back.
In this video, I want to explain how constructors work in a little more depth, because we have worked
with our custom constructor here, and I want to show you that you can have multiple constructors as
well as something called a default constructor.
Okay.
And therefore I want to have a separate example that will make it even clearer than doing it with this
car example, however, you can apply what we're going to learn in this video to the car example afterward.
Okay, so first of all, what I want to do is I go over and open up the solution explorer and I'm going
to create a new class.
Okay.
So just so you know how it's done, we've done it before.
But I wanted to show you once again and I'm going to call this one customer dot CSS.
Okay.
So this customer dot CSS will be an internal class customer inside of our same namespace.
Now this class should have a few properties okay.
So I'm going to use the prop keyword.
The first one will be string name.
Then I'm going to use another property here which will be string let's say address.
And then I'll have another property called string contact number.
So here contact number okay.
So let's say we have a customer inside of our database.
So we have a database with customer information a bunch of customer information.
And we have a few columns.
Right.
Because each of those properties can be a separate column.
So one column could be the name.
The other column could be the address, the third column could be the contact number.
And we could have even more columns.
So each of those columns you can now use as a property inside of your class, because this customer
class is going to then be usable to connect directly to a database and talk to the database accordingly
and get the data out accordingly.
All of that is the fundamentals that we are going to need in order to do that later on, which is why
it's important that we understand how this works.
So now how would having multiple different constructors matter here?
Well, let's take one constructor, one custom constructor.
So this one will be a custom constructor.
And this one is going to take in everything.
So all three of those properties right.
So it's going to be public.
And again what's the name of a constructor.
It is the same name as the class.
So public customer but where you can also use the C tour code snippet.
So there's an extra code snippet called C tour which gives you a constructor okay.
So enter C tour, press tab and it creates a constructor for you with the public keyword, the same
name as the class, and then to curly brackets and the rounded brackets.
So now the first one is the custom constructor where I get everything, I get the name, I get this
the address as well as the contact number.
So I'm going to have string name then string address and string contact number.
Okay I'm going to have all three of those.
So in this constructor I'm going to set all of those.
I'm going to set the name to be name.
I'm going to set the address to be address.
And I'm going to set the contact number to be contact number.
So that's what my constructor is going to do each time that we create a new customer object.
So you could say a new customer entry inside of our database.
We're going to get all three of these details.
Now what if we only get the name because for some reason we don't have any other information.
We don't know the contact number and neither do we know the address of the user.
But we do know the name.
Or maybe we do know the email address or something like that.
So would we not add that person to our database?
Well, we should still add the person to the database.
So let's have another custom constructor.
And this one will be just with the name.
So it will just have a string name.
And then inside of the constructor we're going to assign the name to be name.
So we've seen what that does right.
Where we just get a customer object.
And we then store that inside of the name property for that particular customer.
Okay.
So now let's before we go further, let's go ahead and create a few customers.
So we had our orders from earlier.
Let's get rid of all of those orders and B and W's even though we love them.
So now let's create a customer object.
So we're going to say customer.
And let's call the first one URL.
And the person will be a new customer with the name URL.
My name is Earl.
All right.
Great series, by the way.
If you don't know it, you should check it out.
So here customer URL, new customer.
And I'm going to only pass one argument and that will be URL.
So now this new customer's name.
So URL dot name will be URL.
So if I print that out let's use CW here and print out name of customer is.
And then we use URL dot name okay.
If we write that and we use it we will see the name of customer is URL.
Okay.
So we created a new object.
And now we could store that in a database for example.
Or do whatever with it, display it on a website or use it in our user interface or whatever.
Okay, so this is one customer, but then maybe we have another one and that is Frank the tank.
And now we know more about Frank the tank.
We know that his name is Frank the tank.
And we know his address.
And you see, the ID is already recommending me that the customer can have a name, can have an address
as well as a contact number.
So address Main Street one and then contact number will be (555) 123-4567.
Whatever.
Okay.
So now this new customer Frank tank.
Obviously we have a bunch more information about him and we're using another constructor accordingly
because well, we prepared both.
We prepared the constructor with just one parameter, which is just the name.
And we prepared a constructor with multiple parameters.
So if we have more information we're going to use this one.
If we only have one information we're going to use that one.
So what if we don't have any information about a customer but we still want to create a customer object.
Then we're going to look at that.
I'd say in the next video where we're going to look at default constructors okay.
So I used the name earlier in the video, but I just wanted to prepare you for when that would make
sense.
Okay.
So let's look at that in the next video.