Click to See Complete Forum and Search --> : Encapsulation


John Reynolds
June 26th, 2001, 03:16 PM
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

Cakkie
June 27th, 2001, 01:22 AM
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
slisse@planetinternet.be

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