Starting out with Visual Basic 7e chapter 3

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Write a Date literal for the following date and time: 5:35:00 PM on February 20, 2017

#2/20/2017 5:35:00 PM#

The primary differences between a Panel and a GroupBox

- A Panel cannot display a title, and does not have a Text property. - A Panel's border can be specified by its BordersStyle property. The available settings are None, FixedSingle, and Fixed3D. The property is set to None by default.

Exception handling

- A structured mechanism for handling errors in Visual Basic programs. - Exception handling begins with the Try keyword, followed by one or more catch blocks, followed by End Try.

Variable

- A variable is a storage location in computer memory that holds data while a program is running. It is called a variable because the data it holds can be changed by statements in the program. - Variables hold data that may be manipulated, used to manipulate other data, or remembered for later use.

Class-level variables

- Class-level variables are declared inside a class declaration (such as a form's class) but not inside of any procedure. - Class-level variables are accessible to all of the procedures in a class.

ControlChars.CrLf

- Stands for carriage return line feed. - Used to display multiple lines of information in a message box.

Naming rules for variables

- The first character must be a letter or an underscore character (do not recommend that you start a variable name with an underscore, but if you do, the name must also contain at least one letter or numeric digits). - After the first character, you may use letters, numeric digits, and underscore characters (You cannot use spaces, periods, or other punctuation characters i a variable name). - Variable names cannot be longer than 1,023 characters. - Variable names cannot be Visual Basic keywords. Keywords have reserved meanings in Visual Basic, and their use as variable names would confuse the compiler.

Naming convention for variables (camel case names)

- The variable name starts with lowercase letters. - The first character of the second and subsequent words is written in uppercase. Ex: intHoursWorked, strCustomerName, etc.

In the following table, fill in the expected values returned by the ToString function when specific numeric values are used with specific format strings. 12.3 (Format String: n4) 12.348 (Format String: n1) 1234567.1 (Format String: n3) 123456.0 (Format String: f1) 123456.0 (Format String: e3) .234 (Format String: p2) 1234567.8 (Format String: c3)

12.3000 12.3 1,234,567.100 123456.0 1.235E+005 23.40% ($1,234,567.800)

Will the following statement execute or cause a runtime error? dblResult = CDbl("186,478.39")

186478.39

After the statement dblResult = 10 \ 3 executes, what value will be stored in dblResult?

28

What value will be stored in dblResult after the following statement executes? dblResult = CInt("28.5")

28

How is a Panel control different from a GroupBox control?

A Panel cannot display a title, and does not have a Text property.

Panels

A Panel control is a rectangular container for other controls, like a GroupBox.

What is an exception handler?

A block of code that is executed automatically when an exception is thrown.

What is a breakpoint?

A breakpoint is a line of code that causes a running application to pause execution and enter break mode. While the application is paused, you may perform debugging operations, such as examining variable contents and the values stored in control properties.

Breakpoint

A line you select in your source code. When the application is running and it reaches a breakpoint, the application pauses and enters break mode.

Logic error

A logic error is a mistake that does not prevent an application from running, but causes the application to produce incorrect results. Mathematical mistakes, assigning a value to the wrong variable, or assigning the wrong value to a variable are examples of logic errors.

What is a named constant?

A named constant is like a variable whose content is read-only, and cannot be changed by a programming statement while the program is running.

Group box

A rectangular area with a thin border and an optional title in its upper-left coner. It is a container that can hold other controls. You can use group boxes to create a sense of visual organization on a form. In Visual Studio, you use the GroupBox control to create a group box with an optional title.

Function

A special type of procedure. When you call a function, you typically send data to the function as input. The function performs an operation using that data, and then it sends a value back as output.

Variable declaration

A statement that creates a variable in memory when a program executes. Here is the general form of a variable declaration: Dim VariableName As DataType

What is the difference between a syntax error and a logic error?

A syntax error will prevent an application from compiling and executing. Examples are misspelled keywords and incorrect use of operators or punctuation. Syntax errors are often reported as soon as you type them. A logic error is a programming mistake that does not prevent an application from starting, but causes the application to produce incorrect results. Examples are incorrect math statements and assigning the wrong value to a variable.

What is a variable?

A variable is a storage location in the computer's memory; used for holding data while the program is running.

Named constant

A variable whose content is read-only, and cannot be changed by a programming statement while the program is running. General form of a named constant declaration: Const ConstantName As Datatype = Value

What is an accept button? What is a cancel button? How do you establish these buttons on a form?

AcceptButton refers to the control that will receive a Click event when the user presses the Enter key. CancelButton refers to the control that will receive a Click event when the user presses the Esc key. You identify the two buttons in form properties, named AcceptButton and CancelButton.

Access key

Also known as mnemonic, is a key that is pressed in combination with the Alt key to access a control such as a button quickly. When you assign an access key to a button, the user can trigger a Click event either by clicking the button with the mouse or by using the access key.

What is an exception?

An exception is an unexpected error that occurs while a program is running, causing the program to abruptly halt.

Exception

An unexpected error that occurs while a program is running causing the program to abruptly halt. Exceptions are typically caused by circumstances outside of the programmer's control.

What is string concatenation?

Appending one string to another.

Implicit Numeric Conversions

Byte ~> Integer ~> Long ~> Decimal ~> Single ~> Double

Which function converts the string "860.2" to value of type Double?

CDbl

Bonus question: Find out which famous Microsoft programmer was launched into space in 2007. Was this programmer connected in any way to Visual Basic.

Charles Simonyi. He introduced the use of prefixes in variable names, called Hungarian Notation, a popular naming convention often used in Visual Basic and many other programming languages.

Where do you declare class-level variable?

Class-level variables are declared at the class-level (inside the class, but not in any procedure)

What kind of code does the try block of a Try-Catch statement contain?

Code related to the normal functioning of the application.

Exception object

Contains various data about an exception; created when an exception is thrown; can optionally be assigned a name in the Catch clause of a Try-Catch statement to access properties.

The general syntax of the Focus method

ControlName.Focus() For example: txtName.Focus()

ToString method

Converts the content of a variable to a string.

TabStop property

Determines if a control can receive focus.

TabIndex property

Determines the position of a control in the tab order.

Option Strict

Determines whether certain implicit data type conversions are legal. When Option Strict is On, only widening conversions are permitted. When Option Strict is Off, both narrowing and widening conversions are permitted.

Write a variable declaration for an Integer variable named intUnitsSold.

Dim intUnitsSold As Integer

How do you create a Load event handler for a form?

Double-click any area of the form in the Designer window, where there is no other control.

Truncation

Dropping the fractional part of a number is called truncation.

Tab order selection mode

Establishes a tab order when you click controls in the desired sequence.

If the following statement is executed, what will the lblGreeting control display? lblGreeting.Text = "Hello " & "Jonathan, " & "how are you?"

Hello Jonathan, how are you?

How do you assign an access key to a Button control?

Insert the & character in its Text.

How do you display an ampersand (&) character on a Button control?

Insert two consecutive && characters into its Text.

How is integer division different from floating-point division?

Integer division throws away any fractional part of the quotient. The result of integer division is always an integer.

What is the difference between a class-level variable and a local variable?

Local variables are not visible to statements outside the procedure in which they are declared. Class-level variables are visible to all of the procedures in a class.

Naming a constant

Looks like a regular variable declaration except for the following differences: - The word Const is used instead of Dim. - An initialization value is required. - By convention, all letters after the prefix are capital. - Words in the name are separated by the underscore character. For example: Const dblINTEREST_RATE As Double = 0.129

What Visual Basic function would you use to get the current date and time from the system?

Now

The general format of calling the method to execute an object's method

Object.Method

Runtime error

Occurs during a program's execution - it halts the program unexpectedly.

Narrowing conversion

Occurs when a larger type is assigned to a smaller type. An example is when a real number is assigned to an integer type variable.

Widening conversion

Occurs when data of a smaller type is assigned to a variable of a larger type. An example is when assigning any type of integer to a Double.

What controls have the focus?

Only controls capable of receiving some sort of input, such as text boxes and buttons, may have the focus.

AcceptButton

Refers to the control that will receive a Click event when the user presses the Enter key.

CancelButton

Refers to the control that will receive a Click event when the user presses the Esc key.

How do you display a form in tab order selection mode? How do you exit tab order selection mode?

Select Tab Order from the View menu.

What is the purpose of single-stepping through an application?

Single-stepping is a useful debugging technique for locating logic errors. In single-stepping, you execute an application's code one line at a time. After each line executes, you can examine variable and property contents. This process allows you to identify the line or lines of code causing the error.

What TextBox control property holds text entered by the user?

Text

TextBox control

The TextBox control is a rectangular area that can accept keyboard input from the user.

What happens if you press the Enter key while a Button control has the focus?

The button's Click event handler executes.

What is meant when it is said that a control has the focus?

The control is accepting keyboard and/ or mouse input from the user.

What happens when a control's TabStop property is set to False?

The control is skipped when the user presses the Tab key.

Single-stepping

The debugging technique of executing an application's programming statements one at a time. After each statement executes, you can examine variable and property contents.

How are the TabIndex properties of the controls inside the group box organized?

The form a subgroup of the GroupBox's own TabIndex value.

What event happens just before a form is displayed on the screen?

The form's Load event.

String concatenation

The operation of joining string values together

Scope

The term scope means the part of a program in which a variable may be accessed. Every variable has a scope, and a variable is visible only to statements in its scope.

When a GroupBox control is deleted, what happens to the controls that are inside?

They are deleted also.

What causes the program to jump to the Catch clause of a Try-Catch statement?

This happens when an exception is thrown.

Exception handler

This is a section of code that gracefully responds to exceptions and is written with a Try-Catch statement.

What Visual Basic function would you use to get the current time from the system, without the date?

TimeOfDay

How can you make the ToString method display parentheses around a number in Currency format when the number is negative?

ToString("c")

What Visual Basic function would you use to get the current time from the system, without the time?

Today

Write a try-catch statement for an application that calculates the sum of two whole numbers and displays the result. The application uses two TextBox controls named txtValue1 and txtValue2 to gather the input, a Label control named lblSum to display the result, and a Button control to activate the calculation.

Try Dim intSum As Interger = CInt(txtValue1.Text) + CInt (txtValue2.Text) lblSum.Text = intSum.ToString() Catch MessageBox.Show("Please enter integer values") End Try

Displaying the & character on a button

Use two ampersands (&&) in the Text property.

Returning a value

When a function sends a value back as output, we typically say that the function is returning a value.

Local variable

When a variable is declared inside of a procedure, such as an even handler, it is referred to as a local variable.

Load events

When an application's form loads into memory, an event known as the Load event (also known as the Form Load) takes place.

Argument

When we send a piece of data as input to a function, we typically say that we are passing the data as an argument.

How can you display the default error message when an exception is thrown?

You display the exception object's Message property in a message box or a label.

For each of the following numeric formats, identify the format string used as the input parameter when calling the ToString method. a. Currency b. Exponential scientific c. Number d. Percent e. Fixed-point

a. "c" b. "e" c. "n" d. "p" e. "f"

What default value is assigned to each of the following variables? a. Integer b. Double c. Boolean d. Byte e. Date

a. 0 b. 0.0 c. False d. 0 e. 12:00:00 AM, January 1 of year 1

What value will be stored in intResult after each statement executes? a. intResult = 10 MOD 3 b. intResult = 47 MOD 15

a. 1 b. 2

After each of the following statements executes, what value will be stored in dblResult? a. dblResult = 6 + 3 * 5 b. dblResult = 12 / 2 - 4 c. dblResult = 2 + 7 * 3 - 6 d. dblResult = (2 + 4) * 3

a. 21 b. 2 c. 17 d. 18

What value will be stored in dblResult after each of the following statements executes? a. dblResult = 6 + 3 * 5 b. dblResult = 12 / 2 - 4 c. dblResult = 2 + 7 * 3 - 6 d. dblResult = (2 + 4) * 3 e. dblResult = 10 \ 3 f. dblResult = 6 ^ 2

a. 21 b. 2 c. 17 d. 18 e. 3.0 f. 36

Assuming that intNumber is an integer variable, what value will each of the following statements assign to it? a. intNumber = 27 b. intNumber = CInt(12.8) c. intNumber = CInt(12.0) d. intNumber = (2 + 4) * 3

a. 27 b. 13 c. 12 d. 18

How would the following strings be converted by the CDec function? a. 48.50000 b. $34.95 c. 2,300 d. Twelve

a. 48 (rounds to nearest even integer) b. 35 c. 2300 d. cannot be converted

Indicate whether each of the following is a legal variable name. If it is not explain why. a. count b. rate*Pay c. deposit.amount d. down_payment

a. Legal b. Illegal; cannot use the * character c. Illegal; cannot use a period d. Legal

Which of the following variable names are written with the convention used in this book? a. decintrestrate b. InterestRateDecimal c. decInterestRate

c.decInterestRate

Show an example of formatting a Date variable in Long Date format when calling the ToString method.

datBirth.ToString("D")

Show an example of formatting a Date variable in Long Time format when calling the ToString method.

datStart.ToString("T")

What will be the final value of dblResult in the following sequence? Dim dblResult As Double = 3.5 dblResult += 1.2

dblResult will be set to 4.7

What will be the final value of dblResult in the following sequence? Dim dblResult As Double = 3.5 dblResult *= 2.0

dblResult will be set to 7.0

Write a statement that uses the ToString method to convert the contents of a variable named dblSalary to a Currency format.

dblSalary.ToString("c")

Assume an application has a label named lblMessage and a TextBox control named txtInput. Write the statement that takes text the user entered into the TextBox control and assigns it to the label's Text property.

lblMessage.Text = txtInput.Text

Write a programming statement that gives the focus to a TextBox control named txtNumber.

txtNumber.Focus()


Kaugnay na mga set ng pag-aaral

Mental health exam 3 quiz questions

View Set

NU370 Week 4 PrepU: Accountability

View Set

allusions - Elijah and the Chariot of Fire to the Golden Calf

View Set

Federal Privacy Protection and Consumer Identification Laws

View Set

CIS 345 Operating Systems Chapter 1 Vocabulary

View Set

Heath & Wellness Chapters 4, 5, & 6

View Set