Auto intantiating objects - how bad can they be?
Auto intantiating objects - how bad can they be?
(OP)
MajP posted a link to advise someone to properly format their code and declare vars.
I was perusing the link, http://www.cpearson.com/excel/declaringvariables.a... and found this..
IE don't declare a var as
How true is this? or should I say how bad is using this syntax?
I especially use this when I am attaching a controller type class to a form or a global helper object such as audit trail....
Surely I don't need to change that to...
Do I?
I was perusing the link, http://www.cpearson.com/excel/declaringvariables.a... and found this..
Quote:
Don't Use Auto-Instancing Object Variables
IE don't declare a var as
CODE
Dim myVar as New clsMyClass
How true is this? or should I say how bad is using this syntax?
I especially use this when I am attaching a controller type class to a form or a global helper object such as audit trail....
CODE
Option Compare Database Option Explicit Private oAudit As New clsAuditTrail Private Sub Form_Current() Call oAudit.SetFieldValues(Me) End Sub
Surely I don't need to change that to...
CODE
Option Compare Database Option Explicit Private oAudit As clsAuditTrail Private Sub Form_Open(Cancel As Integer) Set oAudit = New clsAuditTrail End Sub Private Sub Form_Current() Call oAudit.SetFieldValues(Me) End Sub
Do I?
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
RE: Auto intantiating objects - how bad can they be?
As long as you know the drawbacks and are happy to live with them, then - in my opinion - there's nothing wrong with using auto-instancing objects.
RE: Auto intantiating objects - how bad can they be?
I think in my use case shown above it's fine, but I also appreciate where it might be an issue, I never realised that checking it for Nothing would instantiate the object if the var is Nothing so it is never Nothing.
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
RE: Auto intantiating objects - how bad can they be?
I'd be interested in looking at your Audit Trail and related class (modules) and usage if you'd care to share.
RE: Auto intantiating objects - how bad can they be?
clsAuditTrail
CODE
The LogIt sub is a helper method that can also be used for manually creating audit log records, where as the other two methods are used for forms, so you can pass in a form and it will log any changes made by the user.
Then you simply need two events and a global audit trail object on your form...
CODE
As you see it's nothing fancy, but it does the job and keeps track of when users update data on forms very easily.
A couple of things to note, it only tracks "Checkbox", "TextBox", "Combobox", "Memo" controls and they must not be calculated / derived, you can also use the 'Tag' property on a control and set it to 'SKIP', if you want to exclude any controls from the audit trail.
The AuditLog table is very simple...
Hope you find it useful.
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
RE: Auto intantiating objects - how bad can they be?
If you declare and instantiate the variable at the same time that me be OK if you will need it immediately from the module or class that uses it. If not then instantiate right when you need it. leaving things open and hanging around may work, but it is a bad habit to get into if you are not aware of what is going on.
RE: Auto intantiating objects - how bad can they be?
RE: Auto intantiating objects - how bad can they be?
I'm surprised you didn't' pull me up on my class above interacting with the user via msg boxes, as technically it shouldn't, but hey ho, in perfect world and all that!
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music