Rabu, 14 September 2011

Re: [MS_AccessPros] Some code I can't decipher

 

Duane,

Uh? It was good exercise? I was going to scan it and realized that would be a copyrighted issue. I did not type all the words exactly because, Exercise is UH? Exercise!!!!

 
Jim Wagner
________________________________

________________________________
From: Duane Hookom <duanehookom@hotmail.com>
To: Access Professionals Yahoo Group <ms_access_professionals@yahoogroups.com>
Sent: Wednesday, September 14, 2011 10:33 AM
Subject: RE: [MS_AccessPros] Some code I can't decipher

Jim,
Did you take the time to type all this in? I subscribe to Books 24x7 which has the full text of 1,000s of books (including this one and three of John's which I have the physical versions). While I don't care for sharing copyrighted material, snippets are probably acceptable if proper credit is given. John Viescas may need to weigh in on this. If you typed it in, I could have saved you a lot of time by simple copy and paste.

Duane Hookom
MS Access MVP

To: MS_Access_Professionals@yahoogroups.com
From: luvmymelody@yahoo.com
Date: Wed, 14 Sep 2011 10:05:54 -0700
Subject: Re: [MS_AccessPros] Some code I can't decipher

 

Lee,
here is the section.

Warning
Be careful when using the remove method inside loop. Because vba checks the value of the
count property only as it begins the loop but renumbers the items in the collection as you remove them, you will encounter a runitme error as the loop reaches its halfway point.
To remedy this problem, loop backward from the initial count value to 1. the sample code in frmTestFile uses this technique to remove blank lines from a file.

Disadvantages of Collection classes
Although collection calsses give you an added level of safety and flexibility, there is a downside
to using them; because by default vba treats your class as a normal object, not a collection, you lose two very handy collectio operators.

Firstl, with true collections, you normally do not need to specify the Item method when referring to objects within the collection. That's because item is a collection's default method
For example, in excel vba, the following two statements are equivalent:

Debug.Print Workbooks.item(1).Name
Debug.Print Workbooks.(1).Name

When using a collection class, however, you must always specify the itme method.
The For Each... Loop is the second feature that will not work with collection classes. If you
wish to enumerate all the objects in your collection, you must use a standad For... Next loop
with a numeric variable. Use the Count property to determine the number of objects in the collection and loop from 1 to this number.

you can, however, trick vba into treating your collection classes as if they were the built-in
collection class using a few undocumented techniques. These techniques rely on features
available in Visual basic 6 that aren't supported in the vba user interface. Specifically, you
can set attributes of procedures that define them as default members or enumeration functions,
the latter being what makes For Each...Loops work. VB offers a dialog box for setting these attributes
With vba, you must edit a class's source file on disk and then iport it into a project for the attributes to take effect.

To try this out, follow the steps outlined below:
1. Open the lines collection class in the Visual basic editor and add the following procedure

Public Function NewEnum() As IUnknown
' Enumeration function for the collection
Set NewEnum = mcolLines.[_NewEnum]
End Function
2. Select File Remove lines from the menu and click the Yes button when prompted to export the file.
3 Save lines.cls to a disk directory.
4. Open lines.cls in a text editor like notepad.
5. Find the NewEnum function and edit it so that it looks like this:

Public Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
' Enumeration function for the collection
Set NewEnum = mcolLines.[_NewEnum]
End Function

6.Find the Item method and edit it so that it looks like this:

Public Function Item(ByVal varID as Variant) As Line
Attribute Item.VB_UserMemId = 0
'Set return value of property to item within
'the private collection object specified by
'the passed index value (Note the return type)
Set Item = mcolLines(varID)
End Function
7. Save the lines.cls file.
8. Import the file back in the project by selecting file Import File and choosing
lines.cls from the Import dialog box.

You should be able to treat the Lines collection class just as you would the built-in vba
collection class. You can now edit the code shown earlier in Listing 3.12 to make it looke
like that shown in Listing 3.13. We think you will agree it is much simpler and more intutive.

Listing 3.13
Dim objLine as Line
'Assume objFile is an open TextFile object
For Each objLine in objFile.Lines
Debug.Print objLine.Text
Next

So how does it work? Defautl members and enumeration functions are defined by procedure attributes
stored in the source files. Even though vba won't let you edit them in the visual basic edit, it will honor them when you import files.
Perhaps there will be an easier way to do this in future versions.

Jim Wagner
________________________________

________________________________
From: Lee <leevt99@yahoo.com>
To: MS_Access_Professionals@yahoogroups.com
Sent: Wednesday, September 14, 2011 9:24 AM
Subject: Re: [MS_AccessPros] Some code I can't decipher

 
Oh they actually explain it in the book? Bah! I'll send an email around the office and see if anyone took it from me without asking >.< Thanks!

-Lee

--- In MS_Access_Professionals@yahoogroups.com, Jim Wagner <luvmymelody@...> wrote:
>
> Lee,
>
> On page 74-75 the code is found. The subheading is Disadvantages of Collection Classes.
>
>
> I do not have the time right now to type the text. maybe a little later in the afternoon.
>
> Â
> Jim Wagner
> ________________________________
>
>
>
> ________________________________
> From: Lee <leevt99@...>
> To: MS_Access_Professionals@yahoogroups.com
> Sent: Wednesday, September 14, 2011 8:56 AM
> Subject: [MS_AccessPros] Some code I can't decipher
>
>
>  
> Hi,
> I have the Access 2002 Developers Handbook (well I have the software for the book, no idea what happened to the book itself), and was trying to learn more about classes and how to use them. I ran across some strange looking code in one of the sample files and I can't figure out what it does, or why it's even there. It's part of a custom collection class.
>
> Public Function NewEnum() As IUnknown
> ' Enumeration function for the collection
> Set NewEnum = mcolLines.[_NewEnum]
> End Function
>
> There is no enumeration defined anywhere, but mcolLines is declared as a private class collection variable. This routine is also not called anywhere in the sample file. I pasted the entire class below.
>
> what is "IUnknown"? Or is that sort of redundant to ask? O.o
>
> Why are the brackets around [_NewEnum] and how does the underscore function?
>
> While I'm at it does anyone have an vba specific class resources?
>
> Thanks
> -Lee
>
> ' From Access 2002 Desktop Developer's Handbook
> ' by Litwin, Getz, and Gunderloy. (Sybex)
> ' Copyright 2001. All Rights Reserved.
>
> Option Compare Database
> Option Explicit
>
> ' Private collection to store Lines
> Private mcolLines As Collection
>
> Public Function NewEnum() As IUnknown
> ' Enumeration function for the collection
> Set NewEnum = mcolLines.[_NewEnum]
> End Function
>
> Private Sub Class_Initialize()
> ' Instantiate the Collection object
> Set mcolLines = New Collection
> End Sub
>
> Public Sub Add(ByVal strText As String, Optional ByVal varBefore As Variant)
> ' Declare new Line object
> Dim objLine As New Line
>
> ' Set Text property to passed string
> objLine.Text = strText
> ' Add to private collection, using object's
> ' ID property as unique index
> mcolLines.Add objLine, objLine.ID, varBefore
> End Sub
>
> Public Sub Remove(ByVal varID As Variant)
> ' Call Remove method of private collection object
> mcolLines.Remove varID
> End Sub
>
> Public Function Item(ByVal varID As Variant) As Line
> ' Set return value of property to item within
> ' the private collection object specified by
> ' the passed index value (Note the return type!)
> Set Item = mcolLines(varID)
> End Function
>
> Property Get Count() As Long
> ' Return Count property of private collection
> Count = mcolLines.Count
> End Property
>
> Property Let Changed(ByVal fChanged As Boolean)
> Dim objLine As Line
>
> ' Set Changed property of each Line to fChanged
> For Each objLine In mcolLines
> objLine.Changed = fChanged
> Next
> End Property
>
> Property Get Changed() As Boolean
> Dim objLine As Line
>
> ' Loop through all Line objects in collection--
> ' if any Changed property is True then the
> ' Changed property of the collection is True
> For Each objLine In mcolLines
> If objLine.Changed Then
> Changed = True
> Exit For
> End If
> Next
> End Property
>
>
>
>
> [Non-text portions of this message have been removed]
>

[Non-text portions of this message have been removed]

                       

[Non-text portions of this message have been removed]

------------------------------------

Yahoo! Groups Links

[Non-text portions of this message have been removed]

__._,_.___
Recent Activity:
MARKETPLACE

Stay on top of your group activity without leaving the page you're on - Get the Yahoo! Toolbar now.


A bad score is 598. A bad idea is not checking yours, at freecreditscore.com.
.

__,_._,___

Tidak ada komentar:

Posting Komentar