LinkedIn principal author Doug Winnie describes how to access methods of classes using dot notation to access functionality within objects. In addition, Doug explains how comments can be used to add documentation into Visual C# code, and how comments can be used to hide lines of code from the compiler to prevent their execution at runtime.
- [Voiceover] We have a number off pieces assembled, but we need to put them together to do something. We need to have our button trigger the random number generator to make a number from one to six. And then we need to display that in the text block. All this needs to happen in the event handler roll button, underscore, click. First, we need to remove the code that is already there, but instead of deleting it, I want to temporarily remove it by using comment. Comments are special pieces of text that are inside of code that the compiler ignores.
They're used to put in tips or notes to the developer as well as hide lines of code that you want to ignore or keep from running. A comment starts with two forward slashes. Everything you type after that comment is ignored. So in this case, the line of code that changes the text field is ignored. Now, we need to create the random number. The random number generator instance has a method called next that you need to provide two values. The first, is the lowest, or the minimum value it can be.
The second is the highest or maximum. In programming, maximum can sometimes mean the highest value it can't be. So, in our instance, the minimum value on a die is one. The highest value on a die is six. So the maximum value in this instance is seven, because it can't go up to seven. It can be between one and up to seven, but not including seven. Just like with XAML object attributes, we'll use dot notation to access the methods within a class instance, so you'll access the object by name.
Random, generator. An access the next method by adding a dot and enter next. Methods are accessed using parenthesis instead of using equal sign and they have to be written as a pair. Inside the parenthesis, we'll add the minimum value, a comma and then the maximum value. Notice the tip at the bottom. It's showing us that the first number is an integer and the second digit is an integer, and it gives us a description, "returns a random integer that is within a specified range." And we end with a semi-colon.
But we need to do something with this value. We need to store it some place. In this case, we need to create a new integer variable and set it equal to the number that is spit out by the random number generator. There are two places we can put the variable. We can put it in the definition of the class, along side the die variable on line 25. We can also define it within the method and use it just within there. When you define a variable within method, it only exits within that method.
When the code in the method ends, any variables that are defined there vanish and disappear. In the case of the method variable, I don't need to make it public or private, but instead, just state that we are creating an integer variable using the int statement, and then we'll give it a name, like DieValue. Then, we can set it equal to the number created by the generator. Finally, we need to display what the number is. We can add a new line of code that accesses the text property of the DieValue text textblock that is defined on the XAML page.
The text that we want to put in the text block, though, won't always we the same. We want to display a simple message and then the unique value of the die when it was rolled. So, we need to take that text that is static or doesn't change and mix in a value that does change, using a variable. We can do this by building a string and mix in variables. First, let's access DieValue text object, and then we'll access a text property.
We can mix in the string and the variable by starting off with a dollar sign, then begin the text that we are going to write. When you get to the section where the variable will drop in, use a pair of curly braces and put the variable name within it. Then, finish the line of code. Let's test out and run the project. Click the play button with Local Machine to build and run the app. If you click the button, remembering that it might be behind the debugging text, you'll see the text block update, and it'll show the value off the random number that was created.
If you keep clicking it, you'll see that it will always be one through six.
Finally, you'll experiment with your app to learn more about how Windows apps work, and then find out where to go next.
- Installing Visual Studio Community edition
- Working with C#, XAML, and the Windows SDK
- Getting a head start with starter templates
- Testing apps with device emulators
- Creating your first app
- Building interactions, game logic, and scoring
- Adding custom components
- Modifying design parameters in XAML
- Experimenting and updating the final app