Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Record Class

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

  • Record Class

    Another Simple Class Demo:

    James

    Code:
    '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    'SED_PBCC
    '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    '******************************************************************************
    ' Another Class Example using PowerBASIC PBCC 5.0
    ' I am lazy and the prop macro's really save a lot of typing. There are times
    ' where you'll need to use the actual PROPERTY METHODS. You can use any name for
    ' a property name but INSTANCE variables follow normal naming conventions.
    ' Below I want to use Name in the Record Class but compiler won't allow it.
    ' The C++ source this was modeled after was demostrating overloaded functions.
    ' Since we don't have that I use OPT parameters to determine which Address to use.
    ' I have individual properties for the variables but they are not used in this demo,
    ' and could have been eliminated but I left them in case someone wants to use this
    ' code to try other things.
    ' AS mentioned in one of the other Demos Never use CLASS METHOD CREATE in a Class
    ' you may want to use as a Base class for "implementation inheritance" as CLASS
    ' METHODS are not inherited.
    '******************************************************************************
    '                                James C. Fuller
    '                            [email protected]
    '                               August 5,2008
    '******************************************************************************
    'Saves on Typing
    #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
    #IF NOT %DEF(%RET_M_MACRO)
        %RET_M_MACRO = 1
        MACRO Ret_M(RetVal) = METHOD=RetVal:EXIT METHOD
    #ENDIF
    #IF NOT %DEF(%RET_F_MACRO)
        MACRO Ret_F(RetVal) = FUNCTION=RetVal:EXIT FUNCTION
        %RET_F_MACRO = 1
    #ENDIF
    '******************************************************************************
    CLASS cAddress1
    	INSTANCE Street,City,State,Zip AS STRING
    	INTERFACE iAddress1 : INHERIT IUNKNOWN
    		PropGet(Street,STRING)
    		PropSet(Street,STRING)
    		PropGet(City,STRING)
    		PropSet(City,STRING)
    		PropGet(State,STRING)
    		PropSet(State,STRING)
    		PropGet(Zip,STRING)
    		PropSet(Zip,STRING)
    		METHOD SetAddress(BYVAL sStreet AS STRING,BYVAL sCity AS STRING, BYVAL sState AS STRING,BYVAL sZip AS STRING)
    			Street = sStreet
    			City =  sCity
    			State = sState
    			Zip = sZip
    		END METHOD
    		METHOD Show()
    			PRINT Street
    			PRINT BUILD$(City,", ",State,"    ",Zip)
    		END METHOD
    
    	END INTERFACE
    
    END CLASS
    '------------------------------------------------------------------------------
    CLASS cAddress2
    	INSTANCE Dormitory,Room AS STRING
    	INTERFACE iAddress2 : INHERIT IUNKNOWN
    		PropGet(Dormitory,STRING)
    		PropSet(Dormitory,STRING)
    		PropGet(Room,STRING)
    		PropSet(Room,STRING)
    		METHOD SetAddress(BYVAL sDorm AS STRING,BYVAL sRoom AS STRING)
    			Dormitory = sDorm
    			Room = sRoom
    		END METHOD
    		METHOD Show()
    			PRINT BUILD$("Dormitory Building #",Dormitory," Room #",Room)
    		END METHOD
    	END INTERFACE
    
    END CLASS
    '------------------------------------------------------------------------------
    CLASS cAddress
    	INSTANCE OffCampus AS iAddress1
    	INSTANCE OnCampus AS iAddress2
    	INSTANCE AddressType AS LONG
    
    	INTERFACE iAddress : INHERIT IUNKNOWN
    'No overloading so this is as close as I can come.
    		METHOD SetAddress(P1 AS STRING,P2 AS STRING ,OPT P3 AS STRING,P4 AS STRING)
    			IF ISMISSING(P4) THEN
    				AddressType = 2
    				OnCampus = CLASS "cAddress2"
    				OnCampus.SetAddress(P1,P2)
    			ELSE
    				AddressType = 1
    				OffCampus = CLASS "cAddress1"
    				OffCampus.SetAddress(P1,P2,P3,P4)
    			END IF
    		END METHOD
    		METHOD Show()
    			IF AddressType = 1 THEN
    				OffCampus.Show
    			ELSE
    				OnCampus.Show
    			END IF
    		END METHOD
    
    	END INTERFACE
    
    END CLASS
    '------------------------------------------------------------------------------
    CLASS cName
    	INSTANCE First,Middle,Last AS STRING
    	INTERFACE iName :INHERIT IUNKNOWN
    		PropGet(First,STRING)
    		PropSet(First,STRING)
    		PropGet(Middle,STRING)
    		PropSet(Middle,STRING)
    		PropGet(Last,STRING)
    		PropSet(Last,STRING)
    		METHOD SetName(BYVAL sFirst AS STRING,BYVAL sMiddle AS STRING,BYVAL sLast AS STRING)
    			First = sFirst
    			Middle = sMiddle
    			Last = sLast
    		END METHOD
    		METHOD Show()
    			PRINT BUILD$(First," ",Middle," ",Last)
    		END METHOD
    	END INTERFACE
    
    END CLASS
    '------------------------------------------------------------------------------
    CLASS cRecord
    	INSTANCE oName AS iName
    	INSTANCE oAddress AS iAddress
    	CLASS METHOD CREATE
    		oName = CLASS "cName"
    		oAddress = CLASS "cAddress"
    	END METHOD
    
    	INTERFACE iRecord	:INHERIT IUNKNOWN
    ' No macro here because I want to use Name for the GET but
    ' compiler won't allow INSTANCE variable "Name".
    ' To keep it consistant I use the same for address
    		PROPERTY GET Name() AS iName
    			PROPERTY = oName
    		END PROPERTY
    
    		PROPERTY GET Address() AS iAddress
    			PROPERTY = oAddress
    		END PROPERTY
    	END INTERFACE
    END CLASS
    '------------------------------------------------------------------------------
    FUNCTION PBMAIN() AS LONG
    	LOCAL oStudent() AS iRecord
    	LOCAL i AS LONG
    	REDIM oStudent(1 TO 3) AS iRecord
    	FOR i = 1 TO 3
    		oStudent(i) = CLASS "cRecord"
    		IF ISNOTHING(oStudent(i)) THEN
    			PRINT "No oStudent "+FORMAT$(i)
    			WAITKEY$
    			RET_F(0)
    		END IF
    
    	NEXT i
    
    
        oStudent(1).Name.SetName("Norman","W.","Eggler")
    	oStudent(1).Address.SetAddress("44 East Ave","Miami","FL","33101")
    	oStudent(1).Name.Show
    	oStudent(1).Address.Show
    	PRINT "----------------------"
        oStudent(2).Name.SetName("William","","George")
    	oStudent(2).Address.SetAddress("17A","234")
    	oStudent(2).Name.Show
    	oStudent(2).Address.Show
    	PRINT "----------------------"
        oStudent(3).Name.SetName("Carol","L.","Liebert")
    	oStudent(3).Address.SetAddress("17B","18")
    	oStudent(3).Name.Show
    	oStudent(3).Address.Show
    
    	WAITKEY$
    END FUNCTION
    Attached Files
Working...
X