Objects, Properties, and Callbacks in VBA, Lecture notes of Compiler Design

A set of lecture notes from a course on Scripting Languages at Cornell University. The notes cover topics such as classes and properties, visual programming and callbacks, and dialog design in VBA. The document also includes examples of subtyping and event handlers in VBA. The notes touch on missing object-oriented features in VBA and how they have changed in VB.NET. useful for students studying programming languages, object-oriented programming, and VBA specifically.

Typology: Lecture notes

2011/2012

Uploaded on 05/11/2023

shanthi_48
shanthi_48 🇺🇸

4.8

(36)

891 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 5142 Cornell University
08/06/12
1
CSCI-GA.3033.003
Scripting Languages
08/06/2012
Objects in VBA
Properties, Call-backs
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Objects, Properties, and Callbacks in VBA and more Lecture notes Compiler Design in PDF only on Docsity!

CS 5142 Cornell University 1

CSCI-GA.3033.

Scripting Languages

Objects in VBA

Properties, Call-backs

CS 5142 Cornell University 2

Outline

• Classes and properties

• Visual programming and callbacks

CS 5142 Cornell University 4

Documentation of OO in VBA

  • MSDN library →Development tools and languages →Visual Studio 6. →Visual Basic 6. →Product documentation →Using Visual Basic →Programmer’s guide →Part 2: What can you do with Visual Basic →Programming with objects Reference

CS 5142 Cornell University 5

Defining Properties

  • Syntax
    • Property Get id ([ arg *]) … End Property
    • Property Let id ([ arg *,] arg ) … End Property
    • Property Set id ([ arg *,] arg ) … End Property
  • Remarks
    • To return value from Get, assign to id
    • Let assigns regular value, Set assigns object
    • Extra arguments, if any, are for indexing
    • Each routine can be Private or Public
    • Public variable is just shortcut for routines

VBA

CS 5142 Cornell University 7

Subtyping Example

VBA

Dim someFruit As Fruit If … Then Set someFruit = New Apple Else Set someFruit = New Banana End If ' compiler knows that the method exists, ' even if it doesn’t know which version ' will get called someFruit.prepare("slice")

CS 5142 Cornell University 8

Inheritance in VBA

VBA

Public weight As Double Public Function pluck() As String 'empty routine End Function Public Sub prepare(how As String) 'empty routine End Sub Implements Fruit Public color As String Private wght As Double Private Property Let Fruit_weight(ByVal RHS As Double) wght = RHS End Property Private Property Get Fruit_weight() As Double Fruit_weight = wght End Property Private Function Fruit_pluck() As String Fruit_pluck = color & " apple" End Function Private Sub Fruit_prepare(how As String) Debug.Print how & "d " & Fruit_pluck() End Sub Class module “Fruit” (Interface) Class module “Apple” (subclass) Would be abstract in other languagesImplementsin subclass identifies superclass Override private with mangled name

CS 5142 Cornell University 10

Missing OO Features in VBA

• Implementation inheritance

• Constructors

  • Can write Class_Initialize() method
  • Or write subroutine in separate module to

allocate new instance and initialize it

• Static fields and static methods

⇒ OO features changed in VB.NET

Soap-box

CS 5142 Cornell University 11

Outline

• Classes and properties

• Visual programming and callbacks

CS 5142 Cornell University 13

Hungarian Notation

  • Convention: start

identifier with

indication of type

Concepts chk Check box cmd Command button frm User form fra Frame lst List box cmb Combo box mnu Menu opt Option button lbl Label txt Text box

CS 5142 Cornell University 14

Dialog Example

VBA

CS 5142 Cornell University 16

Writing Event Handlers

Sub paintLemonStar(nLines As Integer) Dim n As Integer, i As Integer n = nLines - 1 For i = 0 To n Dim l As Shape Set l = ActiveWindow.Selection.SlideRange.Shapes.AddLine( _ BeginX:=300, BeginY:=200 + i * 100 / n, _ EndX :=400, EndY :=300 - i * 100 / n) l.Line.ForeColor.RGB = RGB(i * 255 / n, i * 255 / n, 0) Next i End Sub Private Sub cmdPaint_Click() paintLemonStar CInt(txtNumberOfLines.Text) Hide End Sub Private Sub cmdCancel_Click() End End Sub

VBA

Retrieve user input Callbacks with mangled names Auxiliary subroutine

CS 5142 Cornell University 17

Code Sheets

  • Right-click→View code
    • Just like other modules
    • Contains event handlers as well as regular subroutines
  • Double-click on control
    • Goes to handler, creating it if necessary
    • Caution: handler name not changed automatically with control name!
  • Dynamic dialog: assign properties at runtime
    • E.g., Visible=True reveals hidden controls
    • UserForm_Initialize sets default, e.g. for list box

VBA

CS 5142 Cornell University 19

Common Controls and Events

  • Controls: Label, TextBox, ComboBox, ListBox, CheckBox, OptionButton, ToggleButton, Frame, CommandButton, TabStrip, MultiPage, ScrollBar, SpinButton, Image
  • Events: Change, Click, DblClick, Enter, Exit, Initialize, KeyPress, SpinDown, and many more
  • In editor for user form code sheet:
    • Left drop-down list: control objects on this form
    • Right drop-down list: events on that object Reference

CS 5142 Cornell University 20

Callback Mechanisms

VBA form Subroutine in form with mangled name VBA class WithEvent / RaiseEvent statements Java Pass object on which to call method Perl, Python, JavaScript Pass anonymous function (lambda) C, C++ Pass function pointer C++ Pass object on which to call “()” operator SmallTalk Pass code block PHP Pass name of function as string Concepts