Types problem
Blitz3D Forums/Blitz3D Beginners Area/Types problem
| ||
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) |
| ||
in your function it sould be c.car not t.car :) |
| ||
Function findcar.car() For t.car = Each car If t\x = 10 Then Return t.car End If Next End Function |
| ||
Function findcar() For c.car = Each car If c\x = 10 Then Return c.car End If Next End Function |
| ||
you never assigned t. as a type diddnt need to maybe give your types a good name so you remmder then |
| ||
/ |
| ||
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. |
| ||
depend what the rest ov his code was mate |
| ||
Nice one GitTech :o) Thanks! |
| ||
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. |
| ||
car has to be global to work in a function |
| ||
Isnt types always global? |
| ||
if it not global no recognisd outside the main loop |
| ||
Types are always global, but the references to them are not. |
| ||
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) |
| ||
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: |
| ||
nice but is a way to get lost with your types better of checking each eg a.car b.car c.car |
| ||
this can create more code yet save on cpu only check for relativw states |
| ||
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 |
| ||
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. |