04.08.2013 Views

Un-Mapping Mapped Network Drives Andrew Coates - dFPUG-Portal

Un-Mapping Mapped Network Drives Andrew Coates - dFPUG-Portal

Un-Mapping Mapped Network Drives Andrew Coates - dFPUG-Portal

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

there’s now a container in the way, so there isn’t a<br />

txtShoCol object on the form anymore. Instead it’s<br />

contained in the cntChgCol, which is on the form, so the<br />

reference now has to be THISFORM.cntChgCol.txtShoCol<br />

instead.<br />

Andy: Or even better, use THIS.Parent.txtShoCol instead.<br />

In fact, that’s what you probably should have done the<br />

first time, planning ahead in case the group of controls<br />

was going to be reused in a container class, or you were<br />

going to add them to a Page Frame to the form.<br />

Paul: That reminds me—in MS Knowledge Base article<br />

Q155013, there’s a text box in a column of a grid in a<br />

container that’s doing an incremental search. In the<br />

KeyPress event, there’s a line of code that seems a<br />

little excessive:<br />

THIS.Parent.Parent.Parent.Search( nKeyCode )<br />

I can work out what it’s doing—the Search() method<br />

is a method of the container, and this is passing the<br />

keystroke to it, but that’s not very clear, is it? It’s unlikely<br />

to be broken when it’s reused, because the article is about<br />

building a custom control.<br />

Andy: So what are you suggesting? In that particular case,<br />

I don’t see that there’s any alternative. The referencing has<br />

to be relative, because the text box can’t possibly know<br />

what the name of the outermost container will be at<br />

runtime. There’s no “THISCONTAINER” reference, and<br />

anything relative to “THISFORM” is obviously not<br />

applicable. It’s not pretty, but I don’t see what else you<br />

can do.<br />

Paul: Well, what I’d do is create a ThisContainer reference.<br />

Either as a property if I’m using a class, which is set to<br />

THIS.Parent.Parent.Parent in the INIT() method, or as a<br />

LOCAL variable that has to be set every time the<br />

KeyPress method is called (and therefore is of little<br />

benefit). There’s a marginal benefit in the latter<br />

approach, because the reference is also used in<br />

THIS.Parent.Parent.Parent.Pop() in the same method<br />

where the variable can be used as well.<br />

Andy: I see. So you’re proposing to replace this:<br />

IF nKeyCode = 13 && They hit the ENTER key<br />

This.Parent.Parent.Parent.Pop()<br />

ENDIF<br />

IF (nKeyCode > 48 AND nKeyCode < 58) OR ;<br />

(nKeyCode > 64 AND nKeyCode < 123)<br />

** Calls the search method if you hit a<br />

** letter or numeric key.<br />

This.Parent.Parent.Parent.Search(nKeyCode)<br />

ENDIF<br />

with this:<br />

LOCAL loThisContainer<br />

loThisContainer = This.Parent.Parent.Parent<br />

IF nKeyCode = 13 && They hit the ENTER key<br />

loThisContainer.Pop()<br />

ENDIF<br />

IF (nKeyCode > 48 AND nKeyCode < 58) OR ;<br />

(nKeyCode > 64 AND nKeyCode < 123)<br />

** Calls the search method if you hit a<br />

** letter or numeric key.<br />

loThisContainer.Search(nKeyCode)<br />

ENDIF<br />

As you say, the speed benefit is marginal, but the<br />

code is now much more readable and hence easier to<br />

maintain. I’m wondering why use a LOCAL variable<br />

if you can’t create a property, when you could use a<br />

PUBLIC variable to simulate the property and initialize<br />

it by placing goThisContainer = THIS in the container’s<br />

INIT() method.<br />

Paul: Aaargh! Splutter, cough . . . You’re playing devil’s<br />

advocate, aren’t you? Well, since there can be only one<br />

global variable, you’ll run into problems if your forms are<br />

modeless and can have multiple instances open in the<br />

application at a time—or even if you adopt this technique<br />

in every container class and just have two or more<br />

containers on the same form.<br />

Andy: All right! I give in; even though my forms would<br />

probably be modal anyway, the second reason is the<br />

clincher. (Just testing .)<br />

20 FoxTalk December 1998<br />

http://www.pinpub.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!