Fields6:18 with Jeremy McLain
Fields define the attributes an object can have.
Additional information about how to use the Mono C# Compiler can be found by typing
mcs --help in the command prompt.
Remove a file:
List files in the current directory:
Compile Treehouse Defense:
mcs -out:TreehouseDefense.exe *.cs
Run Treehouse Defense:
Compile and run Treehouse Defense:
mcs -out:TreehouseDefense.exe *.cs && mono TreehouseDefense.exe
To fill out a class, 0:00 we need to think about what sort of attributes it should have. 0:01 It's better to think about the most minimal set of attributes the objects 0:05 should have. 0:09 Let's look at one of the simplest objects in our game, the map. 0:10 Let's take a look at a graphic representation of a map. 0:14 We can divide the map into a grid. 0:17 The width of our map is the number of grid squares it is wide. 0:19 The height is the number of grid squares it is high. 0:23 So at its simplest, a map can be described by the attributes for width and height. 0:26 Let's open up the Map.cs file. 0:34 Width and height become two fields in our class. 0:36 These fields are declared right inside the class here. 0:39 We declare them similar to the way we declare variables inside of methods. 0:44 First, we give them a type, let's use int. 0:49 Then we give them a name, in this case, width. 0:52 We'll create another field for height. 0:56 Now every map object that's instantiated from the map class can 1:02 store both width and height. 1:05 I'll show you how to assign these fields' values, but 1:08 before we do that, we need to make these fields accessible to other classes. 1:10 You see, every field in a class is assigned an accessibility level. 1:15 The accessibility level determines if the value stored in these fields can be 1:20 accessed or modified by classes other than the map class. 1:24 We'll learn about two of these levels right now and 1:28 we'll learn about others in later courses. 1:31 The two we'll learn about right now are public and private. 1:34 Private fields are only accessible to methods 1:38 in the same class that they're declared in. 1:41 Public fields, on the other hand, can be accessed by any method in any class. 1:44 We can specify the accessibility level using an access modifier keyword. 1:49 To demonstrate the difference between public and private, 1:55 I'll make width public, and I'll make height private. 1:57 Actually, if we don't put anything here at all, then the field is private by default. 2:03 So you might say that the private keyword is unnecessary and 2:08 technically it is, but it's best to type it anyway. 2:12 It tells others that you've thought about the accessibility level of the field and 2:16 you've decided it should be private. 2:20 Now let's look at how to access these fields. 2:23 For that, we'll go back to the game class. 2:26 First, we need to create a map object. 2:29 So we'll say, Map map = new 2:32 Map, now we can access the fields using dot notation. 2:37 For example, to access the width field, we can type map.width. 2:42 We can use this just like any other variable. 2:48 We can do some math with it or print it out or even assign a value to it. 2:51 Let's assign it, eight. 2:55 Let's set the height field to five. 2:59 Now that we've assigned each field a value, let's try to use them. 3:06 Let's say int area = map.width 3:09 times map.height. 3:16 Now let's compile our program thus far to see what happens. 3:20 First let's make sure that we've saved all of our files. 3:24 We'll also need to make sure that the console is open. 3:27 Workspaces uses the mono C# compiler, so we'll type mcs. 3:33 Here, we list all of the files that are part of our program. 3:40 We could list all five of our code files. 3:43 But an easier way is to use an asterisk as a wild card and type *.cs. 3:45 That tells the compiler to compile all the files in the current directory 3:51 that end with .cs. 3:55 Let's run this and see what happens. 3:57 We get two compiler errors saying that height is inaccessible 4:01 due to its protection level. 4:05 We're getting these errors because the game class is 4:07 trying to access the height field. 4:10 But the height field isn't public. 4:12 To make this code work, we need to change the field's accessibility level 4:14 from private to public. 4:19 Let's compile again. 4:22 It compiled successfully with no errors. 4:27 We have a warning about the area variable not being used, but that's okay for now. 4:30 Let's list the contents of the directory to see the executable file that 4:35 was created. 4:38 Looks like the program the compiler generated is called Game.exe. 4:40 The compiler named it that because Game.cs is 4:45 alphabetically the first file in the directory. 4:48 I propose we give it a better name, though. 4:51 Let's delete the Game.exe file, and 4:53 then tell the compiler to name the file something else. 4:56 When deleting files, be very careful that you're deleting what you want. 5:00 There's no undo. 5:03 In Console, we can delete files by typing rm, and then the name of the file. 5:05 We can tell the compiler to name the program something different using 5:13 an additional option on the command line. 5:17 Right after typing mcs, we can type, 5:20 -out:, and then the name of the file we want created. 5:23 Let's call it TreehouseDefense.exe. 5:28 And of course, 5:32 we still need to tell the compiler we're compiling all of the cs files. 5:33 Let's compile and then list the files again. 5:38 Here's our program, TreehouseDefense.exe. 5:44 Remember, to run this program, just type mono TreehouseDefense.exe. 5:49 The program ran all of the code in the main method. 5:56 It doesn't appear to do anything, 5:59 though, because we haven't told it to print anything to the screen. 6:00 During this course, 6:04 we'll compile the code often to make sure that we don't have any compiler errors. 6:05 It's a lot easier to find and 6:10 fix compiler errors as we go along, than it is to wait until the very end. 6:11 So, let's compile as often as we can. 6:15
You need to sign up for Treehouse in order to download course files.Sign up