212 Methods Chapter (Web server address) 6 (lines 85 93) sets the

212 Methods Chapter 6 (lines 85 93) sets the player s point, displays the dice in pointGroupBox, enables roll- Button and disables playButton. Notice that for many of the cases, we cast the enumeration values to type int. Although each enumeration value is assigned an integer value on lines 26 32, each value is considered to be of enum type DiceNames, and therefore must be cast to int for use in the switch structure, which requires constant integral expressions. The rollButton_Click event handler s task is to roll the dice and determine if the player won by making the point value or lost by rolling 7. Line 40 calls method roll- Dice. Lines 42 54 in method rollButton_Click analyze the roll. Depending on the value of the roll, the buttons rollButton and playButton will become either disabled or enabled. This is done by setting the Enabled property to trueor false. 6.12 Duration of Variables The attributes of variables include name, type, size and value. Each variable in a program has additional attributes, including duration and scope. A variable s duration (also called its lifetime) is the period during which the variable exists in memory. Some variables exist briefly, some are created and destroyed repeatedly and others exist for the entire execution of a program. A variable s scope is where the variable s identifier (i.e., name) can be referenced in a program. Some variables can be referenced throughout a program, while others can be referenced from limited portions of a program. This section discusses the duration of variables. Section 6.13 discusses the scope of identifiers. Local variables in a method (i.e., parameters and variables declared in the method body) have automatic duration. Automatic duration variables are created when program control reaches their declaration; that is, they exist while the block in which they are declared is active, and they are destroyed when that block is exited. For the remainder of the text, we refer to variables of automatic duration as automatic variables, or local variables. The instance variables of a class are initialized by the compiler if the programmer does not provide initial values. Variables of most primitive data types are initialized to zero, bool variables are initialized to false and references are initialized to null. Unlike instance variables of a class, automatic variables must be initialized by the programmer before they can be used. Common Programming Error 6.13 Automatic variables must be initialized before their values are used in a method; otherwise, the compiler issues an error message. Variables of static duration exist from the time at which the class that defines them is loaded into memory. These variables then last until the program terminates. Their storage is allocated and initialized when their classes are loaded into memory. Static-duration variable names exist when their classes are loaded into memory, but this does not mean that these identifiers necessarily can be used throughout the program their scopes may be limited as we will see in the next section. 6.13 Scope Rules The scope (sometimes called declaration space) of an identifier for a variable, reference or method is the portion of the program in which the identifier can be accessed. A local vari
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Leave a Reply