Types problem

Blitz3D Forums/Blitz3D Beginners Area/Types problem

Ross C(Posted 2004) [#1]
Hi, having a little problem with types here. I'm getting an "Illegal Type Conversion Error" when i am trying to return a type from a function.

Type car
	Field x
End Type

c.car = New car
c\x = 10
c.car = New car
c\x = 60
c.car = New car
c\x = 50
c.car = New car
c\x = 40
c.car = New car
c\x = 30
c.car = New car
c\x = 20

c.car = findcar()
Print c\x




Function findcar()

	For t.car = Each car
		If t\x = 10 Then
			Return t.car
		End If
	Next

End Function


I know i could use the Handle and Object method, but i thought that would work above... Am i doing anything wrong? Thanks for any help you can give :o)


Duckstab[o](Posted 2004) [#2]
in your function it sould be c.car not t.car

:)


GitTech(Posted 2004) [#3]
Function findcar.car()

For t.car = Each car
If t\x = 10 Then
Return t.car
End If
Next

End Function



Duckstab[o](Posted 2004) [#4]
Function findcar()

For c.car = Each car
If c\x = 10 Then
Return c.car
End If
Next

End Function


Duckstab[o](Posted 2004) [#5]
you never assigned t. as a type diddnt need to maybe
give your types a good name so you remmder then


Duckstab[o](Posted 2004) [#6]
/


Perturbatio(Posted 2004) [#7]
the name of the variable does not matter, it could be flobbynorglebob.car as GitTech has posted, the function did not have a return type.


Duckstab[o](Posted 2004) [#8]
depend what the rest ov his code was mate


Ross C(Posted 2004) [#9]
Nice one GitTech :o) Thanks!


Perturbatio(Posted 2004) [#10]
depend what the rest ov his code was mate

Nope, doesn't matter, the variable used in the function is local, not global.
It is destroyed when the function has exited.


Duckstab[o](Posted 2004) [#11]
car has to be global to work in a function


cermit(Posted 2004) [#12]
Isnt types always global?


Duckstab[o](Posted 2004) [#13]
if it not global no recognisd outside the main loop


Orca(Posted 2004) [#14]
Types are always global, but the references to them are not.


Ross C(Posted 2004) [#15]
I always use different variable names for type in function, so as not to be confused with the ones in the main program. I don't use Global type references at all really :o)


Perturbatio(Posted 2004) [#16]
car is the TYPE of the variable c, t, whatever, the TYPE is global as is the type list, but the variable which refers to that type (in this instance) is local it's just being used to point to each type in the type list.

a quick demo:



Duckstab[o](Posted 2004) [#17]
nice but is a way to get lost with your types

better of checking each eg a.car b.car c.car


Duckstab[o](Posted 2004) [#18]
this can create more code yet save on cpu

only check for relativw states


Perturbatio(Posted 2004) [#19]

nice but is a way to get lost with your types

better of checking each eg a.car b.car c.car



It's not a suggestion of how to handle types, it's a demonstration of LOCAL variables and GLOBAL variables, nothing more than that.

besides which referring to each type by an individual variable is inefficient, since they are all of the same type, looping through them is quicker, more memory efficient and less error prone.

i.e.
for n = 1 to 5
   a.car = new car
   a\x = n*10 ;or whatever
next

for a.car = each car
   print a\x
next


instead of:
a.car = new car
   a\x = 10
b.car = new car
   b\x = 20
c.car = new car
   c\x = 30
d.car = new car
   d\x = 40
e.car = new car
   e\x = 50

print a\x
print b\x
print c\x
print d\x
print e\x



Ross C(Posted 2004) [#20]
To try and give all of your types a Global reference each is a bit unrealistic i think. Sort of defeats the flexiblity of type.