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:
' Inside frmMain
Private pvObjFormController as clsMainFormController
Private sub Form_Load()
set pvObjFormController = new clsMainFormController
pvObjFormController.init Me
End Sub
Private Sub cmdSave_Click()
If pvObjFormController.Save Then
Msgbox "Data was successfully saved"
Else
MsgBox "Failed to save data " & pvObjFormController.ErrMsg
End If
End Sub
private sub Form_Unload()
set pvObjFormController = nothing
End Sub
' ------
' Code inside clsMainFormController.cls
' define a class level variable to hold reference to the form
Private pvFrmMain as frmMain
private pvObjCustomer as clsCustomer
public ErrMsg as string
Public Sub Init(byval oFrm as frmMain)
set pvFrmMain = oFrm
End Sub
Public Function Save() as Boolean
Call ControlsToObect
If pvObjCustomer.Update Then
Save = True
Else
Save = False
Me.ErrMsg = pvObjCustomer.ErrMsg
End If
End Function
private ControlsToObject()
pvObjCustomer.CustomerID = pvFrmMain.txtCustomerID
pvObjCustomer.Surname= pvFrmMain.txtSurname
' And so on assign control properties to my class object properties
End Sub
private sub ObjectToControls()
pvFrmMain.txtCustomerID = pvObjCustomer.CustomerID
pvFrmMain.txtSurname = pvObjCustomer.Surname
' And so on assign object properties from control properties object properties
End Sub
private function DataIsValid() as boolean
' code to validate controls
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
Re: VB.NET equivalent of a VB6 technique
Hello Hello, I would really appreciate your views.
Thanks
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:
' Inside frmMain
Private pvObjFormController as clsMainFormController
Private sub Form_Load()
pvObjFormController.FormControl = Me
End Sub
Private Sub cmdSave_Click()
If pvObjFormController.Save Then
Msgbox "Data was successfully saved"
Else
MsgBox "Failed to save data " & pvObjFormController.ErrMsg
End If
End Sub
private sub Form_Unload()
End Sub
' ------
' Code inside clsMainFormController.cls
' define a class level variable to hold reference to the form
Private pvFrmMain as Form
private pvObjCustomer as clsCustomer
public ErrMsg as string
Public Sub Init(byval oFrm as frmMain)
set pvFrmMain = oFrm
End Sub
Public Function Save() as Boolean
Call ControlsToObect
If pvObjCustomer.Update Then
Save = True
Else
Save = False
Me.ErrMsg = pvObjCustomer.ErrMsg
End If
End Function
private ControlsToObject()
pvObjCustomer.CustomerID = pvFrmMain.txtCustomerID
pvObjCustomer.Surname= pvFrmMain.txtSurname
' And so on assign control properties to my class object properties
End Sub
private sub ObjectToControls()
pvFrmMain.txtCustomerID = pvObjCustomer.CustomerID
pvFrmMain.txtSurname = pvObjCustomer.Surname
' And so on assign object properties from control properties object properties
End Sub
private function DataIsValid() as boolean
' code to validate controls
End Function
WriteOnly Property FormControl() as Form
Set(value as Form)
pvFrmMain = value
End Set
End Property
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.
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.