Wednesday, April 27, 2011

Chapter 12 Summary

For Chapter 12, we're now delving into the area of writing our own classes. We've been using the Visual Basic Studio pre-written classes for everything up to this point.

Classes are the structures/foundations of a program where data types/objects are declared, stored, and processed. Each classes has its own set of controls, properties, functions, procedures, variables, etc.

The purpose of writing our own class is to access different private variables outside of class by only accessing certain public properties that we set the parameters to. We did an example of this in class, for the purpose of using the example of giving money to another person. You don't give the random person access to your home, your safe, your safe combo, and an unsupervised gateway to your stashed money. Instead, you go retrieve the money and deliver the money yourself to the person. Much in the same case, we can add a new class, use the Get...Set method, and retrieve different sets of data between classes while still keeping the rest of the date private.

To do this, click Project, then Add Class. Like a form, this only creates a blueprint of the class. To create an instance of the class, declare it within the code. Any variable you've entered in the new class, if set to private, won't be accessable outside the class. Setting the variables to public isn't safe, so in this case we must use a Public Property procedure.

Format:
Public Property PropertyName() As String
   Get
      Statements
   End Get
   Set (ByVal value As String)
       Statements
   End Set
End Property

Example:

Public Property Name() As String
   Get
      return strname
   Set (ByVal value As String)
      If value<>nothing then
         strname=value

When this is accessed by another class, it'll first set the value of the variable strName to whatever is stored in the variable in the class. When this is called in the other class, it'll then use the get procedure to retrieve only the stored value, which in this case is the name. I'll add more tomorrow.

No comments:

Post a Comment