6 Replies - 431 Views - Last Post: 08 March 2012 - 07:15 AM Rate Topic: -----

#1 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Populating List<> from Class Instance

Posted 07 March 2012 - 05:15 PM

Please tell me what I am doing wrong with the following code:


public class MainClass
{
     CreateButton createButtonInstance = new CreateButton();

  protected override void Initialize()
  {
     createButtonInstance = new CreateButton("btnEasy", 1, 60, 45, 1, x, y, Color.White, IEnums.myButtonState.Up, 0.0);
     createButtonInstance = new CreateButton("btnMedium", 1, 92, 45, 1, x, y, Color.White, IEnums.myButtonState.Up, 0.0);
     createButtonInstance = new CreateButton("btnHard", 1, 60, 45, 1, x, y, Color.White, IEnums.myButtonState.Up, 0.0);
  }
}

public class CreateButton
{
  private MyButton myButton;
  private List<MyButton> buttonList;

     public CreateButton()
     {
         myButton = null;
         buttonList = null;
     }

     public CreateButton(string name, int menu, int width, int height, int floor, int x, int y, Color colour, IEnums.myButtonState btnState, double timer)
     {
         myButton = new MyButton(name, menu, width, height, floor, x, y, colour, btnState, timer);

         //the problem is here: buttonList returns to null each time CreateButton is called
         if (buttonList == null)
             buttonList = new List<MyButton>();

         buttonList.Add(myButton);
     }
}



The problem is where I have placed the comment. I think that with

createButtonInstance = new CreateButton()


I am creating a new instance each time, thus buttonList is returning to null.

How can I use the same instance of class CreateButton and hence populate buttonList List<>?

Is This A Good Question/Topic? 0
  • +

Replies To: Populating List<> from Class Instance

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Populating List<> from Class Instance

Posted 07 March 2012 - 05:28 PM

The problem you are having is that buttonList is an instance variable. When you create a new button, it's gets its own buttonList. Put static in front of the buttonList declaration and you'll only have one that is shared by all instances.
Was This Post Helpful? 1
  • +
  • -

#3 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: Populating List<> from Class Instance

Posted 07 March 2012 - 05:39 PM

Thanks! :) that does the trick!

Unfortunately, I don't understand exactly what is happening though. I usually use static to call a method/variable without having class instance. Is what you suggested somehow the same?
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: Populating List<> from Class Instance

Posted 07 March 2012 - 10:04 PM

Every time you create a "CreateButton" class, it gets its own List. And that's a problem, since you just want one list shared between them. I probably wouldn't go about making this list static though. I'd change my organization entirely. Classes should be nouns, not verbs. Methods are verbs. Classes describe things, not actions.

You could make this whole CreateButton class irrelevant by adding a static method to your MyButton class that returns a MyButton. Then you can maintain the list in your MainClass, and do something like list.Add(MyButton.Create(...)) to handle it.
Was This Post Helpful? 1
  • +
  • -

#5 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: Populating List<> from Class Instance

Posted 08 March 2012 - 05:04 AM

Thanks for your thoughts Curtis Rutland.

I thought of populating the list immediately so that I don't have list.Add(MyButton.Create(..)) for each button I created. Thus making it kind of a short cut.

Do you think this is not good programming practice?
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,972
  • Joined: 08-June 10

Re: Populating List<> from Class Instance

Posted 08 March 2012 - 07:07 AM

Well, I can't say that's bad or good in and of itself. The problem is organization. If you want a method that creates and adds, you need it to be part of the same class that stores the list as well. In this case, that's not working, because you're creating a new list for each one. But I can't architect the thing for you, so you'll have to decide how to arrange it.
Was This Post Helpful? 1
  • +
  • -

#7 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: Populating List<> from Class Instance

Posted 08 March 2012 - 07:15 AM

I appreciate your tips and suggestions. Thanks for sharing!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1