30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

208 Procedures Chapter 6<br />

playDie executes. We use the Image property (line 41) <strong>to</strong> display an image on a label. We<br />

set the property’s value with an assignment statement (lines 41–43). Notice that we specify<br />

the image <strong>to</strong> display through procedure FromFile in class Image (contained in the<br />

System.Drawing namespace). Method Direc<strong>to</strong>ry.GetCurrentDirec<strong>to</strong>ry<br />

(contained in the System.IO namespace) returns the location of the folder in which the current<br />

project is located, including bin, the direc<strong>to</strong>ry containing the compiled project files. The<br />

die images must be placed in this folder for the solutions in Fig. 6.15 and Fig. 6.16 <strong>to</strong> operate<br />

properly. The graphics used in this example and several other examples in this chapter were<br />

created with Adobe ® Pho<strong>to</strong>shop Elements and are located in the project direc<strong>to</strong>ry available<br />

on the CD-ROM that accompanies this book and at www.deitel.com.<br />

Notice that we must include an Imports directive (line 4) <strong>to</strong> use classes in<br />

System.IO, but not <strong>to</strong> use classes in System.Drawing. By default, Windows applications<br />

import several namespaces, including Microsoft.<strong>Visual</strong><strong>Basic</strong>, System,<br />

System.Drawing, System.Windows.Forms and System.Collections. The<br />

se namespaces are imported for the entire project, eliminating the need for Imports directives<br />

in individual project files. Other namespaces can be imported in<strong>to</strong> a project via the<br />

Property Pages dialog (opened by selecting Project > Properties from the menu bar)<br />

in the Imports listing under Common Properties. Some of the namespaces imported<br />

by default are not used in this example. For instance, we do not yet use namespace<br />

System.Collections, which allows programmers <strong>to</strong> create collections of objects (see<br />

Chapter 24, Data Structures and Collections).<br />

The Windows application in Fig. 6.16 rolls 12 dice <strong>to</strong> show that the numbers generated<br />

by class Random occur with approximately equal frequencies. The program displays the<br />

cumulative frequencies of each face in a TextBox.<br />

1 ' Fig. 6.14: RandomInteger.vb<br />

2 ' Generating random integers.<br />

3<br />

4 Imports System.Windows.Forms<br />

5<br />

6 Module modRandomInteger<br />

7<br />

8 Sub Main()<br />

9 Dim randomObject As Random = New Random()<br />

10 Dim randomNumber As Integer<br />

11 Dim output As String = ""<br />

12 Dim i As Integer<br />

13<br />

14 For i = 1 To 20<br />

15 randomNumber = randomObject.Next(1, 7)<br />

16 output &= randomNumber & " "<br />

17<br />

18 If i Mod 5 = 0 Then ' is i a multiple of 5?<br />

19 output &= vbCrLf<br />

20 End If<br />

21<br />

22 Next<br />

Fig. Fig. 6.14 6.14 Random integers created by calling method Next of class Random<br />

(part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!