Results 1 to 5 of 5

Thread: VB.NET equivalent of a VB6 technique

  1. #1

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    VB.NET equivalent of a VB6 technique

    Hi

    I'm looking for the VB.NET way to segregating my controller code and the form. Let me explain. In VB6 I had something like this, please bear with me.

    VB Code:
    1. ' Inside frmMain
    2. Private pvObjFormController as clsMainFormController
    3.  
    4. Private sub Form_Load()
    5.   set pvObjFormController = new clsMainFormController
    6.   pvObjFormController.init Me
    7. End Sub
    8. Private Sub cmdSave_Click()
    9.     If pvObjFormController.Save Then
    10.         Msgbox "Data was successfully saved"
    11.     Else
    12.         MsgBox "Failed to save data " & pvObjFormController.ErrMsg
    13.     End If
    14. End Sub
    15. private sub Form_Unload()
    16.   set pvObjFormController = nothing
    17. End Sub
    18.  
    19. ' ------
    20. ' Code inside clsMainFormController.cls
    21.  
    22. ' define a class level variable to hold reference to the form
    23. Private pvFrmMain as frmMain
    24. private pvObjCustomer as clsCustomer
    25.  
    26. public ErrMsg as string
    27. Public Sub Init(byval oFrm as frmMain)
    28.    set pvFrmMain = oFrm
    29. End Sub
    30.  
    31. Public Function Save() as Boolean
    32.    Call ControlsToObect
    33.    If pvObjCustomer.Update Then
    34.        Save = True
    35.    Else
    36.        Save = False
    37.         Me.ErrMsg = pvObjCustomer.ErrMsg
    38.    
    39.    End If
    40.  
    41. End Function
    42. private ControlsToObject()
    43.    pvObjCustomer.CustomerID = pvFrmMain.txtCustomerID
    44.    pvObjCustomer.Surname= pvFrmMain.txtSurname
    45.   ' And so on assign control properties to my class object properties
    46.  
    47.  
    48. End Sub
    49. private sub ObjectToControls()
    50.    pvFrmMain.txtCustomerID = pvObjCustomer.CustomerID
    51.    pvFrmMain.txtSurname  = pvObjCustomer.Surname
    52.   ' And so on assign object properties from control properties object properties
    53.  
    54. End Sub
    55. private function DataIsValid() as boolean
    56.  
    57.   ' code to validate controls
    58. End Function

    Now if you're wondering why I use this technique is that we use sourcesafe and while one developer concentrates on the visual interface of the form another can write the necessary validation code and stuff like this. For example in my sample above developer A works on the form, developer B works on clsMainFormController.cls while developer C works in the clsCustomer.cls. Of course developer A will have to advise developer B about any new controls added etc. I also like this technique because if you mess up the form (which happens very often) your validation and save routines are intact.

    Thanks for being patient to read up to here . My question is, how do I apply the same principle in VB.NET? To begin with I won't want to make all my controls as public since you can access controls as frmMain.txtCustomer.text as in VB6. Should I inherit the Main form in a class and apply the necessary validations etc. I would really appreciate if our VB.NET experts in this forum could use the above VB6 code and show some VB.NET samples.

    Thanks a lot
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  2. #2

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: VB.NET equivalent of a VB6 technique

    Hello Hello, I would really appreciate your views.


    Thanks
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: VB.NET equivalent of a VB6 technique

    You shouldn't have to inherit the form to modify controls. Try setting up a form property in your class, then pass ME in as the value.
    VB Code:
    1. ' Inside frmMain
    2. Private pvObjFormController as clsMainFormController
    3.  
    4. Private sub Form_Load()
    5.  
    6. pvObjFormController.FormControl = Me
    7.  
    8. End Sub
    9. Private Sub cmdSave_Click()
    10.     If pvObjFormController.Save Then
    11.         Msgbox "Data was successfully saved"
    12.     Else
    13.         MsgBox "Failed to save data " & pvObjFormController.ErrMsg
    14.     End If
    15. End Sub
    16. private sub Form_Unload()
    17.  
    18. End Sub
    19.  
    20. ' ------
    21. ' Code inside clsMainFormController.cls
    22.  
    23. ' define a class level variable to hold reference to the form
    24. Private pvFrmMain as Form
    25. private pvObjCustomer as clsCustomer
    26.  
    27. public ErrMsg as string
    28. Public Sub Init(byval oFrm as frmMain)
    29.    set pvFrmMain = oFrm
    30. End Sub
    31.  
    32. Public Function Save() as Boolean
    33.    Call ControlsToObect
    34.    If pvObjCustomer.Update Then
    35.        Save = True
    36.    Else
    37.        Save = False
    38.         Me.ErrMsg = pvObjCustomer.ErrMsg
    39.    
    40.    End If
    41.  
    42. End Function
    43. private ControlsToObject()
    44.    pvObjCustomer.CustomerID = pvFrmMain.txtCustomerID
    45.    pvObjCustomer.Surname= pvFrmMain.txtSurname
    46.   ' And so on assign control properties to my class object properties
    47.  
    48.  
    49. End Sub
    50. private sub ObjectToControls()
    51.    pvFrmMain.txtCustomerID = pvObjCustomer.CustomerID
    52.    pvFrmMain.txtSurname  = pvObjCustomer.Surname
    53.   ' And so on assign object properties from control properties object properties
    54.  
    55. End Sub
    56. private function DataIsValid() as boolean
    57.  
    58.   ' code to validate controls
    59. End Function
    60. WriteOnly Property FormControl() as Form
    61. Set(value as Form)
    62.    pvFrmMain = value
    63. End Set
    64. End Property

  4. #4

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: VB.NET equivalent of a VB6 technique

    Thanks wild_bill.

    Would you consider this technique as correct in VB.NET or is there a better way to achieve this.
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: VB.NET equivalent of a VB6 technique

    I can't think of a better way to do it. As far as I know I'm not breaking any standards.

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