Top Banner

of 13

Lecture 12 - Shared Class Resources (VB 2008)

May 30, 2018

Download

Documents

curlicue
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    1/13

    Lecture 12: Shared Class Resources

  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    2/13

    Outline In the last lecture, we continued our discussion of Overriding

    With Polymorphism, a simple but powerful concept Form 1 : Overriding Base Class Methods and Properties. Form 2 : The Ability of Methods to Accept Related Classes.

    And then illustrated with a generalized Multiple-JTrain Example That uses both forms of Polymorphism.

    We also learned the basics of creating our own custom Collections:

    Similar to Collections.ArrayList, but defined by us

    By inheriting from the .NET System Class, CollectionBase. And defined our own Collection called JTrainCollection.

    In this lecture, we first continue by updating Module1: Add two utility Methods:

    AddTrain()

    FindTrain() Update ourMain() Method to use these with JTrainCollection

    Then, we discuss SharedMembers and Methods Which allow us to share resources amongst all Object Instances.

    Example (JFreightTrain) : A Shared WagonWeight Member

  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    3/13

    Last time, we had just added the

    CommandTrain() Method

  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    4/13

    Next, add the AddTrain() Method

  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    5/13

    The FindTrain() Method

  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    6/13

  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    7/13

  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    8/13

    Shared Resources

    It is often useful to define a Shared Class resource:

    A Member, Property, or Method which is associated with a Class. But not to any particular instance. Such shared resources can be useful for:

    Book-keeping SPACE efficiency.

    To make a resource shared, we use the Shared keyword.

    Some JTrain Examples: A Shared WagonWeight Memberfor Class JFreightTrain

    That sets the weight ofall wagons for all instances (once only). This lets us change the LoadWeight at once for all Trains!

    Note: We are really setting their ideal full-load (not actual) weights

    A Shared Counterfor Class JTrain: That keeps track of how many instances exist, total.

    We simply add JTrain.Counter Then increment on creation, using the Base Constructors

  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    9/13

    Ex.: Shared WagonWeight Member

  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    10/13

    Example (cont.)

  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    11/13

    Shared Members (Assignment 1)

    (*) As an exercise, do the same for aShared JTrain Counter

    1. Add a Shared Membercalled Counter. Note: (*) You must also update the Bases New() methods, as well!

    To increment Counter each time a new JTrain or JFreightTrain is created.

    2. Update the Main() Method to let the user check it!

    I will check this, when you turn in your Final Project.

  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    12/13

    Shared Methods

    Shared Methods work in the same way...

    Such Methods are shared by ALL Class instances.

    However, Shared Methods have a natural limitation : They may only access Members of the class that are also Shared.

    Otherwise, it is unclear which instance of the Class is being acted on.

  • 8/14/2019 Lecture 12 - Shared Class Resources (VB 2008)

    13/13

    Overview / Forward

    In this lecture, we have continued our discussion of Objects

    By finishing our our own custom Collection: JTrainCollection Similar to Collections.ArrayList, but defined by us

    By inheriting from the .NET System Class, CollectionBase. As illustrated its use, with a generalized Multiple-JTrain Example.

    We then learned about Shared Members and Methods Which allow us to share resources amongst all Object Instances.

    Ex. 1 (JFreightTrain) : A Shared WagonWeight Member Ex. 2 (JTrain): A Shared Counter Member

    Next Lecture, we will continue with a discussion of Objects: The NET Framework

    The .NET Framework Classes

    Namespaces

    The Object Browser, etc

    Finally, I will assign yourFINAL PROJECT for the course.