Does anybody have an example code, thats
a) made from normal procedural code
and another one doing the same but
b) made with objects
both doing the same?
In the case of the example i'd like to see which code is shorter and better readable.
Therefore it can be something where objects really show their muscles.
It should be not about accessing COM Servers, but just a very small, few line problem sollution.
I'd like to see something that gives me an impression of the advantages of the "new object world"
and can be diecty compard to something we already know (procedural code) .
Here is an example I was just getting ready to post on the PB Forum.
Not exactly what you were looking for but.....
James
Edit: Code was not correct. see listing for changed.
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'SED_PBWIN
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
#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
'Return Nacros
#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 cFileOpen
INSTANCE FileName,Folder,Filter,Start,DflExt,Title AS STRING
INSTANCE hParent,Flags AS DWORD
'THis is the offending code. This class is used as a base class so the CLASS METHOD CREATE
'does not get called. Only CLASS METHOD CREATE of the top derived Class is Called. It can be left with
'no ill effects as long as you realize if you use it as a base class the variables are not initialized.
CLASS METHOD CREATE
Filter = CHR$("All Files",0,"*.*",0)
Flags = %OFN_EXPLORER OR %OFN_FILEMUSTEXIST
END METHOD
INTERFACE iFileOpen : INHERIT IUNKNOWN
PropSet(FileName,STRING)
PropSet(Folder,STRING)
PropSet(Filter,STRING)
PropSet(hParent,DWORD)
PropSet(DflExt,STRING)
PropSet(Start,STRING)
PropSet(Title,STRING)
METHOD GetName() AS STRING
DISPLAY OPENFILE hParent,,,Title,Folder,Filter,Start,DflExt,Flags TO FileName
METHOD = FileName
END METHOD
END INTERFACE
END CLASS
'==============================================================================
CLASS cTextInBuffer
INSTANCE sFileName,sBuffer AS STRING
INTERFACE iTextInBuffer : INHERIT cFileOpen,iFileOpen
METHOD Fill(OPT FName AS STRING) AS STRING
LOCAL ff AS LONG
IF ISMISSING(FName) THEN
sFileName = MYBASE.GetName
ELSE
sFileName = FName
END IF
IF ISFILE(sFileName) = 0 THEN
? "Could not locate File"+$CR+sFileName
RET_M("")
END IF
ff = FREEFILE
OPEN sFileName FOR BINARY AS ff
GET$ ff,LOF(ff),sBuffer
CLOSE ff
METHOD = sBuffer
END METHOD
END INTERFACE
END CLASS
FUNCTION PBMAIN()
LOCAL oInBuffer AS iTextInBuffer
LOCAL sBuffer AS STRING
oInBuffer = CLASS "cTextInBuffer"
oInBuffer.Filter = CHR$("BASIC", 0, "*.BAS;*.INC;*.BAK", 0)
oInBuffer.Flags = %OFN_EXPLORER OR %OFN_FILEMUSTEXIST
sBuffer = oInBuffer.Fill()
? "Len of sBuffer = " + FORMAT$(LEN(sBuffer))
END FUNCTION
Hehe, your example is fine. Just missing is now the exactly same functionality in "normal" PB code.
But lets see if we get more contributions.
Any good sample is welcome.
Quote from: Theo Gottwald on August 14, 2008, 08:48:25 PM
Hehe, your example is fine. Just missing is now the exactly same functionality in "normal" PB code.
I think I've become addicted to Classes. It took so many years to get my head around them, now that I have, I can't think any other way ;D
James
We'll see if the same will happen to us also.
Quote from: Theo Gottwald on August 15, 2008, 10:07:35 PM
We'll see if the same will happen to us also.
It is happening to me as well.... I'm with James regarding PB objects. I love 'em. :)
Have just made my first real world object today.
Many CLASS METHODS, very few METHODS in the INTERFACE.
After all the whole thing is very good readable.
I can say that i could have done it without Object also.
But now its really a pleasure to use dynamic strings in Properties and this way built complex and complete (!)
Data-structures, which was not as easy as its now before in Version 8.
Anyway these first Object nearly has all the functionality of the whole programm.
And all the important Programm Data is in the Properties.
For this project I had no reasons to use Sub-Objects.
But there was one thing, I noted.
I used this MACRO:
#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
MACRO PropGS(PropN,PropT)=PropGet(PropN,PropT):PropSet(PropN,PropT)
#ENDIF
Because I like "obfuscated" code :-). I guess the Debugger doesn't.
I guess it makes the Single-Step Debugger run out of sync.
In this Project, the Breakpoints and Single-Step did't really work as expected.