Novice iLogic Discovery - Classes>Objects & Properties/Methods/Events

Novice iLogic Discovery - Classes>Objects & Properties/Methods/Events

cadman777
Advisor Advisor
461 Views
0 Replies
Message 1 of 1

Novice iLogic Discovery - Classes>Objects & Properties/Methods/Events

cadman777
Advisor
Advisor

Since last time in here I've been trying to learn how to make macros in VBA. The reason is b/c nobody in here made any sense. it was like coming into a room where everybody speaks Martian and I only know English. So, I picked up VBA for Dummies by Mueller and began reading it. About 3/4 of the way through I came across a statement defining what an OBJECT is (i.e., Object Oriented Programming). It also defined the 4 key terms related to objects: Properties/Methods/Events. So I'm in here today to post the segment of text out of his book to show you guys HOW SIMPLE THIS IS, and how I define it in TWO SIMPLE SENTENCES. Maybe some noobies in here will benefit from this information:

 

Understanding Classes

A class is a description of an object: It’s the blueprint that VBA uses to build an object when you request one. The class isn’t the object; it’s merely the set of building instructions for the object. You can use the class to visualize what the object will look like, but you can’t use a class to perform any tasks. Once VBA builds the object using the class, it doesn’t look at the class any more unless it wants to build another object. This differentiation between classes and objects is important to remember because many information sources confuse the two concepts. You need to keep them separate to better understand how objects work. The sections that follow describe classes and objects in detail.

 

Understanding object-oriented programming concepts

At one time, developers had to worry about every variable, construct, and step in their code. Procedural languages use step-by-step instructions to tell the computer how to perform a task. Many developers continue to use procedural languages because they find them easy to use and understand. Object-oriented programming (OOP) hides implementation details from the developer. All the developer needs to know is that an object accomplishes a specific task; how the object performs the task is up to the object’s developer. The act of hiding the working details of an object is called encapsulation. The idea behind using objects is that you don’t worry about how the information you type gets turned into executable code. This concept may sound really odd to anyone who’s used to working with a procedural language, but that’s the way it is. I had similar difficulties when I moved from assembler and C to C++. It took some time for me to learn that although the object code does get translated into executable code in some way, the whole reason for using objects is to create an abstraction so that you worry less about the actual underlying code than you do about the task the object is supposed to perform. The object creator takes care of the internal workings of the object. Using classes also benefits the developer because it’s possible to inherit all of the features of another class. Inheritance is the act of creating a new class based on the content of a parent class. For example, you might already have a class called Dog and want to create a new class called BorderCollie. Because a border collie is a kind of dog, you can inherit the feature of the Dog class into the new BorderCollie class. Class theory can become quite complex, but you really don’t need to worry about the complexities when working with VBA. You can find all kinds of OOP topics online that discuss everything from the intricacies of good class definition to whether OOP is really better than procedural code. If you really want to know the low-level concepts behind OOP, one of the best places to begin is Wikipedia (http://en.wikipedia.org/wiki/Object-oriented_ programming).

 

Understanding properties, methods, and events

All classes include some comment elements. Because a class is essentially a black box that accepts input and provides output, you need some way to interact with it. Classes provide three common constructions that help you interact with them: properties, methods, and events. The following list describes each of these constructions. _ Properties: A property provides a means to access data within the object. Unlike a variable, a property can include code that controls the interaction with the object data. The property might check the data type of the incoming request or format the outgoing data in some way. Properties can provide read/write access so that you can change the object data as well as use it in your own code. However, a class developer can also choose to make a property read-only or write-only.

Methods: A method provides a means of asking the object to perform a task. As with a Function or Sub, a method can return a value and accept data as input. Unlike with a Function or Sub, you need not worry about the inner workings of the method. Your only concern is that you must provide certain input to obtain specific output. In some cases, you will find methods that accept no input and provide no output but still perform a task. For example, a Refresh() method may tell the object to refresh its data. Methods always perform a task, but need not work with data (from an external perspective) to do it.

Events: An event is the object’s way of interacting with the outside world. An event signals that something has happened. A user might have clicked a button, or the status of a text box might have changed. The events that an object signals depend on the communication that the class designer chooses to provide. Even when an object signals an event, however, nothing takes place in your code unless you create an event handler, a special Sub or Function, to do something with the event.

 

Here's how I summarize the above into TWO SENTENCES:

1. CLASSES are programs that create OBJECTS, which in turn receive input from the user and give output to the user. 

2. PROPERTIES are the way to ACCESS DATA in the Objects, while METHODS are the way to get the Object to DO SOMETHING for you, while EVENTS are the TIMING for getting the Object to act.

 

That info tells you that OBJECTS are the things you are creating w/your code, and the PROPERTIES, METHODS and EVENTS are the things you 'say' to the OOJECTS to get them to do something for you. Pretty simple.

 

Cheers

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
462 Views
0 Replies
Replies (0)