15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

364<br />

Using a Stack<br />

To use the Peek command, you have to assign the value of the Peek command<br />

to a variable like this:<br />

Dim BlowMyStack as New Stack<br />

Dim X as Object<br />

BlowMyStack.Push (“My cat”)<br />

BlowMyStack.Push (108.75)<br />

BlowMyStack.Push (“Fat dog”)<br />

X = BlowMyStack.Peek<br />

The preceding code assigns the value “Fat dog” to the X variable, which is<br />

declared as an Object data type. (In Visual Basic.NET, an Object data type<br />

can hold any type of data including integers, strings, and decimal numbers,<br />

such as (47.748).<br />

The Peek command retrieves the data but leaves it on top of the stack.<br />

If you want to remove data, you use the Pop command, which retrieves and<br />

removes data, as shown in the following Visual Basic.NET example:<br />

Dim BlowMyStack as New Stack<br />

Dim X as Object<br />

BlowMyStack.Push (“My cat”)<br />

BlowMyStack.Push (108.75)<br />

BlowMyStack.Push (“Fat dog”)<br />

X = BlowMyStack.Pop<br />

Figure 4-3 shows the difference between the Peek and the Pop commands.<br />

Figure 4-3:<br />

The Peek<br />

command<br />

retrieves<br />

data, but<br />

the Pop<br />

command<br />

retrieves<br />

and removes<br />

data from<br />

the stack.<br />

X = “Fat dog”<br />

Fat dog<br />

108.75<br />

My cat<br />

The “Peek” command<br />

copies data off the<br />

stack but leaves the<br />

data still on the stack.<br />

X = “Fat dog”<br />

108.75<br />

My cat<br />

The “Pop” command<br />

removes data off<br />

the stack.

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

Saved successfully!

Ooh no, something went wrong!