Object does not exist!
Blitz3D Forums/Blitz3D Programming/Object does not exist!
| ||
Obviously I'm doing something wrong, but in my mind all logic is correct. [bbcode] Type Cars Field Id% End Type ;*********************************** Local Car1.Cars = New Cars For Car1.Cars = Each Cars Car1.Cars\Id% = 2000 Next Print (Car1.Cars\Id%) ; Here!!Object does not exist WaitKey() [/bbcode] Last edited 2013 Last edited 2013 Last edited 2013 |
| ||
A loop variable is incremented at the end of the loop. If it is still in range then the body of the loop is executed again.For n = 1 To 5 Print n Next Print Print n WaitKey The same thing happens in your example. Car1 is "incremented" until it runs off the end of the list. Then the loop is exited with Car1 = Null. |
| ||
In other words, the loop ended because Car1 no longer pointed to a Cars instance. That's how those loops know to end. Car1 went off the end of the list, so to speak. The solution is to point Car1 to the Cars instance you want it to point to. If you want it to be the last Cars instance then just: Car1.Cars=last Cars Print (Car1.Cars\ID% should do it (I think). |
| ||
; Hi Yue, you might have been trying to do something like this: ; Edited FOR THE LAST TIME! |
| ||
In other words, you cannot just get the 'nth' instance of a type, the way you can with an array. This is because the system doesn't keep track of that info, because it doesn't know how many there will be. Flexibility in scale means it's harder to get to a specific one by number. The solutions are either to use an array as Virtland suggests above (if you know how many there will be), or write a function that iterates through each time you need to find one based on the number. Below I've thrown together an example where the GetCars(n) function will return the car the way an array would (based on 0, not 1). If there is no Cars instance at that 'index' (because there aren't enough of them), it returns null, which you should handle in your code (see the example). Local myCar.Cars,i ;Set up the Cars For i=0 To 9 myCar.Cars=New Cars myCar\Id=i+1 Next ;Iterate through them, going one too far For i=0 To 10 myCar.Cars=GetCar(i) If myCar<>Null Print "Car #"+i+", ID="+myCar\Id Else Print "There is no Car #"+i EndIf Next WaitKey() Type Cars Field Id% End Type Function GetCar.Cars(n) Local myCar.Cars Local index=0 For myCar=Each Cars index=index+1 Next If n>index-1 Or n<0 Return myCar.Cars ; Which is intentionally null Else index=0 myCar.Cars=First Cars While index<>n index=index+1 myCar.Cars=After myCar Wend EndIf Return myCar.Cars End Function |
| ||
;I found Axel Wheeler's code inspiring. ![]() ; Here's another way to do it without storing cars in arrays: ; I tried to make it as compact as possible ; using Handle and Object commands. ; ; for reference look here to learn about Handle & Object.###( ): ; http://blitzbasic.com/Community/posts.php?topic=53348 ; http://www.blitzbasic.com/Community/posts.php?topic=75556 ; Note that with the Handle command, that the Handle is immediately created ;even if your code does not store the Handle anywhere, ; as in the following unusual code examples: Print" Handle of first car = "+ Handle(First car) cp.car = After Object.car(Handle(First car)) ; cp now points to 2nd car h% = Handle(After Object.car(Handle(First car))) ;h is now handle of 2nd car temp = handle(new car) ;; creates a new car instance and its handle too. ; (When not used with the Print command the Handle(..) result must ; be sent to some variable.) |
| ||
oO? I always think that I will never stop learning and never conquer the demon of ignorance that led to inside. ![]() |