Professional Excel Development [Electronic resources] : The Definitive Guide to Developing Applications Using Microsoft® Excel and VBA® نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Professional Excel Development [Electronic resources] : The Definitive Guide to Developing Applications Using Microsoft® Excel and VBA® - نسخه متنی

Stephen Bullen, Rob Bovey, John Green

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید











What Is an Interface?


An interface is a list of public properties, methods, events, user-defined-types, constants and/or enumerations that we can use to interact with an object. When we dimension a variable to be a certain object type, we're actually specifying the interface that the variable will use to talk to an object. When we later make the variable refer to an object, we're specifying which object we want to talk to, through that interface. When the code is run, the compiler checks to see whether the object has the interface we specified, and throws a Type Mismatch error if it doesn't, as shown in Listing 11-1.

Listing 11-1. A Type Mismatch Error



'Declare a variable that will talk to objects through the
'Worksheet interface
Dim wksInput As Worksheet
'Sheet1 in our workbook has the Worksheet interface,
'so we can talk to it
Set wksInput = Sheet1
'The ThisWorkbook object doesn't have the Worksheet interface,
'so we get a Type Mismatch error.
Set wksInput = ThisWorkbook

Whenever we create a class module, the VBA compiler also creates a default interface for that class. The default interface is given the same name as the class and contains a list of all the public properties, methods etc. that we add to the class. When we dimension a variable using Dim clsTheClass As CClassName, we're saying that the variable will use the interface CClassName. When we use code like Set clsTheClass = New CClassName, we're creating an object that is a new instance of the class CClassName, then setting the variable to refer to the object, as in Listing 11-2.

Listing 11-2. Variables, Interfaces and Classes



'Declare a variable to use the CClassName interface
Dim clsTheClass As CClassName
'Create a new instance of the CClassName class
'and set our variable to refer to it
Set clsTheClass = New CClassName

The code in the class defines how the object behaves, whereas the interface defines how we access the code.
By hiding this implementation detail from us, VBA makes it much easier for us to work with class moduleswe don't need to care whether we're dealing with a class or an interface. Unfortunately, it also hides the useful fact that we can define our own custom interfaces and mix and match classes and interfaces if we want to! The rest of this chapter examines a few ways that we can improve our applications by doing just that.


/ 225