Announcement

Collapse
No announcement yet.

Class / Function comparison

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Class / Function comparison

    I hope this will give some insight on the advantages of Class usage as it pertains to me in one specific category.I am getting up there in age and my memory, never too good to begin with, is really starting to annoy. Having to remember the number and position of parameters is frustrating.
    One of the new additions, "DISPLAY OPENFILE" looked like a particularly good candidate to encapsulate in a class. I list here a Class and a standard function that do essentially the same thing. The problem with the Function; I still have to remember parameter type and position for anything other than the default. Also you can't skip parameters even if they are all optional.

    James

    Code:
    'SED_PBWIN
    #COMPILE EXE
    #DIM ALL
    #IF NOT %DEF(%Prop_MACROS)
        %Prop_MACROS = 1
        MACRO PropGet(PropName,PropType)=PROPERTY GET PropName() AS PropType:PROPERTY=PropName:END PROPERTY
        MACRO PropSet(PropName,PropType)=PROPERTY SET PropName(BYVAL param AS PropType):PropName=param:END PROPERTY
    #ENDIF
    '******************************************************************************
    'Uses CLASS METHOD CREATE -> Do not use as a base class
    '******************************************************************************
    CLASS cDisplayOpenFile
        INSTANCE sFileName,Folder,Filter,Start,DflExt,Title AS STRING
        INSTANCE hParent,Flags AS LONG
    
        CLASS METHOD CREATE
            Filter = CHR$("All Files",0,"*.*",0)
            Flags = %OFN_EXPLORER OR %OFN_FILEMUSTEXIST
            Title = "Select File"
        END METHOD
    
        INTERFACE iDisplayOpenFile : INHERIT IUNKNOWN    
            PropSet(Folder,STRING)
            PropSet(Filter,STRING)
            PropSet(hParent,DWORD)
            PropSet(DflExt,STRING)
            PropSet(Start,STRING)
            PropSet(Title,STRING)
            PropSet(Flags,DWORD)
            METHOD GetName() AS STRING
                DISPLAY  OPENFILE hParent,,,Title,Folder,Filter,Start,DflExt,Flags TO sFileName
                METHOD = sFileName
            END METHOD
        END INTERFACE
    END CLASS
    '******************************************************************************
    'Using a Function
    '******************************************************************************
    FUNCTION fDisplayOpenFile(OPT   lhParent     AS LONG, _
                                    sTitle       AS STRING, _
                                    sFolder      AS STRING, _
                                    sFilter      AS STRING, _
                                    sStart       AS STRING, _
                                    sDflExt      AS STRING, _
                                    lFlags       AS LONG) AS STRING
    
        LOCAL sFileName,Folder,Filter,Start,DflExt,Title AS STRING
        LOCAL hParent,Flags AS LONG
    
        IF ISMISSING(lhParent) THEN    
            hParent = 0
        ELSE
            hParent = lhParent
        END IF    
        IF ISMISSING(sTitle) THEN
            Title = "Select File"
        ELSE
            Title = sTitle
        END IF
        IF ISMISSING(sFolder) THEN
            Folder = ""
        ELSE
            Folder = sFolder
        END IF
        IF ISMISSING(sFilter) THEN
            Filter    = CHR$("All Files",0,"*.*",0)
        ELSE
            Filter = sFilter
        END IF
        IF ISMISSING(sStart) THEN
            Start = ""
        ELSE
            Start = sStart
        END IF
        IF ISMISSING(sDflExt) THEN
            DflExt = ""
        ELSE
            DflExt = sDflExt
        END IF
        IF ISMISSING(lFlags) THEN
            Flags = %OFN_EXPLORER OR %OFN_FILEMUSTEXIST
        ELSE
            Flags = lFlags
        END IF        
                        
        DISPLAY  OPENFILE hParent,,,Title,Folder,Filter,Start,DflExt,Flags TO sFileName
        FUNCTION = sFileName
        
    END FUNCTION                                
    
    FUNCTION PBMAIN()
        LOCAL oGetFile AS iDisplayOpenFile
        LOCAL sFile AS STRING
    
    'First the Class using all defaults
        oGetFile = CLASS "cDisplayOpenFile"
        sFile = oGetFile.GetName
        ?sFile
    
    'Now the Function using all defaults
        sFile = fDisplayOpenFile 
        ?sFile
    
    'Now the Class with a new filter, title And Flags
        oGetFile.Filter = CHR$("BASIC", 0, "*.BAS;*.INC", 0)
        oGetFile.Title = "Please Select a Basic File using The Class Interface"
        oGetFile.Flags = %OFN_EXPLORER OR %OFN_FILEMUSTEXIST
        sFile = oGetFile.GetName
        ?sFile
    
    'The Function
    'Even though all parameters are optional you cant skip
    ' Not Valid  ->  fDisplayOpenFile ,,,CHR$("BASIC", 0, "*.BAS;*.INC", 0)
    'You have to send a BYVAL 0 for all the preceeding
    'And you have to remember the exact paramater order 
    
        sFile = fDisplayOpenFile(BYVAL 0, _
                                 "Please Select a Basic File using The Function" , _
                                 BYVAL 0, _
                                 CHR$("BASIC", 0, "*.BAS;*.INC", 0), _
                                 BYVAL 0, _
                                 BYVAL 0, _
                                 %OFN_EXPLORER OR %OFN_FILEMUSTEXIST)
        ?sFile                         
    END FUNCTION
Working...
X