1 / 26

Classes and Class Libraries Examples and Hints

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.

tieve
Download Presentation

Classes and Class Libraries Examples and Hints

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes and Class LibrariesExamples and Hints November 9, 2010

  2. Complex Number Class • A CLASSic example of a Class. • Demonstrates a good use of ToString. • Demonstrates operator overloading.

  3. Operator overloading • 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?

  4. Show the Program

  5. 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 • adding validation code. • If you anticipate ever adding on to your class, you should use Public properties with • with Private variables, like you did in assignment 3.

  6. 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:

  7. Operator Overloads • 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. • This makes your code easier to read AND write.

  8. 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.

  9. Do the Math

  10. Conversions • VB allows you to define conversions from your class by overloading CType. • The code below effectively defines CDbl for the Complex class as being the absolute value:

  11. Absolute Value as ReadOnly Property • 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.

  12. 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)

  13. 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.

  14. 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 • Allow users of your program to link to specific web pages, while preventing access to other pages (or general browsing)

  15. WebBrowser Demo • Open the “HTML Lecture” VB project. • Click on WebBrowser Demo. • This form will appear:

  16. 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.

  17. Open New Browser Code

  18. Go To Google Code

  19. Navigation Code • 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.

  20. 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.

  21. DbConn: Another Wrapper Class • Most VB textbooks show you how to connect to a database and retrieve data, typically using code like this:

  22. 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.

  23. 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.

  24. 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).

  25. 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)

  26. 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)

More Related