arraylist - C#/XNA Why can't I add to the Array List? -


i'm having trouble adding objects arraylist. when attempting add keyboardcontroller() , gamepadcontroller() arraylist, i'm told controllerlist field, being used type. both of these classes implement interface icontroller. furthermore, i'm being told both kc() , gc() must have return type. able let me know causing problem. there more proper way of doing this?

// initialization  arraylist controllerlist; controllerlist.add(new keyboardcontroller());  //error controllerlist.add(new gamepadcontroller());   //error  ianimatedsprite mariosprite = new smallmariorunningrightsprite();  protected override void update(gametime gametime) {     foreach(icontroller controller in controllerlist)     {         controller.update();     }      mariosprite.update();      base.update(gametime); } 

this specific section of code provided me instructor , unclear why isn't functioning properly.

you trying execute non-initializer code (the calls arraylist.add) outside of method body. won't work.

you have either use collection initializer syntax

arraylist controllerlist = new arraylist                                {                                    new keyboardcontroller(),                                    new gamepadcontroller()                                }; 

or initialization in constructor of class.

also, don't use arraylist if don't have to. use list<icontroller> instead.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -