Classes and Class Libraries Examples and Hints

1 / 26

# Classes and Class Libraries Examples and Hints - PowerPoint PPT Presentation

Classes and Class Libraries Examples and Hints. November 9, 2010. Complex Number Class. A CLASSic example of a Class. Demonstrates a good use of ToString . Demonstrates operator overloading. Operator overloading. Allows you to define common operators for your custom classes.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Classes and Class Libraries Examples and Hints' - tieve

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Classes and Class LibrariesExamples and Hints

November 9, 2010

Complex Number Class
• A CLASSic example of a Class.
• Demonstrates a good use of ToString.
• Allows you to define common operators for your custom classes.
• What does it mean for two objects of your class to be equal?
• How do you add two objects? Multiply?
• How do you convert an object of your class to another type?
Class Complex: The Basics
• I got this code from the Stevens book.
• Note that Stevens cheats—he uses Public variables instead of properties for Re and Im.
• Public variables work just like our standard Public properties, but they do not allow for
• If you anticipate ever adding on to your class, you should use Public properties with
• with Private variables, like you did in assignment 3.
ToString
• Complex numbers are generally written as

a + bi

where a is the real part and b the imaginary part.

• The Complex class incorporates this format into its ToString function:
• You can add operator functions to your classes, such as Equals, Plus, Minus, Times, etc.
• However, VB allows you to define what the standard operator symbols (=, +, -, *, etc.) mean for your class.
Equals
• Being able to test equality is frequently important.
• In your custom classes, you can define what “=“ means.
• Note that if you define “=“, you must also define “<>”.
• Note also that “>” and “<“ are not defined, since they are not well defined for complex numbers.
• All operator overloads must be declared Public Shared.
• “Shared” means that the operator belongs to the class as a whole, not to individual objects.
• The equals that is being overloaded here is the comparison equals, the one that comes after “If”, not the assignment equals.
• Remember that for objects, the assignment equals works on references (addresses), not the objects themselves.
Conversions
• The code below effectively defines CDbl for the Complex class as being the absolute value:
• Personally, I would rather see absolute value implemented as a property, not a conversion.
• The property is ReadOnly since it is not something that you could assign directly; it depends on the real and imaginary parts of the number.
• For example, the complex numbers 1, i, -1, and –i all have the same absolute value.
Building on the Complex class
• Can you think of other properties, functions, operators, constructors, conversions, etc. that could be added to the Complex class?
• Some classes that might be similar to Complex?
• Vector
• Matrix
• Field (electric, gravitational)
Wrapper Classes
• VB’s .NET framework contains lots of wrapper classes.
• A wrapper class encapsulates some difficult or tedious code which performs something useful into a simple interface.
• For example, it would take a lot of code to write your own web browser into your program.
• Fortunately, Microsoft has provided the WebBrowser control (in the ToolBox).
• With the WebBrowser control (a class, of course), you get pretty much all of Internet Explorer’s functionality with just a few lines of code.
WebBrowser Control
• The VB Toolbox offers a WebBrowser control.
• This is basically a customizable version of Internet Explorer that you can build into your programs.
• Possible uses:
• Display pictures from the web (as an alternative to the PictureBox control)
• Display help files
WebBrowser Demo
• Open the “HTML Lecture” VB project.
• Click on WebBrowser Demo.
• This form will appear:
The WebBrowser Form
• frmBrowser contains a SplitContainer control.
• In the top half is a WebBrowser control.
• In the bottom is a TextBox.
• When the WebBrowser navigates to a new web page, the TextBox displays the HTML code for that page.
• The WebBrowser control’s “AllowNavigation” property determines if the user can use the browser as a true web browser or not.
• If set to False, they will not be able to connect to linked pages by clicking on hyperlinks.
• Set AllowNavigation to False if you want to restrict the user to web pages related to your program.
Getting the HTML source code
• If you want to retrieve the source code (usually HTML or XML) from a web page, use the WebBrowser’sDocumentText property.
• The above code demonstrates this.
• The code is placed in the WebBrowser’s Navigated event; that is, it waits until the new page is completely loaded.
• Once you have retrieved the DocumentText, you can search for keywords or values, such as a stock price or sports score, using the various String functions.
DbConn: Another Wrapper Class
• Most VB textbooks show you how to connect to a database and retrieve data, typically using code like this:
DbConn
• This code is tedious, difficult to understand or remember.
• As written, it works only with Access 2007 databases (Microsoft.ACE.OLEDB.12.0).
• The function encapsulates the code to some degree, but we can get greater functionality and flexibility by incorporating it into a class.
• I did this about three years ago, both for my work at UMTRI and for this course.
DbConn: Public Interface
• Public Sub New(ByValFileName As String)
• Public Sub New(ByValServerName As String, ByValDatabaseName As String)
• These two constructors identify the database to be connected to, and test the connection. If the database cannot be located or it can’t be open, the constructor raises an exception.
• The first constructor (one parameter) is for Access databases;
• The second (overloaded) constructor (two parameters) is for SQL Server databases.
OpenDatabaseConnection, CloseDatabaseConnection
• Public Sub OpenDatabaseConnection()
• Public Sub CloseDatabaseConnection()
• There are time and availability costs to opening a database and leaving it open.
• In general, if you need to run a lot of queries in a row, you should open the connection once, run all the queries, and then close the connection.
• I didn’t show you these subs for assignment 2, but you may want to use them if you find your data-connected program running slowly.
• Both GetDataTable and ExecuteSQL leave the connection in the condition that they found it (which defaults to closed).
Running the Queries
• You know how to use the remaining parts of DbConn’s user interface:
• Public Function GetDataTable(ByValsql As String) As DataTable
• Public Sub ExecuteSQL(ByValsql As String)
Connection Encapsulated
• Together, the public interface of DbConn encapsulates all of the complex code we saw earlier.
• That interface is,again:
• Public Sub New(ByValFileName As String)
• Public Sub New(ByValServerName As String, ByValFileName As String)
• Public Sub OpenDatabaseConnection()
• Public Sub CloseDatabaseConnection()
• Public Function GetDataTable(ByValsql As String) As DataTable
• Public Sub ExecuteSQL(ByValsql As String)