19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

66 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

of the string or n is 0, then the empty string is returned. If m + n is greater than<br />

the length of the string, then Mid(str, m, n) is the right part of the string beginning<br />

with the mth character. The same is true for Mid(str, m). For instance, the<br />

values of Mid(“abcdef ”, 3, 9) <strong>and</strong> Mid(“abcdef ”, 3) are both “cdef ”.<br />

10. With FormatCurrency(n, r), fractional values are preceded by a leading zero,<br />

<strong>and</strong> negative values are surrounded by parentheses instead of beginning with a<br />

minus sign. The function has additional optional parameters. FormatCurrency(exp,<br />

, vbFalse) suppresses leading zeros for fractional values. FormatCurrency(exp,<br />

, , vbFalse) uses minus signs for negative numbers. For instance, the<br />

value of FormatCurrency(–3/4, 3, vbFalse, vbFalse) is –$.750.<br />

11. With FormatNumber(n, r), fractional values are preceded by a leading zero. The<br />

function has additional optional parameters. FormatNumber(exp, , vbFalse)<br />

suppresses leading zeros for fractional values. For instance, the value of FormatNumber(3/4,<br />

3) is 0.750 <strong>and</strong> the value of FormatNumber(3/4, 3, vbFalse) is<br />

.750. FormatNumber(exp, , , , vbFalse) suppresses commas.<br />

12. When n is a number that is halfway between two whole numbers (such as 1.5,<br />

2.5, 3.5, <strong>and</strong> 4.5), then n is rounded by Round(n) to the nearest even number.<br />

That is, half the time n is rounded up <strong>and</strong> half the time is rounded down. For<br />

instance, Round(2.5) is 2 <strong>and</strong> Round(3.5) is 4. Similar results hold for any number<br />

whose decimal part ends in 5. For instance, Round(3.65, 1) is 3.6 <strong>and</strong><br />

Round(3.75, 1) is 3.8. On the other h<strong>and</strong>, FormatNumber, FormatCurrency, <strong>and</strong><br />

FormatPercent always round 5’s up. For instance, FormatNumber(2.5) is “3.00”.<br />

13. The value of FormatDateTime(Now, vbLongDate) is today’s date. For any<br />

positive number n, FormatDateTime(Now + n, vbLongDate) is the date n<br />

days from today <strong>and</strong> FormatDateTime(Now - n, vbLongDate) is the date<br />

n days ago.<br />

14. The functions FormatNumber, FormatCurrency, FormatPercent, <strong>and</strong><br />

FormatDateTime were added to <strong>Visual</strong> <strong>Basic</strong> in VB6.0. The same results can be<br />

obtained with the Format function alone. However, these new functions execute<br />

faster than Format <strong>and</strong> are easier to use. In addition, they can be placed in<br />

VBScript programs that are used to make Web pages interactive.<br />

15. Each time the function Rnd appears in a program, it will be reassigned a value.<br />

For instance, the task attempted (but not accomplished) by the first set of lines<br />

that follows is achieved by the second set of lines. Because each of the Rnd’s in<br />

the first set of lines will assume a different value, it is highly unlikely that the<br />

square of the first one will equal the product of the last two.<br />

‘Generate the square of a r<strong>and</strong>omly chosen number<br />

R<strong>and</strong>omize<br />

picBox.Print “The square of”; Rnd; “is”; Rnd * Rnd’<br />

Generate the square of a r<strong>and</strong>omly chosen number<br />

R<strong>and</strong>omize<br />

numVar = Rnd<br />

picBox.Print “The square of”; numVar; “is”; numVar * numVar<br />

16. Additional information about the keywords, functions, methods, <strong>and</strong> properties<br />

discussed in this section appear in Appendix C. With the Learning, Professional,<br />

<strong>and</strong> Enterprise Editions of <strong>Visual</strong> <strong>Basic</strong> you can obtain a detailed (<strong>and</strong> somewhat<br />

advanced) discussion about an item appearing in code by clicking on the<br />

item <strong>and</strong> pressing F1. Other ways of obtaining help with these editions are presented<br />

in Appendix B.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!