From the course: Object-Oriented Programming with C#
Scope - C# Tutorial
From the course: Object-Oriented Programming with C#
Scope
- [Instructor] So now let's take a look at how scope affects classes. For this example, we're going to create a base language class and an English class that extends it. We'll use this to store all of the text for our game. Let's go into our solution and into our source folder and create a new folder and we'll call this text. While we're in here let's create another folder called characters, and we'll move our character and player classes into that folder. This will help us keep our code organized as we continue to add more to our game. Let's right click on the text folder and create a new class called Language. We can delete all the code from the template and create a new name space for OOP Adventure. Now let's create a new abstract class for language. This abstract class is going to contain all the properties for all the text inside of our game. Whenever we need to reference a piece of text, we'll look at a class that extends the language class in order to get that string and display it to the console. Let's take a look at creating some of these now by creating our first property called Welcome. Previously, we used the get and set keywords to automatically create a getter and setter for our property. Sometimes we don't want other classes to be able to access or set values of these properties and that's when scoping comes into play. Normally you can use the protected keyword similar to public in order to make sure that no other classes are able to access that property. But if you use the private keyword, any classes that extend it aren't going to be able to access it the same as they would as if it was set the public. So what we need to do is use another keyword called Protected for our setter. Let's go ahead and set this to an empty string and then we can copy this so we can add the next two lines of strings to our class. After welcome, we'll do choose your name, and the final one will be default name. Now it's time for us to create our English class. Let's go to our solution, right click on the text folder, add a new class, and call it English. We'll delete all this code. We'll add our name space and create a public class English that extends language. Now the English class will inherit all of the properties from our language class. Let's go into our program and create a new instance of our language. We'll do it at the top just under where we include the name space and create a new variable called language, and we'll set it to a new instance of English. Next, let's select the Hello string and cut it. We'll come back to that later. We can now reference the Language.ChooseYourName property in order to write this to the console. Since we haven't set the value, if we ran the game, it would just be an empty string. Let's go ahead and change the value of that string before we output it to the console. We'll use a reference to language, we'll select choose your name, and set it equal to the text that we previously cut. If you save your file, you'll notice that the compiler isn't happy with this, and that's because choose your name is actually set to private, and you can't access it outside of the class itself. In order for this to work, we're going to need to change it, either in the base class Language or in the child class English. Let's select the Choose Your Name equals in string, cut this, and remove the reference to language. Now inside of our English class let's go ahead and create a constructor and paste in the Choose Your Name with the string. Now, if we run our game you'll see that the English class, when it's constructed, sets the value of Choose Your name to the string Hello what's your name, and we're referencing it now in our program. This is a very important concept in object oriented programming. One, scope plays an important role as to how you protect the data inside of classes. Also, we are now referencing English instead of a default language class. What that means is that if we wanted to create another language like Spanish, we can create a new language class Spanish, extend language, and just set the choose your name to a string in Spanish. Then in our program, we can simply instantiate Spanish, get all the same properties from language, but change the language without having to change any of the other code since we're simply referencing the property of our base language class. Next, we'll take a look at how to access this language throughout our entire program.