Answers to Invalid, Valid Variable Name Exercise

 

Based on the rules for naming variables in Visual Basic indicate whether the following variable names are VALID or INVALID. Check the appropriate column for each variable name. Are the GOOD variables names?

 

Variable Name

Valid?

Invalid?

Good Variables Name? If not why?

GPA

YES

 

Yes. Assuming the application has something to do with grades this abbreviation is an acceptable variable name.

Count#1

 

YES

Invalid character   #

$grosspay

 

YES

Invalid character   $

GradePointAverage

YES

 

Yes - Descriptive

Attempted.Hours

 

YES

Invalid character . The period has special meaning in Visual Basic and may not be used in programmer supplied names.

Attempted-Hours

 

YES

Invalid character   -

Interest  Rate

 

YES

You may not use a space in a variable name

A2

YES

 

Not good name  Not descriptive

2A

 

YES

Must begin with a letter

End

 

YES

End is a reserved word. Reserved words have a special meaning to the compiler and may not be used for programmer supplied names

Printitout

YES

 

Not the greatest name. It is not clear what it means.

MONEYAFTERALLTAXES

YES

 

Not the greatest. All uppercase is valid but not helpful in reading the variable name. Also, while it’s length does not violate the rules it is probably too long to have to work with in code.

 

Rules for naming variables in Visual Basic

·         Unique within scope

·         <= 255 characters (other languages may have different length restrictions)

·         Begin with a letter

·         No embedded spaces or many special characters (. , “ - $ # * and others). The underscore _ is a valid character.

·         Cannot be a reserved word

·         Use upper and lower case with purpose. Once a variable is declared you do not have to be concerned with upper/lower case. The editor recognizes words that are the same except for case and makes them all the same for you. (use of upper and lower case differ between languages)

·         It is always good programming practice to use names that are descriptive or mnemonic.