News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Dim / ReDim... Bug or no Bug?

Started by Big C., August 14, 2010, 10:15:19 AM

Previous topic - Next topic

Big C.

Hi Kevin,

i'm confused with the commands Dim and Redim and I don't know whether its a bug or not

My Testcode:

ForceDeclaration

Type TObject
A
B
C
EndType

Dim MyObject(0) As TObject

Dim MyObject(5)


Repeat
Cls

Print "MyObject("+Str$(GetArrayElements(MyObject()))+")"

Sync

WaitKey

PopulateMyObject()

MyObject(1).A = 100

Print "A = "+Str$(MyObject(1).A)

Sync
Flushkeys
waitkey
Until EscKey()

Psub PopulateMyObject()

ReDim MyObject(10)

Print "Elements of MyObject("+Str$(GetArrayElements(MyObject()))+") after ReDim"
Sync
FlushKeys
WaitKey
ReDim MyObject(20) As TObject
Print "Elements of MyObject("+Str$(GetArrayElements(MyObject()))+") after ReDim"
Sync
FlushKeys
WaitKey


EndPsub


First run w/o modifaction will run fine but it seems not logical because after the second redim u will get 10 elements and not 20 and u can assign the UDT A-Variable a value

Second Run after commented out the second Dim-Command
- first u have 0 Elements
- after first redim u get 10, then 20 elements
- u can assign a value to the UDT-Variable A too

Third Run after commented out the second Dim- and second Redim-Command
- first u have 0 Elements
- after redim u get 10 elements
- ERROR

Fourth Run after only commented out the seceon Redim-Command
- first u have 5 Elements
- after redim u get 10 elements
- ERROR

Fifth Run after commenented out the second Dim- and first Redim-Command
- first u have 0 Elements
- after redim u get 20 elements
- and u can assign A a value

Is there a bug because it seems that MyObject and UDT MyObject are in some cases not the same? If so what will happens if u free the memory with UnDim MyObject()? Or I'm totally wrong?

ty Big C.






kevin

#1
 from what i can tell you're attempting to refer to two different structures of the same name, using short hand notation.

ie.

Type Stuff
   x,y,
 End type

 Dim Things(100) as Stuff

The array handle for the Things array could either be    

  Things()   ; short hand or typed array/variable/list

  or

  Things().stuff   ; long hand.  Declaring the this is typed array/variable/list.

 
  if you have two arrays of the same name, but different types and refer to them both using the short hand notation, then PB can't obtain the type from the handle, so it'll have to assume it's an Integer Array.  Which is what it's doing in this case.  
 
  So you're example should be this.

PlayBASIC Code: [Select]
ForceDeclaration

Type TObject
A
B
C
EndType

Dim MyObject(0) As TObject

Dim MyObject(5)


Repeat
Cls

Print "MyObject("+Str$(GetArrayElements(MyObject()))+")"

Sync

WaitKey

PopulateMyObject()

MyObject(1).A = 100

Print "A = "+Str$(MyObject(1).A)

Sync
Flushkeys
waitkey
Until EscKey()

Psub PopulateMyObject()

ReDim MyObject(10)

Print "Elements of MyObject("+Str$(GetArrayElements(MyObject()))+") after ReDim"
Sync
FlushKeys
WaitKey
ReDim MyObject(20) As TObject
Print "Elements of MyObject("+Str$(GetArrayElements(MyObject().tobject))+") after ReDim"
Sync
FlushKeys
WaitKey


EndPsub





Big C.

#2
An attempt should be less. Are by accident encountered the problem, after I looked for 2 hours  :( my own (logical) error yesterday. Also careful reading of the helpfile didn't bring me appropriate solution/reference. Try and error helps me :P

Thanks for the explanation.  :)

BTW:
UnDim MyObject() will free the MyObject-Array and UnDim MyObject.TObject will free the UDT MyObject-Array separatly from Memory.
UnDim MyObject() As TObject will produce an error although Redim MyObject() As TObject will works fine but produce an error if u write ReDim MyObject().TObject  ???

Thx

kevin

#3
Quote
UnDim MyObject() will free the MyObject-Array and UnDim MyObject.TObject will free the UDT MyObject-Array separatly from Memory.

If there's more than one array called MyObject  then, you need to use the long hand notation for the handle.  


Quote
UnDim MyObject() As TObject will produce an error although Redim MyObject() As TObject will works fine but produce an error if u write ReDim MyObject().TObject  Huh

 I don't really understand your question.  You're mixing handles with declarations..

 Dim and ReDim are the same, they're declarations and initialization commands.  The only difference is that ReDim preserves the contents of the array that's being resized, if it existed.  

  UNDIM is to free an array.