Page 1 of 2
using #pb_any
Posted: Mon Apr 19, 2004 9:59 am
by MLK
just want to say...
i like this more:
Code: Select all
If CreateFile(#file=#PB_Any,file$)
DeleteFile(file$)
EndIf
then:
Code: Select all
If #file=CreateFile(#PB_Any,file$)
DeleteFile(file$)
EndIf
i dont really understand.. how it is working now (second method) - does it returns the handle or the counted constant, or the success of operation ? i would say the first way is the better one.
Posted: Mon Apr 19, 2004 10:02 am
by Dare2
Usage is more like this:
Code: Select all
myFileHandle.l=CreateFile(#PB_Any,file$)
If myFileHandle
; do things
EndIf
Posted: Mon Apr 19, 2004 10:06 am
by freak
You must use a variable.
Code: Select all
filenumber = CreateFile(#PB_Any, file$)
If filenumber
;
; code here
;
EndIf
Timo
Posted: Mon Apr 19, 2004 10:07 am
by MLK
Dare2 wrote:Usage is more like this:
Code: Select all
myFileHandle.l=CreateFile(#PB_Any,file$)
If myFileHandle
; do things
EndIf
thats ugly
Posted: Mon Apr 19, 2004 10:08 am
by freak
Well, what you suggest is not PureBasic syntax
Timo
Posted: Mon Apr 19, 2004 10:08 am
by MLK
freak wrote:You must use a variable.
Code: Select all
filenumber = CreateFile(#PB_Any, file$)
If filenumber
;
; code here
;
EndIf
Timo
ugly too
does anybody like my suggestion ?
Posted: Mon Apr 19, 2004 10:12 am
by Dare2
hehe.

I'll send it to a beauty parlour.
The old way is still available:
Code: Select all
Result = CreateFile(#File, FileName$)
If result
; do things
EndIf
Posted: Mon Apr 19, 2004 10:19 am
by MLK
who is talking about the old way ?
Posted: Mon Apr 19, 2004 10:39 am
by GedB
The Do or Die idiom is one approach. Unfortunately Fred has said that the approach works by accident and will not guarantee that the behaviour will remain:
viewtopic.php?t=9926&start=15
A similar approach is to create a HandleError() funciton:
viewtopic.php?t=9860
Now that Purebasic is growing, and being applied to bigger projects then I cleaner syntax for handling errors and exceptions really is needed. How to do this without spoiling the simplicity is another matter.
Fred has promised Macros in 4. Perhaps that is the answer.
Posted: Mon Apr 19, 2004 10:43 am
by Dare2
Hiya MLK.
who is talking about the old way ?
I was.
I can't quite work out what you are trying to achieve with the proposed syntax. As far as I can see, it removes the need to type an extra line (
If ... verses
Assign and then
If ...) but introduces an implied function that creates a file handle:
If CreateFile(
myfile.l=#PB_Any,file$)
Thereafter
myFile is available for use with the file. However both the old and new (#PB_Any) methods provide a way to reference the file without having to introduce a new syntax. The new way requires one extra statement in the preceding examples.
Posted: Mon Apr 19, 2004 11:14 am
by LarsG
personally, I find:
Code: Select all
If CreateFile(#file=#PB_Any,file$)
DeleteFile(file$)
EndIf
to be uglier than:
Code: Select all
filenumber = CreateFile(#PB_Any, file$)
If filenumber
;
; code here
;
EndIf
because I find assigning stuff as parameters messy..
but I guess it's all about personal preference...
Posted: Mon Apr 19, 2004 11:42 am
by MLK
do i missunderstand, if i say the handle is something different to the #constant ?
if i write:
Code: Select all
handle=openfile(0,file$)
handle=openfile(#x,file$)
handle=openfile(#pb_any,file$)
;but which is the #fileconstant for closefile(?)
handle=#fileconstant=openfile(#pb_any,file$)
;and how is returned, if the file is succesfull opened ?
if handle=#fileconstant=openfile(#pb_any,file$) = 1 ???
tell me what i missunderstood
Posted: Mon Apr 19, 2004 11:58 am
by Dare2
Not sure if I understand exactly what you are asking or suggesting, but here is my take on your question. Hope it is on track.
Code: Select all
; -- Original way gives results,
; result is file id, but references to the file
; are made with the value you used to open file.
result=OpenFile(0,file$)
UseFile(0)
CloseFile(0)
result=OpenFile(#x,file$)
UseFile(#x)
CloseFile(#x)
; -- New option -
; result is now the "handle" used to
; reference or, um, handle the file.
handle=OpenFile(#pb_any,file$)
UseFile(handle)
CloseFile(handle)
IMO the new or #PB_Any way for gadgets and objects means that there is now one less value to worry about. The "handle" is it.
Posted: Mon Apr 19, 2004 12:17 pm
by MLK
i dont think that "handle" ist the same like #file/#sprite/#window
Posted: Mon Apr 19, 2004 12:33 pm
by Dare2
lol, MLK, I am losing the plot.
In places where #PB_Any is valid usage (and this is as I understand it), if #PB_Any is used to establish an item, then a value is returned.
The returned value is then used whenever a reference to the item is required. The technical name for the returned value may be "handle" or "ID" or "banana", but whatever it is, that is how the item is referenced.
Using the original way, a value was used to establish an item, and another value was returned.
With the original method, sometimes the first value was required to identify the item to be used, and sometimes the second.
However, as I said, I am losing the plot, and I'm not exactly sure what we are discussing, so I will bow out now.