Problem
I have to create a website in Classic ASP, and I really want to do a clean design so I will try to put everything in Classes and Models to emulate the ViewModel Pattern.
So can you tell me if this approach of creating objects is good because I will use it everywhere
Class User
Public Name
Public Adress
Public Title
Public Text
Public Rights 'ArrayList of Right objects
Private Sub Class_Initialize()
Initialize
End Sub
Private Sub Class_Terminate()
Dispose
End Sub
Private Sub Initialize()
Name = ""
Adress = ""
Title = ""
Text = ""
Set Rights = Server.CreateObject("System.Collections.ArrayList") ' Arraylist .net
End Sub
Private Sub Dispose()
End Sub
End Class
Class Right
Public Name
Private Sub Class_Initialize()
Initialize
End Sub
Private Sub Class_Terminate()
Dispose
End Sub
Private Sub Initialize()
Name = ""
End Sub
Private Sub Dispose()
End Sub
End Class
And then I do this for instantiating objects :
Dim myUser
Set myUser = New User
'Do some work
Set myUser = nothing 'Dispose the object
Any idea, suggestion or correction is welcome.
Solution
I am not a big VB person, but I would think that if you give your class a Dispose method you should use it rather than setting it to nothing, I know this is kind of the same thing, but if you aren’t going to use the Method don’t create the method, it’s extra stuff. if you are going to use it in the future, comment to that effect.
'This Method is for future development
Private Sub Dispose()
End Sub
OR
'TODO: Implement!
Private Sub Dispose()
End Sub
my comment syntax is probably off a little bit.
That’s very nice & tidy, if you write all your code as cleanly, I want to debug your code!!
That said I might be wrong, but I don’t think there’s a Dispose pattern in VBScript (that’s not .net!), but I see what you’re trying to accomplish and it’s excellent – use that method to set all your resources to Nothing
.
The only thing I’d warmly recommend, is to use the vbNullString
constant instead of ""
– vbNullString
takes 0 bytes of memory, which isn’t the case for ""
.