Objects Object Linking and Embedding OLE You may
Objects
Object Linking and Embedding (OLE) ¨ You may link to objects defined in many other programs. The other program is started up. ¨ You may embed them into your program as well. The functionality is placed within your program.
OLE Examples ¨ Excel Spreadsheets ¨ Word. Pad Documents ¨ Power. Point Presentations ¨ Sounds ¨ Many other
Objects Every thing we deal with is an object. Forms, collections, controls, and so on.
Object Classes ¨ Classes are like molds for objects ¨ They can have properties, methods, and events ¨ A particular occurrence of a class is an instance of that class. ¨ cmd. My. Button is an instance of the Command Button Class
Analyzing Objects ¨ Type. Of / Is If Type. Of pic. My. Picture is Command. Button then int. X=msg. Box(“This is a Button, not a picture”) End If ¨ Type. Of(pic. My. Control) ¨ Returns the type of control
Referring to multiple properties ¨ With cmd. Button. caption = “Hello”. top = 5000. left = 1000. height = 500. width = 1500 End With
System Objects ¨ Printer ¨ Err ¨ Screen ¨ Debug ¨ Clipboard ¨ App Returns printers Returns errors in code Returns screen properties Useful when writing program Accesses and writes cut/paste Info about your program
Example – The Clipboard Object Clipboard. Clear Clipboard. Set. Text txt. Name. Sel. Text ¨ These lines take the value of the selected text in the txt. Name box and apply it to the system Clipboard object ¨ Many other properties and methods exist with the several system objects.
Arrays of Objects ¨ An array of objects is like a bunch of cars coming off the assembly line. ¨ Objects all have the same inherent properties, methods, and events derived from their class. ¨ But these may have different settings. For instance car(27). color = vb. Blue
Arrays of Controls ¨ The most common place you will see this is in control arrays. ¨ Set a button’s properties to all of its default values, then copy and paste the button. ¨ You will be asked if you wish to start an array. You will end up with something like this: cmd. Buy(1), cmd. Buy(2), cmd. Buy(3) … ¨ This is very useful because the same procedure can handle several related buttons!
Reasons for arrays ¨ You can instantly change properties of all ¨ You can have select case or other statements that handle all of them ¨ You can add new ones
Collections “The Collection object provides a convenient way to refer to a related group of items as a single object. The items, or members, in a collection need only be related by the fact that they exist in the collection. Members of a collection don't have to share the same data type. ”
The Collection Class ¨ Collection is a class ¨ We are used to seeing things such as the printers collection. ¨ Collections start at 1, not 0 ¨ You can create instances of the collection class. Private col. My. Students as Collection ¨ You can. add, . Count, and. Remove items from your collection. ¨ You can refer to a member of the collection by its. item number.
Collection Example Dim col. Cars as New Collection col. Cars. Add “ 57 Chevy” col. Cars. Add “Model T” col. Cars. Add “ 86 Hot Wheel” …. col. Cars. Remove 3 col. Cars. Add “Matchbox #20” , Before: =3
Collections ¨ col. My. Cars. Count returns the number of cars in collection col. My. Cars(1) returns “ 57 Chevy”
Collections ¨ The neatest thing about collections isn’t covered in the book. ¨ Instead of just referring to an item by its index, you can refer to it by a key value. Cars. Add "57 Chevy", "Bob" In this case, I can refer to Cars(“Bob”) to find out the info about his car.
Example of Key Values Dim Cars As New Collection Cars. Add "57 Chevy", "Bob" Cars. Add "62 T-Bird", "Fred" Cars. Add "78 Hot Wheel", "Junior" Cars. Add "72 Matchbox", "Billy" Msg. Box (Cars("Fred"))
Object Properties, Methods, and Events ¨ When you type in an object and a period, a list of properties, methods, and events comes up. ¨ You can also use the object browser to analyze an object. ¨ Use F 2 or View—Object Browser
Object Browser
About Object Browser ¨ Lists all classes of objects ¨ Lists all Events, Properties, Methods, and Constants of that object. Event Constant Property Method
Object Browser
Objects - Creating ¨ You can create your own objects. See chapter 24.
Object Summary ¨ You can link or embed functionality from other programs. ¨ You can view the properties, methods, constants, and ¨ ¨ events associated with an object with the object browser. You can determine the type of object something is. You can use system objects such as printers, debug, and app. You can put objects in arrays for ease of functionality. You can form collections of objects with Items and Keys.
- Slides: 24