Lab: Using Objects and Saving Data

Lab: Using Objects and Saving Data

In this lab, you ll create the Translator application. containing a Web form and a Class module. You ll create an instance of an object, save that instance in Session state, and use Session state and ViewState from the Web form. When complete, the application will look like Figure 3-6.

figure 3-6 the completed translator application

Figure 3-6. The completed Translator application

Estimated lesson time: 20 minutes

Exercise 1: Create the User Interface

In this exercise, you ll create the user interface by adding server controls to the Web form.

To create a new Web application project

  1. Open a new ASP.NET Web application project. In the New Project dialog box, enter the name Translator and click OK.

  2. In the Properties window, change the name of the WebForm1.aspx file to Translator.aspx.

To add the controls to the user interface

Add server controls to the Web form, as shown in Figure 3-6, and define the following properties:

Control

Control type

Property

Value

Labe0l 1

Label

Text

Universal Translator

Font

Bold, Large

TextBox1

Text Box

ID

txtSource

TextMode

MultiLine

Button1

Button

ID

butTranslate

Text

Translate

Exercise 2: Create a Translator Class

In this exercise, you ll create a Friend class to contain the logic and data that the Translator application uses.

To create a class

  1. From the Project menu, choose Add Class. Visual Studio displays the Add New Item dialog box. Name the class TranslatorClass.vb and click Open.

  2. In the Code window, add the following code:

    Visual Basic .NET

    Friend Class TranslatorClass Private mstrText As String Private mstrOriginal As String ' Controls access to the module-level ' variables. Public Property Text() As String Get Text = mstrText End Get Set(ByVal Value As String) mstrText = Value ' Keep a copy of the original for Restore. mstrOriginal = Value End Set End Property ' Restores translated text back to the original. Public Sub Restore() mstrText = mstrOriginal End Sub ' Translates the value in the Text property. Public Sub Translate() Dim strWord As String, intCount As Integer Dim arrWords() As String Dim bCaps As Boolean ' Convert the string to an array using System.String . arrWords = mstrText.Split(" ") For intCount = 0 To UBound(arrWords) ' Check if word is capitalized. If LCase(arrWords(intCount)) <> arrWords(intCount) Then bCaps = True arrWords(intCount) = LCase(arrWords(intCount)) End If strWord = arrWords(intCount) ' Do translation. If strWord <> "" Then strWord = Right(strWord, Len(strWord) - 1) & _ Left(strWord, 1) & "ay" ' Recapitalize if necessary If bCaps Then strWord = UCase(Left(strWord, 1)) & _ Right(strWord, Len(strWord) - 1) End If End If ' Store back in the array. arrWords(intCount) = strWord ' Reset caps flag. bCaps = False Next ' Rebuild string from array. mstrText = String.Join(" ", arrWords) End Sub End Class

    Visual C#

    internal class TranslatorClass { string mstrText, mstrOriginal; // Controls access to class-level variables. public string Text { get { return mstrText; } set { mstrText = value; // Keep a copy of the original for Restore. mstrOriginal = value; } } // Restores translated text back to the original. public void Restore() { mstrText = mstrOriginal; } // Translates the value in the Text property. public void Translate() { string strWord; string[] arrWords; bool bCaps = false; // Convert the string into an array using System.String. arrWords = mstrText.Split(' '); for(int intCount = 0; intCount <= arrWords.GetUpperBound(0); intCount++) { // Change to lowercase. strWord = arrWords[intCount].ToLower(); // Check if word is capitalized. if(!arrWords[intCount].Equals(strWord)) bCaps = true; // Do translation. if(strWord != "") { strWord = strWord.Substring(1,strWord.Length - 1) + strWord.Substring(0,1) + "ay"; // Recapitalize if necessary. if(bCaps) strWord = strWord.Substring(0,1).ToUpper () + strWord.Substring(1, strWord.Length - 1); } // Store the word back in the array. arrWords[intCount] = strWord; // Reset the caps flag. bCaps = false; } // Rebuild the string from the array. mstrText = String.Join(" ", arrWords); } }

Exercise 3: Store a Translator Object in Session State

In this exercise, you ll add code to the Web form s Page_Load event procedure to initialize a Session state variable containing the TranslatorClass object that your Web form will use.

To create a Session state variable

In the Code window for the Web form, add the following Page_Load event procedure:

Visual Basic .NET

Dim TransClass As TranslatorClass Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' The first time this page is displayed If Not IsPostBack Then ' Create a new Translator object. TransClass = New TranslatorClass ' Store the object in a Session state variable. Session("TransClass") = TransClass Else ' Get the Session TransClass variable. TransClass = Session("TransClass") End If End Sub

Visual C#

// Declare an object. TranslatorClass TransClass; private void Page_Load(object sender, System.EventArgs e) { // The first time this page is displayed if (!IsPostBack) { // Create a new Translator object. TransClass = new TranslatorClass(); // Store the object in a Session state variable. Session["TransClass"] = TransClass; } else // Get the Session TransClass variable. TransClass = (TranslatorClass)Session["TransClass"]; }

Exercise 4: Use the TransClass Object from Web Form Events

In this exercise, you ll add code to the Web form to use the TransClass object stored in the Session state variable. You ll also use a Boolean variable stored in the page s ViewState property to switch the function of the command button from Translate to Restore.

To use the TransClass object from event procedures in the Web form

  1. Open the Web form s code module in the Code window.

  2. Add the following code to the butTranslate_Click event procedure:

    Visual Basic .NET

    Private Sub butTranslate_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles butTranslate.Click ' Declare a boolean switch. Dim bSwitch As Boolean ' Get the value from ViewState and switch it. bSwitch = Not Viewstate("bSwitch") ' Save the new value in ViewState. ViewState("bSwitch") = bSwitch ' Use the switch to either translate or restore ' the text in TextBox1. If bSwitch Then ' Get the text. TransClass.Text = txtSource.Text ' Translate it. TransClass.Translate() ' Display the text. txtSource.Text = TransClass.Text ' Change the Button text. butTranslate.Text = "Restore" Else ' Restore the original text. TransClass.Restore() ' Display the text. txtSource.Text = TransClass.Text ' Change the Button text. butTranslate.Text = "Translate" End If End Sub

    Visual C#

    private void butTranslate_Click(object sender, System.EventArgs e) { // Declare a boolean switch. bool bSwitch; // Check if ViewState variable exists. if(ViewState["bSwitch"] != null) // Get the value from ViewState and switch it. bSwitch = !(bool)ViewState["bSwitch"]; else // Set the switch. bSwitch = true; // Save the new value in ViewState. ViewState["bSwitch"] = bSwitch; // Use the switch to either translate or restore // the text in txtSource. if (bSwitch) { // Get the text. TransClass.Text = txtSource.Text; // Translate it. TransClass.Translate(); // Display the text. txtSource.Text = TransClass.Text; // Change the Button text. butTranslate.Text = "Restore"; } else { // Restore the original text. TransClass.Restore(); // Display the text. txtSource.Text = TransClass.Text; // Change the Button text. butTranslate.Text = "Translate"; } }

  3. Run and test the application.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Web Applications With Microsoft Visual Basic. Net and Microsoft V[.  .. ]0-315
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Web Applications With Microsoft Visual Basic. Net and Microsoft V[. .. ]0-315
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 118

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