PB Beginner - a few questions

Just starting out? Need help? Post your questions and find answers here.
User avatar
scanfff
User
User
Posts: 18
Joined: Thu May 30, 2019 10:02 am
Location: Seattle
Contact:

PB Beginner - a few questions

Post by scanfff »

Hi,

I come form a c/c++ background but have always loved tinkering in basic.

Firstly when I define an array:

Dim MyArray.i(10)

does the array follow C - 0-9 or like VB6 1-10. i.e

MyArray(0) = 100 or MyArray(1) = 100

Also my first attempt was to create an array of gadgets like so:

Dim Canvas_Gadgets.i(10)

For i = 1 To 10
Canvas_Gadgets(i) = CanvasGadget(#PB_Any, i*220, ImageHeight(0)/2, 320, 200, #PB_Canvas_Container)
Next i
BindEvent(#PB_Event_SizeWindow, @OnWindowResize(), 0)

....

; here's where I get the error -Line 19: Canvas_Gadgets() is not a function, array, list, map or macro. (line 19 is ResizeGadget)

Procedure OnWindowResize()
For i = 1 To 10
;line 19
ResizeGadget(Canvas_Gadgets(i), #PB_Ignore, WindowHeight(0)/2, #PB_Ignore, #PB_Ignore)
Next i
EndProcedure
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: PB Beginner - a few questions

Post by wilbert »

scanfff wrote:Dim MyArray.i(10)

does the array follow C - 0-9 or like VB6 1-10.
Neither.
It's 0 - 10 so the array has 11 elements.
https://www.purebasic.com/documentation ... e/dim.html

If you want to use the array inside a procedure, I think you need
Global Dim
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
scanfff
User
User
Posts: 18
Joined: Thu May 30, 2019 10:02 am
Location: Seattle
Contact:

Re: PB Beginner - a few questions

Post by scanfff »

wilbert wrote:
scanfff wrote:Dim MyArray.i(10)

does the array follow C - 0-9 or like VB6 1-10.
Neither.
It's 0 - 10 so the array has 11 elements.
https://www.purebasic.com/documentation ... e/dim.html

If you want to use the array inside a procedure, I think you need
Global Dim
Thanks, the Global fixed it!
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: PB Beginner - a few questions

Post by skywalk »

VB6 only uses 1 if you set Option Base 1.
PB starts from 0 and goes to the number in the index.
0-10 in your example.
This is not true with structured arrays.
MyStructArr.i[10] = 0-9.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Jeff8888
User
User
Posts: 40
Joined: Fri Jan 31, 2020 6:48 pm

Re: PB Beginner - a few questions

Post by Jeff8888 »

Slightly off topic. Here is a nice program I got from the forum that may be useful to you. It generates an assembly listing of your program so you can see how PB generates assembly code.

Code: Select all

;Generates and lists assembly code for file in line 2
file$="timeloop1.pb"
dir$ = GetPathPart(file$)
If file$ And FileSize(file$)>0
  Compiler = RunProgram(#PB_Compiler_Home+"\Compilers\pbcompiler", Chr(34)+dir$+file$+Chr(34)+" /COMMENTED ", dir$,#PB_Program_Hide|#PB_Program_Wait)
  dir$+"PureBasic.asm"
  If Compiler And FileSize(dir$)>0
    RunProgram("notepad.exe", dir$,"")
  Else
    MessageRequester("Error!", "There was an error creating the assembly code file.")
  EndIf
Else
  MessageRequester("Error!", "Bad input file")
EndIf
Post Reply