PB 1.64 - Passing type fields from typed array into function

Started by thaaks, November 18, 2008, 02:57:25 PM

Previous topic - Next topic

thaaks

Here's the issue:
I have an array of types (Containers(10) as TContainer). Each element has a field which is another type (element as TElement). Now I want to pass the typed field element of any container from the array into a function.

That doesn't work because the compiler complains.

Error in <Main.pba>, 38: 'printelement' Parameter(#1) is Incorrect Type, Expecting Typed Variable Handle Data Type

Here is the code snippet:

; PROJECT : Project2
; AUTHOR  : Thomas Haaks
; CREATED : 18.11.2008
; ---------------------------------------------------------------------

Type TElement
name$
amount
endtype

Type TContainer
size
element as TElement
endtype

Dim Containers(10) as TContainer

Dim TestElem as TElement
Dim oneElem as TElement ; used for allocation in loop

TestElem = new TElement
TestElem.name$ = "TestElem"
TestElem.amount = 14

; initialize the containers
for i = 1 to 10
Containers(i) = new TContainer
oneElem = new TElement
oneElem.name$ = "ContainerElement_" + str$(i)
oneElem.amount = i
Containers(i).size = i
Containers(i).element = oneElem()
next

PrintElement(TestElem())

for i = 1 to 10
PrintElement(Containers(i).element())
next

sync
waitkey

function PrintElement(elem.TElement)
print "name = " + elem.name$ + ", amount = " + str$(elem.amount)
endfunction


Any idea how to solve this or create a workaround?

Or am I doing something wrong?

Cheers,
Tommy

Big C.

hmmm... i think its not a bug...

Is this what you want to get?

cheers Big C.


Type TElement
name$
amount
EndType

Type TContainer
size
element As TElement
EndType

Dim Containers(10) As TContainer



; initialize the containers
For i = 1 To 10
Containers(i) = New TContainer
Containers(i).size = i
SetContainer(Containers(i).element,"ContainerElement_"+Str$(i),i)
Next

ShowContainer Containers()


Sync
WaitKey

Function SetContainer(me As TElement Pointer, name$, amount)
me.name$ = name$
me.amount = amount
EndFunction

Function ShowContainer(me().TContainer)
For i= 1 To 10
Print "size =" + Str$(me(i).size)
Print "name = " + me(i).element.name$ + ", amount = " + Str$(me(i).element.amount)
Next
EndFunction


thaaks

Let's say it this way: it works similar to what I want.

I could also change my loop to this:

for i = 1 to 10
;PrintElement(Containers(i).element())
print "name = " + Containers(i).element.name$ + ", amount = " + str$(Containers(i).element.amount)
next


and get the same result.

But what I really wanted to do is to pass the "element" field into a function. That doesn't seem to work as the compiler can't deal with the indexed container array first.

PS: Nice to see you posting here once in a while, Big C.! Greetings to the even northern part of Germany  ;D

kevin

   


Type TElement
name$
amount
endtype

Type TContainer
size
element as TElement
endtype

Dim Containers(10) as TContainer

Dim TestElem as TElement pointer
Dim oneElem as TElement  pointer; used for allocation in loop

TestElem = new TElement
TestElem.name$ = "TestElem"
TestElem.amount = 14

; initialize the containers
for i = 1 to 10
Containers(i) = new TContainer
oneElem = Containers(i).Element
oneElem.name$ = "ContainerElement_" + str$(i)
oneElem.amount = i
Containers(i).size = i
next

PrintElement(TestElem.TElement)

for i = 1 to 10
PrintElement(Containers(i).element)
next

sync
waitkey

function PrintElement(elem as TElement pointer)
print "name = " + elem.name$ + ", amount = " + str$(elem.amount)
endfunction

thaaks

Thanks, Kevin!

Am I guessing right that Containers(i).element just means "the pointer to the element but not the element itself" (reference instead of value)?

Is that true for all types inside types constructs like this:

Type TA
   foo
   bar
EndType

Type TB
   myInteger
   bla as TA
EndType

Dim MyB as TB

MyB.bla <- this would just be the pointer to a TA instance instead of a TA instance


What about MyB.myInteger? That's the integer itself but not a pointer...

I need to get this into my head somehow. One of the most time consuming issues I have with PB while coding with data structures...

kevin


  Accessing MyB.bla will automatically return a pointer to this part of the structure.

  This is all covered in Help->About->Types.   
 

Big C.

Quote
Let's say it this way: it works similar to what I want.

ok, then I did not understand you...  :(

But here is my way in combination with Kevins Solution (and I've read the tut about Types  :P)



; PROJECT : Project2
; CREATED : 19.11.2008
; ---------------------------------------------------------------------

Type TElement
name$
amount
EndType

Type TContainer
size
element As TElement
EndType

Dim Containers(10) As TContainer



; initialize the containers
For i = 1 To 10
Containers(i) = New TContainer
Containers(i).size = i
SetContainer(Containers(i).element,"ContainerElement_"+Str$(i),i)
Next

For i = 1 To 10
Print "Size = " + Str$(Containers(i).size)
PrintElement(Containers(i).element)
Next

Sync
WaitKey

Function SetContainer(me As TElement Pointer, name$, amount)
me.name$ = name$
me.amount = amount
EndFunction

Function PrintElement(elem As TElement Pointer)
Print "name = " + elem.name$ + ", amount = " + Str$(elem.amount)
EndFunction


Dont ask me why I do that... In my opinion it is a leaner solution   ::)

cheers
over and out  8)