- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
IMPLEMENTING OBJECT COLLABORATION in Font
CHAPTER 8 IMPLEMENTING OBJECT COLLABORATION Drawing Data Matrix 2d Barcode In None Using Barcode drawer for Font Control to generate, create ECC200 image in Font applications. www.OnBarcode.comGS1 - 12 Generation In None Using Barcode drawer for Font Control to generate, create GS1 - 12 image in Font applications. www.OnBarcode.comPublic Class User ... Public Sub Logon(ByVal UserName As String, _ ByVal UserPassword As String) 'Code to check logon credentials 'if successful . . . UserLog.IncrementUserCount() End Sub . . . End Class Although you may not use shared properties and methods often when creating the classes in your applications, they are useful when creating base class libraries and are used throughout the .NET Framework system classes. The following code demonstrates the use of the Compare method of the System.String class. This is a shared method that compares two strings alphabetically. It returns a positive value if the first string is greater, a negative value if the second string is greater, or zero if the strings are equal: Public Function CheckStringOrderAscending(ByVal String1 As String, _ ByVal String2 As String) As Boolean If String.Compare(String1, String2, True) >= 0 Then Return True End If End Function PDF-417 2d Barcode Encoder In None Using Barcode creator for Font Control to generate, create PDF417 image in Font applications. www.OnBarcode.comCode-39 Generation In None Using Barcode creator for Font Control to generate, create USS Code 39 image in Font applications. www.OnBarcode.comActivity 8-3. Implementing Exception Handling and Shared Methods in VB
Barcode Generator In None Using Barcode generator for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.comPainting Code 128B In None Using Barcode creator for Font Control to generate, create Code128 image in Font applications. www.OnBarcode.comIn this activity, you will become familiar with the following: Creating and calling shared methods of a class Using structured exception handling in VB QR Code Generation In None Using Barcode generation for Font Control to generate, create Denso QR Bar Code image in Font applications. www.OnBarcode.comPainting Postnet 3 Of 5 In None Using Barcode generation for Font Control to generate, create USPS POSTNET Barcode image in Font applications. www.OnBarcode.comCreating Shared Methods
Data Matrix Scanner In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comData Matrix Reader In Visual Studio .NET Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comTo create the shared methods, follow these steps: 1. Start VS. Select File New Project. 2. Choose a Windows Application project. Name the project Act8_3. 3. A default form is included in the project. Add controls to the form and change the property values, as listed in Table 8-3. Your completed form should look similar to Figure 8-4. Matrix 2D Barcode Printer In .NET Framework Using Barcode generator for ASP.NET Control to generate, create Matrix 2D Barcode image in ASP.NET applications. www.OnBarcode.comCode 128 Code Set A Encoder In C#.NET Using Barcode drawer for .NET Control to generate, create Code 128A image in VS .NET applications. www.OnBarcode.comCHAPTER 8 IMPLEMENTING OBJECT COLLABORATION
Code 128C Generation In VB.NET Using Barcode encoder for .NET framework Control to generate, create Code-128 image in VS .NET applications. www.OnBarcode.comCode 39 Generation In Java Using Barcode printer for Java Control to generate, create USS Code 39 image in Java applications. www.OnBarcode.comFigure 8-4. The completed logger form Table 8-3. Logger Form and Control Properties
Generate QR In VS .NET Using Barcode encoder for Reporting Service Control to generate, create QR Code image in Reporting Service applications. www.OnBarcode.comCreate PDF417 In Java Using Barcode drawer for Android Control to generate, create PDF-417 2d barcode image in Android applications. www.OnBarcode.comObject
UPC Symbol Printer In C#.NET Using Barcode creation for Visual Studio .NET Control to generate, create UPC-A Supplement 2 image in VS .NET applications. www.OnBarcode.comQR-Code Encoder In Java Using Barcode generator for BIRT reports Control to generate, create Denso QR Bar Code image in Eclipse BIRT applications. www.OnBarcode.comForm1 Textbox1 Textbox2 Button1
EAN 128 Recognizer In C# Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comEncode Barcode In None Using Barcode drawer for Office Word Control to generate, create Barcode image in Word applications. www.OnBarcode.comProperty
Name Text Name Text Name Text Name
Text
Value
frmLogger Logger txtLogPath c:\LogTest.txt txtLogInfo Test Message btnLogInfo
Log Info
4. Select Project Add Class. Name the class Logger. 5. Because you will be using the System.IO class within the Logger class, add an Imports statement to the file: Imports System.IO Public Class Logger . . . End Class 6. Add a shared LogWrite function to the class. This function will write information to a log file. To open the file, you will create a FileStream object. You will create a StreamWriter object to write the information to the file. Public Shared Function LogWrite(ByVal LogFilePath As String, _ ByVal LogInfo As String) As String Dim oFileStream As FileStream = _ New FileStream(LogFilePath, FileMode.Open, FileAccess.Write) Dim oStreamWriter As StreamWriter = _ New StreamWriter(oFileStream) oFileStream.Seek(0, SeekOrigin.End) oStreamWriter.WriteLine(Now) oStreamWriter.WriteLine(LogInfo) oStreamWriter.WriteLine() CHAPTER 8 IMPLEMENTING OBJECT COLLABORATION
oStreamWriter.Close() Return "Info logged" End Function 7. Open frmLogger in the code editor. In the right object drop-down list, choose btnLogInfo. In the left drop-down list, choose the click event. Add the following code, which runs the LogWrite method of the Logger class and displays the results in the form s text property. Note that because you designated the LogWrite method as shared (in step 6), the client does not need to create an object instance of the Logger class. Shared methods are accessed directly through a class reference. Private Sub btnLogInfo_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnLogInfo.Click Me.Text = Logger.LogWrite(txtLogPath.Text, txtLogInfo.Text) End Sub 8. In the Solution Explorer, select the project node. Right-click the project node and select Properties. In the Property Pages dialog box, change the startup object to frmLogger. 9. Select Build Build Solution. Make sure there are no build errors in the Error List window. If there are, fix them, and then rebuild. 10. Select Debug Run. When the form launches, click the Log Info button. You should get an unhandled exception message of type System.IO.FileNotFoundException. Stop the debugger. Creating the Structured Exception Handler
To create the structured exception handler, follow these steps: 1. Open the Logger class code in the code editor. 2. Locate the LogWrite method and add a Try-Catch block around the current code. In the Catch block, return a string stating the logging failed: Try Dim oFileStream As FileStream = New FileStream _ (strLogFilePath, FileMode.Open, FileAccess.Write) . . . rest of code Catch Return "Logging failed!" End Try 3. Select Build Build Solution. Make sure there are no build errors in the Error List window. If there are, fix them, and then rebuild. 4. Select Debug Run. When the form launches, click the Log Info button. This time, you should not get the exception message because it was handled by the LogWrite method. You should see the message Login Failed! in the form s caption. Close the form.
|
|