Problem with array of custom type

Blitz3D Forums/Blitz3D Programming/Problem with array of custom type

alain(Posted 2007) [#1]
Hi there,

I have a problem on a simple Blitz3D program. I don't think it is a bug on Blitz3D but I cannot understand why I have this strange result.

Here is the program:

Type T1 
        Field val.T2[10] 
End Type 

Type T2
        Field x% 
End Type 


;create two instances of T1
x1.T1 = New T1
x2.T1 = New T1

;create one instance of T2 on x1 and one instance of T2 on x2
x1\val[0] = New T2
x2\val[0] = New T2

;get first pointer of T2 on x1 and x2
pointer1.T2 = x1\val[0]
pointer2.T2 = x2\val[0]

;count number of elements
i1 = 0
i2 = 0
While(pointer1<>Null)
	pointer1 = After(pointer1)
	i1 = i1+1
Wend

While(pointer2<>Null)
	pointer2 = After(pointer2)
	i2 = i2+1
Wend

Print("i1:"+i1)
Print("i2:"+i2)
WaitKey()
End


And result is:

i1:2
i2:1

What's wrong with this program? In my understanding I instanciate one T2 element on x1 and another one on x2...

Why do I have strangely two elements on x1 and one on x2?

thanks for any help or advice

have a nice day.


Stevie G(Posted 2007) [#2]
Hopefully this makes sense ...

Pointer1 and pointer2 are both pointing to the same type list which contains a total of two T2 instances. As the first pointer relates to the first instance in this list your count will find 2 instances before the list is null. The second pointer refers to the second instance of T2 in the list ( which is also the last ) so returns 1.

I think your understanding of how types work is wrong. You have no association between the T1 and T2 types so no way of knowing how many elements of T2 are associated with x1 & x2.

I've no idea what you're trying to do but is it this kind of thing?

Type T1
	Field val
End Type

Type T2
	Field Parent.T1
	Field x
End Type

x1.T1 = New T1
x2.T1 = New T1

;add 2 instances of T2 to x1
Create_T2( x1 , 15 )
Create_T2( x1 , 10 )

;add 1 instance of T2 to x2
Create_T2( x2 , 5 )

;count instances of T2 in x1
i1 = Count_T2( x1 )
;count instances of T2 in x2
i2 = Count_T2( x2 )


Print("i1:"+i1)
Print("i2:"+i2)
WaitKey()
End

;=====================================================
;=====================================================
;=====================================================

Function Create_T2( parent.T1 , x )

	this.T2 = New T2
	this\Parent = parent
	this\x = x
	
End Function

;=====================================================
;=====================================================
;=====================================================

Function Count_T2( parent.T1 )

	Count = 0

	For this.T2 = Each T2
		If Parent = this\Parent
			Count = Count + 1
		EndIf
	Next	

	Return Count
	
End Function



alain(Posted 2007) [#3]
Thanks for this clear reply.

I understood my mistake: It was not clear for me that we cannot have two separate collection of the same Type.

Types are global on Blitz3D, and it is not possible to have two separate list of the same type.

Well, I will use your proposed solution. It won't be as fast as my first guess but it will work.

Thanks again for your help.

alain