Getting and Displaying the Quotation


At this point, you ve read individual quotations into the quotesArray object (and added error handling in case of problems), and you ve picked a random number. Given these two conditions, it s easy to get an element from the quotesArrayList object, as shown here:

Dim selectedQuote As Strin selectedQuote = quotesArrayList(randomNumber)

But we re not finished quite yet. When you built the quotations text file, you included both the quotation and the author s name, separated with a pipe character, but you probably don t want to display the quotation that way. You might want to convert the pipe character to another character, such as a dash ( ) or even a line break (<br> tag) and a dash. To convert the pipe character to another character, you can use the String object s Replace method. In the following code, the "|" character is replaced with the string "<BR>&#151;", which is the HTML code for a line break and a dash:

Dim selectedQuote As Strin selectedQuote = quotesArrayList(randomNumber Dim convertedQuote As String convertedQuote = selectedQuote.Replace("|", "<BR>&# 151;") labelQuote.Text = convertedQuote

Notice that we create a new variable, convertedQuote, to hold the string after we ve replaced the characters in it, and we display the value of that variable. When you call the Replace method, it doesn t perform the replacement in the string variable itself, so you need a new variable to hold the results of the replacement.

Alternatively, you can split the string into two parts, one containing the quotation and one containing the author s name. You can then display the parts separately, which also allows you to format them separately. You can split strings using the Split method, which breaks up the string based on a delimiter character just what we want. What makes this technique slightly complex is that the Split method returns an array of strings, so we have to create an array as a holding pen for the split string. Here s the code, with the new lines in boldface:

Dim selectedQuote As Strin selectedQuote = quotesArrayList(randomNumber   Split quotation and author Dim quoteArray(1) As String quoteArray = selectedQuote.Split("|") Try    labelQuote.Text = quoteArray(0)    labelAuthor.Text = quoteArray(1) Catch End Try 

This code creates an array named quoteArray that contains two elements (0 and 1), because we know that the string has only two parts. The Split method puts the first part of the string into element 0 and the second part into element 1. We then take these elements back out of the array and assign them to string variables before displaying them in labels.

I put another Try-Catch block around the lines that extract the elements from the temporary array. When we split the line as shown in the example, we re assuming that the line contains a pipe character. But if a line in the quotation file doesn t contain a pipe character, the Split method doesn t produce the two-element array that we re expecting, and trying to get the quotation or author generates an error. By putting the lines in a Try- Catch block, we don t fix the problem in the file, but at least the page doesn t display an error. I didn t put any statement into the Catch block because if we can t display a quotation, we have no other particular action to take for example, displaying an error message doesn t particularly help the user. Users will just have to get along without a quotation until the next postback.

The complete Page_Load event handler of the  RandomQuotes.aspx application can be found on the companion CD. In the example on the CD, the code for converting the pipe character to a line break plus a dash is commented out. If you want to use that code, remove the comment markers (') at the beginning of those lines and instead comment out the lines that split the quotation.




Microsoft ASP. NET Web Matrix Starter Kit
Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)
ISBN: 0735618569
EAN: 2147483647
Year: 2003
Pages: 169
Authors: Mike Pope

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net