Question about Type and Each command

Blitz3D Forums/Blitz3D Beginners Area/Question about Type and Each command

Baley(Posted 2004) [#1]
Please try this source:

Type chair
 Field colour$
 Field Height%
End Type

mychair1.chair = New chair
mychair2.chair = New chair

mychair1\colour$="brown"
cont=1

For mychair1.chair= Each chair
 Print "ELEMENT: "+cont+" - "+mychair1\colour$
 e$=Input()
 cont=cont+1
Next

End


Is it normal mychair1 has two elements ?? I'm using B+. Thanks.


semar(Posted 2004) [#2]
Yes it is normal, they are indeed two different pointers, but both point to the same structure; that means that they go through the same list, thus the same element collection.

If you want two different lists, you can choose between:

1) make two different type structure and pointers (type chair_1 and chair_2, pointer mychar_1 and mychar_2)
OR
2) use only one type structure and one pointer, and add an additional field - ex. ID - which can be set each time you create a new element. Example (one structure and one pointer):

Type chair
 Field ID ;--------> New Field added !
 Field colour$
 Field Height%
End Type

mychair.chair = New chair
mychair.ID = 1 ;here we set the new ID field
mychair\colour$="Blue"

mychair.chair = New chair
mychair.ID = 2 ;here we set the new ID field
mychair\colour$="brown"

cont=1

;Display only the chair with ID = 1
For mychair.chair= Each chair
 If mychair\ID = 1 then
   Print "ELEMENT: " + cont + " - "+mychair\colour$
   e$=Input()
   cont=cont+1
 EndIf
Next

End


Personally, I would use the 2nd choice.

Sergio.


Zethrax(Posted 2004) [#3]
'mychair1' is just a pointer variable that points to an element, it doesn't actually contain elements.

The For Each loop is iterating through the chain of elements that you've created of type 'chair', and the pointer to each element is being placed in 'mychair1.chair' during each pass through the loop, overwriting the previous pointer value in 'mychair1.chair'.


Baley(Posted 2004) [#4]
Thanks for reply.

So mychair1 and mychair2 are pointers to newly created elements of ONE list. Then, the TYPE command DOES create a list and the NEW command adds elements to that list.

I thought the TYPE command was more like STRUCT in C and that a list was created by NEW command.
;IT CREATES A LIST CALLED chair
Type chair
 Field colour$
 Field Height%
End Type

;IT ADDS TWO ELEMENTS TO LIST chair AND ASSIGNS POINTERS
;TO VARIABLES mychair1 AND mychair2 .
mychair1.chair = New chair
mychair2.chair = New chair

mychair1\colour$="brown"
cont=1

For mychair1.chair= Each chair
 Print "ELEMENT: "+cont+" - "+mychair1\colour$
 e$=Input()
 cont=cont+1
Next

End

It's a bit different from C or other Basic dialects. It can be a bit confusing if one knows how lists work in C. In PureBasic for example, it would be:
;IT DEFINES A NEW TYPE
Structure chair
 colour$
 height.l
EndStructure

OpenConsole()

;IT CREATES TWO LISTS OF chair TYPE
NewList mychair1.chair()
NewList mychair2.chair()

;IT ADDS ONE ELEMENT TO LIST mychair1
AddElement(mychair1()) 
;IF I NEED A POINTER TO THAT ELEMENT:
;*p=AddElement(mychair1()) 

mychair1()\colour$="brown"
cont=1

ResetList(mychair1()) 
While NextElement(mychair1()) 
 PrintN("ELEMENT: "+Str(cont)+" - "+mychair1()\colour$)
 e$=Input()
 cont=cont+1
Wend

End



semar(Posted 2004) [#5]
Yep, you've got it.

I agree with you that the way types work right now, could be confusing. Perhaps this will change in the future...

Sergio.