In this video, Doug Winnie explains how to create a private member in a class using the private statement. This creates permissions and rules for the various members of your custom classes.
- [Instructor] When we create members in our class, we can define them as either being public or private. Private members are only available within the class. Let's open our Cat class to look at an example. In our Cat class, we have a public string for the name. We can also create an integer for the age of the cat, but age is something that we can't force a change on. I can't tell a cat, hey, grow up a couple of years. By having this as public, though, that is exactly what's going on.
I am providing public access to the property. In this case, I need to make it private, because I don't want to let anyone outside of the class to have the ability to change the value. But now, I have another problem. I have a private member, but now, that also means that no one can get the age of the cat. The solution is to create a new method that will be public, and return the cat's age. So, at the bottom, let's create a new method that will be a public method that will return an integer.
We'll call it GetAge. Inside, we can return the age property, which we can do, since it is private in this context. Now, we can test these new permissions we have put on a cat. Go back to the Progame.cs file. At the bottom, let's try and access the age property. Pet.age It's going to try and autocorrect this to GetAge. Press Escape, and then push the semicolon.
When you enter this in, you'll get an error, because, in this context, the age property doesn't exist. It is private, so we don't have access to it. We do have access to the GetAge method, though. So, let's create a new message in the console. Inside, we can create a console message and build a string. We'll then access pet.petName to get the name of the pet, and then we'll insert the word is, and then insert pet.GetAge as a method, so make sure you have a pair of parenthesis.
And then we'll add years old. Let's go ahead and run and see if this works. Go to Debug, and then select Start Without Debugging. Because we are using the public method, we are able to get the private properties value, but we've been able to secure the permissions to access the age, to avoid public access to the value. You can make any member of a class private, including methods. If you want to have a method you can only access within a class, you should type it as private.
Programming expert Doug Winnie starts by sharing the history of C# to give you context into its purpose and beneficial uses. Then he walks through a sample of code showing how to run a program using the Visual Studio IDE. After warming up with a sample, he dives into working with values, variables, methods, and custom functions. Next, he shows how to capture values input by site users, managing different variable types, building compound conditional tests, and using loops with arrays. In the final five videos, he covers the basics of object-oriented programming including classes, objects, and permissions.
- C# history, uses, and terminology
- Setting up your PC or Mac
- Working with values and variables
- Using methods to repeat actions
- Customizing functions with parameters
- Managing scope, rules, variables, values, and functions
- Capturing input from a user
- Creating conditional tests
- Using loops
- Creating arrays to store groups of values
- Collections and lists
- Making your own classes and objects
- Defining permissions for class members
- Extending classes