Basic Method Creation
Methods are very useful in the C# language. Methods can help organize and simplify code, and are used all the time in game programming. This lesson should be able to introduce anyone who knows even nothing about programming, to the world of methods and how to create them, use them, and benefit from them in your programming.
A method can be looked at like a named container that holds code and does something to a value. This can be useful, for example, if you wanted to explode a ship. Instead of re-writing the code to explode the ship every time there is a collision, you can just create a method that explodes a ship, and then just call that method every time you want a ship to explode.
First, before we can call a method, we need to learn how to create methods. Here is the method creation structure:
accessModifier returnType methodName ( parameters )
First of all, methods need an access modifier. This modifier serves the same purpose as access modifiers on variables.
Next, there is the return type. The return type shows what type of variable you want the method to return. For example, let’s say that we have created a method called GiveBackEight. This method would look like the following:
private int GiveBackEight()
{
return 8;
}
When we call this method, we want to set any integer to a value of eight. In order to be able to set an integer to the value that the method returns we need to have an int as the parameter type. This way, we can set the value of a variable to the value this method returns by using the command int myVariable = GiveBackEight(); This sets the value of myVariable to the value of what GiveBackEight returns.
You can also create a variable that has a return type of, void which means the method does not return anything. These types of methods could be used for example to just draw something. You could declare a method that drew the number eight on the screen like this:
private void DrawEight()
{
Console.WriteLine("8");
}
This just draws the number eight onto the screen. These types of methods don’t give back anything. These types of methods are to do commands that will finish themselves. They are never used to change the value of a variable and give it back because with a void return type, the methods cannot return anything. You can call this method by using a command like DrawEight(); This just calls the method to do what it does.
After the return type, you have the method name, which is just like a variable name, and is used to access the method.
Finally, you have the parameters, which are placed in parentheses. A parameter is like a value that is given from an outside source so that the method can complete what it needs to do accurately. We have talked about these types of variables in the previous chapter. For example, if we wanted to allow the capability to draw any number, not just eight, we could change the DrawEight method to:
private void DrawNumber(int numberToDraw)
{
Console.WriteLine(numberToDraw.ToString());
}
This code takes any number that you choose as a parameter, and then draws that number. This is very helpful because, instead of having to make a billion different methods to draw every number, you can just create one method that has the capability to draw any number.
Method Overloads
Sometimes you may want to create the same method, but with maybe a few different parameters. An example of this is, maybe you have an explosion method that explodes at a given intensity value. You may also want to make it where the default intensity value is 100. You could do something like this:
private void ExlodeShip(int intensity)
{
//Explosion code
}
private void ExplodeShip()
{
ExplodeShip(100);
}
This code first creates the ExplodeShip method. This method takes an int parameter for the intensity of the explosion. Below that method though, we have another method with the same name. This method is an overload of the original ExplodeShip method but takes no parameters. Since 100 is the most common explosion intensity, for most cases the user will just pass in 100 for the intensity value. Instead of making him do that, we create an overload of ExplodeShip that takes no parameters but calls the one-parameter ExplodeShip version and passes in 100 because that is what the user wants. For more complicated methods, this can save a lot of time.
There are a few things you need to watch out for when using method overloads. You need to be careful not to have a continual loop. This could maybe happen when you on accident call the same method that you are in. For example calling ExplodeShip(); in the zero-parameter method would result in a continual loop because you are inside the method that you are calling.
Another thing is that you can’t have the method overload have the same parameter types. Even if one overload has int intensity and another method has int timeDelay, you can’t do this because the C# compiler (program that executes the code) will not be able to tell the difference. For it, both overloads have the same parameter types and it won’t know which method to call.
Properties
Properties are another type of code-block that you can use in C#. Some people may think properties are really confusing, but they aren’t at all, and are almost exactly the same as methods. You could look at a property like a method that doesn’t have parameters. To create a property, you would do something like this:
accessModifier returnType propertyName
You can see that this format is almost the same as the method format. We have an access modifier, a return type, and a property name. These all serve the same purpose as for methods.
There is one exception though. Besides the fact that properties can’t take parameters, they also can’t have the void return type. This is because properties are usually used a little differently than methods. Methods are more used to do things to values and then give the value back. Properties aren’t so much used that way. Properties are more used to give a value or set a value of something. This would explain why you can’t have the void return type because properties can’t just do something; they have to return a value.
To explain this, you could create this method which would return the value of ten:
private int Ten()
{
return 10;
}
You could also create the same thing using a property:
private int Ten
{
get
{
return 10;
}
}
The property would be the correct way to go. All this property does, is, when you try to access the value of what Ten has, the get part of the property is called. The only difference is that instead of calling myInt = Ten(); which is the method version, you call myInt = Ten; which doesn’t use the parentheses because there can be no parameters in a property.
Properties are mostly used for when you want to access a value in a class. For example the MathHelper class has a property called Pi which gives the value of PI.
Set Properties
Set properties are also types of properties that you can use. The way I will explain this is in a game example. Pretend you had a player class. Inside that player class you have a health property that allows access to the health. You would want someone to be able to access the health variable and change it to his liking. You could do this by using this code:
private int health;
public int Health
{
get
{
return health;
}
set
{
health = value;
}
}
First, we declare a private variable called health. This variable allows us to access health in our class when we want to change it or set it to a value.
After that we have our property called Health (remember variable names are case-sensitive). First we call the get part which returns the value of what our private health variable is. Then we call the set part. This part is called when we set the value of Health using something like Health = aValue;. In this part of the property we set health, our private variable, to value which is the value that we put on the right side of the equal sign when we set Health.
Since names in C# are case-sensitive, we can make a variable and a property have the same name. Of course, you may think that we can just make the private health variable into a
public one, we won’t have to worry about the property stuff. We could do this, but it would be incorrect programming practice. Also, using properties allows us to change the value of what we return which you can’t do if you just have a public variable. This could look like the following:
private int health;
public int TenMoreThanHealth
{
get
{
return health + 10;
}
set
{
health = value;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment