CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Encapsulation

  1. #1
    Join Date
    Sep 2000
    Posts
    200

    Encapsulation

    Is there any down-side to making a form (used to enter data) a member of a user created class module?

    For example, the class module maintains a collection (dictionary) and has add, edit, & delete methods. It seems as though more data could remain private if I can copy the input data directly from the form (a member) to the collection. Am I thinking too much?

    Starting to get confused, . . . or would it make more sense to make an object of my class a member of the form? . . .

    John


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Encapsulation

    I think the best thing to do is to add the class to the form rather than adding the form to the class. Of course, you can always pass the form to the class when calling a function or something. Of course, you could add the form to the class, but I think this will only make things more complicated than needed. Also, if someone has to continue working on your code, this may become a great source of confusion.
    I'm giving some examples with the add method

    ' form isn't passed, values are
    ' on the form
    private Sub CmdAdd_Click()
    ' assuming that the add method takes two parameters
    objDic.Add text1.text, text2.text
    End Sub
    ' in the class
    public Sub Add(strWord as string, strMeaning as string)
    ' do something, like adding to a local collection
    colDic.Add strMeaning, strWord
    End Sub

    ' form is passed
    ' on the form
    private Sub CmdAdd_Click()
    ' assuming that the add method takes two parameters
    objDic.Add me
    End Sub
    ' in the class
    public Sub Add(frm as form)
    ' do something, like adding to a local collection
    colDic.Add frm.text2.text, frm.text1.text
    ' note that we refferenced the form throught he frm object
    End Sub



    I would advice to use the first one, because of it's independancy, the second requires a form to be called, which contains two textboxes with a specific name. The first just takes strings (in this case), and can easely be called from different places, like from a dll or even an asp page.

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

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