Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    structure type as parameter (Word XP et al.)

    Has anyone invented a neat-trick to pass the item of a structure as a parameter to a procedure?

    I have a simple structure that I use to record details of a set of files (MRUStorageFileRecord)
    I have a SELECT CASE statement that switches based on a user-selected key value (Select Case strKeySequence)
    I have a sort routine that works well on the full name, used as a preliminary to drop duplicate items from my basic data list.

    I'd like to re-use the sort code and drive it by any one of the items of my structure.
    In essence, I'd like the <font color=red> bold red</font color=red> code below to be a parameter

    <pre>'''
    ''' The storage record contains details about every file that has appeared on the desktop.
    '''
    Type MRUStorageFileRecord
    strFullName As String
    strPath As String
    strName As String
    strExtent As String
    blnArchived As Boolean
    dtDateSaved As Date
    lngSize As Long
    dtDateUsed As Date
    End Type

    '''
    ''' An array of this structure is to be sorted according to a key chosen at run-time:
    '''
    Select Case strKeySequence
    Case strcFullName
    Case strcPath
    Case strcName
    Case strcExtent
    Case strcArchived
    Case strcDateSaved
    Case strcSize
    Case strcDateUsed
    Case Else
    Call u.SpeechText.SpeakText("Inavlid sort sequence")
    End Select

    '''
    ''' We sort the structure array by extracting a key field and index, and sorting THAT
    '''
    Public Function SortArray(strIN() As MRUStorageFileRecord, strOUT() As MRUStorageFileRecord)
    Dim strFiller As String
    strFiller = String(lngMaxlength, " ") ' padding to ensure a fixed-length key
    Dim strKey() As String
    ReDim strKey(0)
    Dim lng As Long
    For lng = LBound(strIN) To UBound(strIN)
    ''' Our key is the full name (fixed length) with a trailing index pointer to the array.
    strKey(UBound(strKey)) = Left$(strIN(lng)<font color=red>.strFullName</font color=red> & strFiller, lngMaxlength) & Format(lng, "00000")
    ReDim Preserve strKey(UBound(strKey) + 1)
    Next lng
    </pre>


    As usual, I can't think of a reason not to be able to do this, excepting that the VB designers don't allow it. I can't locate any suitable method for doing it. I can pass the value of one of the items as a parameter, but not the "definition" of the item istelf.
    Generating eight separate sort routines isn't going to be the end of the world; I could always nest the SELECT CASE within the basic sort code anyway.

    These TYPE structures are what we used back in the good old days, but I didn't then ever want to use them like this, so i have little in the way of experience.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: structure type as parameter (Word XP et al.)

    I think we've had some threads searching for magical ways to "calculate" the name of a variable. In this one, for example, there is reference to an Eval function in Access VBA, and using CallByName with objects (not ordinary variables). I haven't come up with anything for arrays of UDTs, though.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: structure type as parameter (Word XP et al.)

    <!profile=MarkD>MarkD<!/profile> posted an example in the thread Jefferson refers to that shows how you can refer to a property of a class indirectly using CallByName. You could replace your MRUStorageFileRecord type with a class of that name, with the members of the type as properties.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: structure type as parameter (Word XP et al.)

    > using CallByName with objects

    Thanks Jefferson; an interesting post.

    I'm going to pass on classes for this one, though; embedding the Select Case within the Sort routine works, and lets me skirt, once more, Class objects, with which I am only slightly familiar (mainly because I shy away from most examples!).

    I ran the code and can see how it works, but it won't be an elegant solution, as you point out, in my case (sic!).

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: structure type as parameter (Word XP et al.)

    Thanks, Hans, but in this case the class seems like to big a hammer to crack the nut.

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: structure type as parameter (Word XP et al.)

    Try declaring a variable of the UDT type and assign each element of the array to that variable as you loop through the array. Then use the variable to reference the items in the type. The UDT has to be publicly declared, of course. One alternative would be to use a collection instead of an array, which would also allow you to use a For...Each structure to step through the collection.
    Charlotte

  7. #7
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: structure type as parameter (Word XP et al.)

    A class can be a teensy-weensy little hammer.

    Create a Class Module named MRUFileStorageRecord. Insert the following 9 lines:

    Option Explicit 'This line's already there, of course.
    Public strFullName As String
    Public strPath As String
    Public strName As String
    Public strExtent As String
    Public blnArchived As Boolean
    Public dtDateSaved As Date
    Public lngSize As Long
    Public dtDateUsed As Date

    That's it, I swear, that's the entire Class Module. There's no need to set the properties up with Property Get and Property Let procedures.

    You can try it out with the following test procedure, then use the same CallByName method in your sort routine:

    <pre>Sub test1230()

    Dim C_SFR As MRUStorageFileRecord

    Set C_SFR = New MRUStorageFileRecord

    C_SFR.strFullName = "Easier Than I Thought"

    Debug.Print CallByName(C_SFR, "strFullName", VbGet)

    Set C_SFR = Nothing

    End Sub
    </pre>


  8. #8
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: structure type as parameter (Word XP et al.)

    What's that you say? You want to be able to use the class in a project other than the class's home project?

    Well, OK, I admit that part's a little tricky:

    1. Export the class module.

    2. Pull the exported .CLS file up in your favorite text editor and change the following 2 lines (both near the start) from False to True:

    Attribute VB_Creatable = True
    Attribute VB_Exposed = True

    3. Go back to the class module, clean out the existing text and use Insert-->File to insert the edited .CLS file into the module. (Note that importing the .CLS file isn't a workable alternative because that resets VB_Creatable to False.)

    4. Delete the 3 or 4 extraneous added lines at the top of the module.

    5. Compile and save. Your class is now usable by any project that has a Reference set to the class's home project.

  9. #9
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: structure type as parameter (Word XP et al.)

    Sorry for the delayed response, but I was just doing some data-structure pondering/rereading and remembered this thread.

    I think either a collection or a Scripting.Dictionary (in place of your UDT) would also easily allow you to have values with strings (i.e., keys) for names.

    Charlotte mentioned using a collection and a For Each loop, but unless I'm missing something there'd be no need for a loop in your situation. Here's sample code using a Dictionary:

    <pre> Dim dctSFR As Scripting.Dictionary

    Set dctSFR = New Scripting.Dictionary

    dctSFR("strFullName") = "Chris Greaves"

    Debug.Print dctSFR("strFullName")

    Set dctSFR = Nothing</pre>


    Added notes (since I been readin' today):

    1. Unlike UDT's, you can pass Dictionaries (or collections) as optional arguments.
    2. I understand Dictionaries have blossomed into HashTables in VB.Net, so Dictionaries are arguably a "forward-looking" way to handle key-value pairs.

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: structure type as parameter (Word XP et al.)

    Steve, no need to apologise; i hope to have many more years yet to learn all there is to learn about computers (grin!).

    Your comments were timely as this <!post=this post,445871>this post<!/post> has also triggered my curiousity about Scripting Dictionaries.

    I have yet to sit down and study these, but I think that a dictionary can be, in loose terms, any structure I choose.

  11. #11
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: structure type as parameter (Word XP et al.)

    Thanks for that excellent post, Steve. A dictionary might be the answer to what I'm wanting to achieve in another context. Do you know of any good online references for a VB6 incarnation?

    Alan

  12. #12
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: structure type as parameter (Word XP et al.)

    I'm not too familiar with online references for VB. Here's the official Microsoft Visual Basic Programmers Guide.

    My favorite Access VBA book has always been the Developer's Handbook (I own the 2.0, 97 and 2002 editions) but, for some reason, the 2002 edition makes virtually no use or mention of Scripting.Dictionaries. There's an entirely separate VBA Developer's Handbook (authors overlap) that I don't own but that I believe is also very highly regarded.

    VB & VBA In a Nutshell is a handy reference (and the 100 pages before the actual "Reference" begins have a lot of useful info as well), and its author (Lomax) is definitely fond of Dictionaries.

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: structure type as parameter (Word XP et al.)

    Steve, a very belated thanks for this. I've finally gotten around to setting aside time to deal with it, and will be using a new "task" as a means of testing my knowledge level.

  14. #14
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: structure type as parameter (Word XP et al.)

    Subsequent to my posting <post#=439508>post 439508</post#> (in this thread), Rory posted <post#=456569>post 456569</post#>, which offers a handy VB method for setting a class's Exposed and Creatable attributes to True (without having to go through the export/edit/import routine).

  15. #15
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: structure type as parameter (Word XP et al.)

    Plus (6.) "Dictionaries are an ideal way to pass parameters to macros when communicating between applications"
    This thread saved my bacon ....

    See also this later thread <post:=731,145>post 731,145</post:>

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •