using #pb_any

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
MLK
User
User
Posts: 57
Joined: Sat Jan 24, 2004 8:46 pm
Location: Germany

using #pb_any

Post 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.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Usage is more like this:

Code: Select all

myFileHandle.l=CreateFile(#PB_Any,file$) 

If myFileHandle
  ; do things
EndIf
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

You must use a variable.

Code: Select all

filenumber = CreateFile(#PB_Any, file$)
If filenumber
  ;
  ; code here
  ;
EndIf
Timo
quidquid Latine dictum sit altum videtur
MLK
User
User
Posts: 57
Joined: Sat Jan 24, 2004 8:46 pm
Location: Germany

Post 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
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Well, what you suggest is not PureBasic syntax :wink:

Timo
quidquid Latine dictum sit altum videtur
MLK
User
User
Posts: 57
Joined: Sat Jan 24, 2004 8:46 pm
Location: Germany

Post 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 ?
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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
MLK
User
User
Posts: 57
Joined: Sat Jan 24, 2004 8:46 pm
Location: Germany

Post by MLK »

who is talking about the old way ?
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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.
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post 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...

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
MLK
User
User
Posts: 57
Joined: Sat Jan 24, 2004 8:46 pm
Location: Germany

Post 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
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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.
MLK
User
User
Posts: 57
Joined: Sat Jan 24, 2004 8:46 pm
Location: Germany

Post by MLK »

i dont think that "handle" ist the same like #file/#sprite/#window
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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.
Post Reply