Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Instantiation is the process of creating an object from a class.
- Click on the Downloads tab to download all the code written in each video of this course.
- Common C# Naming Conventions
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
One place we can easily see object
oriented programming in action
0:00
is in video games.
0:04
This is because many video games
portray simulations of things
0:05
that might exist in the real world.
0:08
Take a tower defense game for example.
0:10
There are lots of tower
defense games out there.
0:13
They all have a few
things in common though.
0:16
The player places towers on a map,
invaders move down a path and
0:18
the towers shoot at them as they pass by.
0:22
The player wins if the towers
destroy the invaders before they
0:25
can reach the end of the path.
0:29
In order to write this game,
one of the first things we need to do is
0:31
decide what types of objects
the game should contain.
0:35
In other words, we need to determine
what classes we need to code up.
0:38
For this, it's often a good idea to pay
attention to the nouns that are used to
0:42
describe the program.
0:47
Let's take a look at
the description of the game.
0:48
The player places towers on a map,
invaders move down a path, and
0:51
the towers shoot at them as they pass by.
0:56
The player wins if the towers
destroy the invaders before they
0:59
can reach the end of the path.
1:03
Let's see here.
1:05
We have the nouns, player, tower,
1:06
map, invader and path.
1:11
We can model each of these
types of objects in code.
1:14
Let's open work spaces and do it.
1:18
To open work spaces, just click on
the work spaces button on your screen.
1:20
Let's start by creating a class for
our tower.
1:24
The first thing we need
to do is create a file.
1:28
We'll call it tower.cs.
1:30
The code for this game will go in
the treehouse defense namespace.
1:33
If you're unsure about why
I'm using a namespace here,
1:37
I suggest checking out the teachers notes
for a video about namespaces in C#.
1:41
Now, we can declare a class called tower.
1:46
Now, we have a tower class.
1:51
Right now it's just an empty class though.
1:53
Other than the name, there's nothing about
this class that describes what a tower is,
1:56
or how it should behave.
2:00
We still need to add code
to the class to do this.
2:02
It's important to remember that
the tower class is not a tower itself.
2:05
It's just a template for
creating tower objects.
2:08
To create an actual tower object,
2:11
we need to use this class to create
an object of type tower just like we use
2:14
the heart cookie cutter to
create a heart-shaped cookie.
2:18
Let's see how to do that now.
2:22
You can see here, that I've already
created a file called game.cs.
2:24
It contains the definition
of the game class.
2:30
It also contain our main method.
2:33
Remember the main method is a specially
named method that will be run first when
2:36
our program starts.
2:40
This is where we'll create
our first tower object.
2:42
We can do this by first saying
what type of an object it is.
2:45
So, Tower then give the object a name.
2:49
We'll use tower with the lowercase t and
2:53
then equals new Tower followed by open and
closing parentheses.
2:57
Remember, we end all statements
in C# with a semicolon.
3:03
This creates a new variable called
tower with a lowercase t that
3:07
is of type Tower with an uppercase T and
assigns it a newly created Tower object.
3:12
In C# the convention is to name classes
starting with a capital letter.
3:19
This is handy when creating
objects because then we
3:25
can just name them the same as their types
except the first letter is in lowercase.
3:28
We could have named this anything.
3:33
We could have named it Bob except that
that wouldn't make a lot of sense.
3:35
But we wanna give our
variables meaningful names.
3:39
For now,
tower with a lower case t will do.
3:41
Letβs review what we just did.
3:46
We just used the tower cookie cutter
to stamp out a new tower cookie,
3:48
and that's how you create an object.
3:53
Now you'll hear me and others use the
terms object and instance interchangeably.
3:56
By creating a tower object, we've just
created an instance of the tower class.
4:01
We could also call this a tower instance.
4:07
In fact, the term for creating an object
from a class is called instantiation.
4:11
We just instantiated an object
from the tower class.
4:17
Sometimes you might think that all of this
4:22
redundant terminology is just there
to confuse you and maybe it is.
4:25
But even so,
4:29
it's good to be able to recognize these
terms when you hear or read them.
4:31
Just remember that an object
is an instance of a class and
4:35
you can use the terms object and
instance interchangeably.
4:39
Let's go ahead and
create the rest of our classes.
4:43
We'll need to have one for Invader.
4:46
We'll create a new file called Invader.cs.
4:48
To save on typing,
I'll just copy the code from Tower.cs.
4:52
Paste it here and then change
the name of the class to Invader.
4:59
Let's make another one for map in map.cs
5:07
We'll also make one for path.
5:21
As you can see, we've created classes for
5:34
all of the nouns in our
description except for player.
5:36
For now, let's hold off on
creating a class for the player.
5:39
We now have five files in our project,
one for each of the classes we've created.
5:44
You don't have to create
a separate file for each class.
5:49
You also don't need to name
them the same as the class.
5:53
This is just the convention
that most people use.
5:56
You'll notice that a lot of
the way that we name things and
5:59
organize things in a C# project
are based on convention.
6:02
Conventions are not hard and set rules.
6:06
They're just things that developers have
come to a general consensus to do in
6:09
order to make the code easier to read and
to use.
6:14
Using conventions helps make the code
look more professionally done.
6:17
What we've just done is create
a scaffold for a game project.
6:21
Now we just need to fill
out the class definitions.
6:25
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up