Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Methods that are called directly on a class name are static methods. Don't confuse them with instance methods which are called on unique instances of the class (AKA objects).

0:00
Towers can only shoot so far.

0:02
So we need to know if an invader is in range of a tower.

0:06
For that we need to calculate the distance between two points on the map.

0:11
We should write a method to perform this calculation and

0:13
return the distance between two points.

0:16
First we need to decide where to put this method.

0:20
Take a moment to think about which of these classes this method should go in.

0:25
We should think about which class has the most in common with this method.

0:29
This method will only deal with points.

0:32
So the natural place to put it is the point class.

0:35
That way we can ask any point how far away it is from any other point.

0:41
This method will need to be public.

0:43
Since we're only working in whole units it will return an integer.

0:47
We'll call our method DistanceTo.

0:49
And it will take x and y parameters.

0:54
So this method will need to calculate the distance between the coordinates passed

0:59
in and those inside of the point object.

1:02
For that we can use the Cartesian distance formula,

1:05
which tells us exactly how far two points are from each other.

1:10
I'm having a little trouble remembering how to do the Cartesian Distance Formula.

1:15
Let's search for it using Google.

1:18
So we'll go to Google and type in Cartesian Distance Formula.

1:29
Here's a link that looks promising.

1:33
Now I remember,

1:35
it's the square root of the sum of the squared differences of each component.

1:40
Let's code up this formula by working from the inside out.

1:44
The innermost operation involves subtracting

1:47
the x components from each other.

1:50
Let's do that and assign the results to variables.

1:52
So we'll say int xDiff.

1:57
Difference is a long word so I'll just shorten it to Diff.

2:00
Equals X x and I’ll do the same thing with y.

2:16
So this is subtracting the x and y that was passed into this method from the x and

2:21
y that are in the point that this method was called on.

2:27
Let's look at the formula again.

2:30
We need to square the differences.

2:34
Squaring a value is just multiplying it by itself.

2:38
We can do that for both of these pieces and then assign the results to a variable.

2:42
Let's call it xDiffSquared.

2:52
XDiffSquared will be xDiff * xDiff.

3:01
Will do the same thing to get yDiffSquared.

3:13
Now the formula says that we need to add them together and take the square root.

3:19
How do we take the square root of a value in C#?

3:22
That sounds like a good question for Google.

3:26
Let's go to Google and type in C# square root.

3:33
The top result is from the Microsoft Developer Network.

3:36
And says, return the square root of a specified number.

3:39
That looks like what we want, let's click on the link.

3:43
Looking at this we can tell that it takes a parameter of type double and

3:49
returns a double.

3:51
We can also see that it's in the Math class which is in the System namespace.

3:56
If you take a closer look at this method definition here, you'll notice this static

4:00
keyword between the public access modifier and the return type.

4:04
Math.Square root is what's known as a static method.

4:09
Static methods are different than the methods we've been creating.

4:13
They're different because they're called directly on the class.

4:17
We don't need to construct an object before we can use them.

4:21
This starts to make sense when we look at how they're called.

4:24
Let's go down here and look at an example of how it's used.

4:29
As you can see, they typed Math.Sqrt.

4:33
Math, with a capital m, is the name of the Math class.

4:38
There's no Math object being used.

4:40
The square root method is being called directly on the class.

4:45
This is what makes static methods special.

4:48
We've actually already called a static method in our code.

4:52
Take a look at the Game.cs file.

4:56
We called a static method when we called WriteLine here.

5:01
Console is the name of the class.

5:04
And we're calling WriteLine directly on the Console class

5:08
without constructing a console object.

5:11
Contrast that to the way we use the OnMap method.

5:17
The OnMap method is not static.

5:20
So we had to construct a map object here.

5:24
And then call OnMap.

5:28
In fact, the Main method, which is the first method called in the program,

5:32
has to be a static method.

5:36
That's why it has the static keyword here.

5:40
This allows the Main method to be called without first creating a game object.

5:46
The OnMap method and the DistanceTo methods are known as instance methods,

5:51
because they're called on objects.

5:54
We learned earlier that objects are also known as instances of a class.

5:59
Math.Sqrt and Game.Main are static methods.

6:05
They're called directly using their class name.

6:08
So whenever we see a method being called directly on the name of a class

6:12
like Console.WriteLine is, we know that the method is static.

6:18
Let's take a quick break.

6:19
When we come back, we'll go back to the DistanceTo formula and

6:22
finish coding it up.
You need to sign up for Treehouse in order to download course files.
Sign up