I will be talking about classes most of the time because as you will learn later on they are a little more flexible.
A class is basically a blueprint on how to create objects memory of that type.
In object oriented programming, you look at something and decide how to represent it in code. For example a halo map has different sections, the header, the index, ect... So you create a class for a map accordingly, having it contain a header and index.
There are 3 main things that will make up your classes:
- variables
- constructors
- methods
The declaration of a class looks like this:
[code]
public class ClassName
{
// variables, constructors, and methods here
}
[/code]
Field Variables:
In your classes you will need them to have variables in order to store information about the class.
To create a variable for the class you will need to declare it inside of the class.
I am going to create a class to represent a coordinate on a Cartesian plane as my example.
A point on a Cartesian plane has an x and a y, and they are both numbers, so we will create the class as follows:
[code]
public class CartPoint
{
public int x;
public int y;
}
[/code]
Right now all we have is a class with 2 variables and no constructors or methods.
Before I explain how to create a constructor I should probably explain what it is used for.
Constructors are special methods used to instantiate(set for the first time) the variables of the class.
The constructor is called when you use the new operator, for example:
[code]CartPoint point = new CartPoint();[/code]
Constructors:
The above code will call the constructor of CartPoint and return a CartPoint to store in the variable point.
Notice how CartPoint doesn't have a constructor? Well the .Net framework automatically gives you one if you don't declare one. It sets all the variables to their default value, so point.x and point.y would both be 0 in the above example.
Now to learn how to create a constructor:
[code]
public class CartPoint
{
public int x;
public int y;
public CartPoint(int x, int y)
{
this.x = x;
this.y = y;
}
}
[/code]
Notice that it just has a return type, and make sure you declare it public or else you won't be able to use it.
So the constructor in this example sets the variables x and y with parameters that are sent into the constructor.
I'll give an example of how to use this:
[code]
CartPoint point = new CartPoint(2, 10);
[/code]
In the above example, point.x is 2 and point.y is 10.
Now, there are a something wrong with the above example. The variables x and y are both public, meaning that they can be accessed by something besides a CartPoint object. What's wrong with that? you might be asking. Well, how do you know when one of them has changed, how do you control and make sure that the data is valid? For example if you needed the data to be between 0 and 300, how would you ensure that nothing sets the data to anything besides that?
The solution is to use accessors. Accessors are used to provide access to a variable, but still be able to have control and know when it's being accessed. Also it allows you to change how the data is stored without changing how it is accessed if you do decide to change the storage mechanism.
I'll create an example of an accessor for x. Notice how I will change x to be a private variable:
[code]
int x; // no access modifier(public or private) before defaults to private
public int X
{
get{ return x; }
set
{
// notify that x is changed if you want
// check to see that the value is allowable
if(value >= 0 && value <= 300)
{
this.x = value;
}
}
}
[/code]
You could also leave out the set part of the accessor to make it a read only variable, this means that you can not do anything to directly change the variable, you can only see what it's value is.
Also, just to set the record straight on private variables, private makes it so only objects of the same type can directly access the variable, in this example, that means one CartPoint object can directly access the private variables of another without having to use the accessor like objects of other types.
Methods:
Now to be productive you're going to need methods to act on the objects of CartPoint.
A good example for CartPoint would be a method that finds the distance to another CartPoint.
The formula for distance is the square root of the difference of a's squared + the difference of b's squared
In order to get the distance between 2 methods we'll need to create a method that takes a CartPoint as a parameter and returns a double(because chances are with the square root it will not be an integer)
[code]
public class CartPoint
{
// fields(s) left out
// constructor(s) left out
public double Distance(CartPoint that)
{
int aDifference = this.x - that.x;
int bDifference = this.y - that.y;
// to square a number multiply it by itself
int sumOfDifferencesSquared = (aDifference * aDifference) + (bDifference * bDifference);
// now get the square root
return Math.Sqrt(sumOfDifferencesSquared); // this returns a double
// square root the
}
}
[/code]