This seems like it might be a bad idea, but I can’t figure out why:
I have a class, cXYZ, with properties A, B and C. It also has a method ‘sGetData’ that loads those three properties from the database, and a method ‘sSaveData’ which saves it back.
class cXYZ public property A as string... public property B as string... public property B as string.. public sub sGetData()... public sub sSaveData()... end class
A webform has the following property:
private property xyz() as cXYZ get return session('myXYZ') end get set (value as cXYZ) session('myXYZ')=value end set end property
And the following events:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load if not ispostback() then xyz=new cXYZ() end if end sub Protected Sub ButtonLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLoad.Click //Can now reference the class txtA.text=xyz.A txtB.text=xyz.B txtC.text=xyz.C end sub Protected Sub ButtonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonSave.Click //Can now reference the class xyz.A=txtA.text xyz.B=txtA.text xyz.C=txtC.text xyz.sSaveData() end sub
I can see some overhead with serializing/deserializing for each property reference- it might be worth doing this:
Protected Sub ButtonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonSave.Click dim localxyz as cXYZ=xyz localxyz .A=txtA.text localxyz .B=txtA.text localxyz .C=txtC.text xyz=localxyz end sub
Other than that, views on why this is good or bad? The class is not large, it maintains the form state. Webforms suck, etc is not very useful..
I would improve that little bit: