CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Posts
    58

    Where should my collection of data objects be instantiated?

    Philosophical question here. When I write windows form applications, I try very hard to keep UI and data structures separate. But I wonder if I am doing it the best way, OO-wise.

    For instance, if I have MyClass, and my application requires many of them, perhaps stored in a List, should I make that List<MyCLass> a member of the Form1 (with Form1 being the "main" form)? If not, where should I instantiate the List? Any opinion on public vs. private declaration, or is it just a matter of whatever is needed?

    Code:
    public partial class Form1 : Form 
    {
      private List<MyClass> myClassList; // good idea? Bad idea?
    
      public Form1 ()
      {
        InitializeComponent();
      }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Where should my collection of data objects be instantiated?

    It really depends on how complicated the app is. Is it a single form app with just one list of data? If so, having a private member list is perfectly fine.

    However, do you plan to grow your application with more data and more forms? If so, then I would separate the data and the view. For a form application, you can store the data in a class and then pass a reference to the class to each form.

    Try to keep that separation of data where by each form only acts on the data (and has no knowledge of any other form). Many beginning developers miss this point and have a form that attempts to change data on another form. This might seem like a good idea, but it's difficult to maintain - all of a sudden, you are trying to change data on the parent of a parent. This approach can quickly get out of hand. If you stick to having a form only ever change the data, then you eliminate this problem.

    An improvement to this idea is rather than passing a reference to each form, make the data class a singleton so it can be accessed by any form (without having to pass in a reference to the form).

    Even better is to switch to an MVVM pattern. This pattern is used in many other types of UI programming and learning it for Windows Forms will help you to transition to other technologies later.

    Search bing or google for "mvvm windows forms" for several examples.

  3. #3
    Join Date
    Apr 2012
    Posts
    43

    Re: Where should my collection of data objects be instantiated?

    If the list is tied closely to the instance of the Form then it should definitely be a field of the Form class. However, if the list is part of some global state then I would opt to use a property of a static class that is globally available.

  4. #4
    Join Date
    Aug 2013
    Posts
    2

    Re: Where should my collection of data objects be instantiated?

    Depends. If I am using a form to populate a single instance of the Object, I will create that instance within the form. However I usually build a public static dataHandler object that holds all of my List<myObject> or any other application wide data.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured