Subclassed Method Access Issue
Monkey Forums/Monkey Programming/Subclassed Method Access Issue
| ||
I have a class "widget." This has a subclass "menubutton." In the following code, I cannot access a method (settitle) that appears in my subclass. However, if I set my temp variable (thiswidget) to be a subclass object, it is no longer valid because my widgetlist is a list of widgets and not of menubuttons. Method settitle(number:Int, title:String) Local thiswidget:widget = New widget For thiswidget = Eachin widgetlist Local widgetcounter:Int = 1 If widgetcounter = number thiswidget.settitle(title) endif widgetcounter = widgetcounter + 1 Next End method If there is no simple mistake I am making I may just use naked buttons in the place where they are needed and hardcode it instead of making a list, I don't need a framework. |
| ||
See if this helps. I'd change other things, but I've tried to minimise changes from your code. [monkeycode] Method settitle(number:Int, title:String) 'Local thiswidget:widget = New widget 'You don't need to construct a widget instance here 'This was being initialised inside the loop. 'It needs to be initialised outside. 'Also, do you mean to start from 1? That's a habit that will get 'confusing with zero-based arrays Local widgetcounter:Int = 1 For Local thiswidget:widget = Eachin widgetlist If widgetcounter = number 'test if the widget is a button Local mb:menubutton = menubutton(thiswidget) If mb <> Null mb.settitle(title) Else Print("Can't set title. Not a button.") End Return End widgetcounter = widgetcounter + 1 End End [/monkeycode] |
| ||
Thanks for the good help. The reason why I am starting with 1 is because elsewhere I use the list length. I am having adrenal fatigue from too much coffee and long nights and I am probably missing something obvious-- I troubleshot for 8 hours over a one character typo the other day. But my framework is almost finished, and my first game in Monkey. I am probably just going to set it up with naked buttons just to get it working-- there are only three of them. But I will try your code to see if it works. |