Execute Command Question

  • Thread starter Thread starter Rob Panosh
  • Start date Start date
R

Rob Panosh

Hello,

I have a sub, ExecuteCommand(), that I pass Insert, Update, and Delete
commands to, Inside this sub I create a Command and DataAdapter object that
executes the incoming command. The question I have is should I create these
objects as Local DIM's everytime the SUB is called or should I create them
as Private instance variables of the class and reuse them?

Thanks,
Rob Panosh
Advanced Software Designs.
 
It depends...if the class as a whole might have other methods that could use
these, then you may want to make them properties. If each time, only this
method needs them, then you make want to amke the function Shared (VB.NET)
or Static (C#) so that you don't have to instantiate an seperate instance of
the class and just declare them locally.

Once again, it really depends on your overall design, but if this is a
utility library, it may lend itself well to being a shared/static member.
Also, even if you do go with the shared thing, you can still declare them as
Shared/Static members and reference them from your method.

HTH,

Bill
 
Bill,

Thanks for the timely reponse. I currently have them defined as Private
Dim's to the class, if they are nothing then I create them otherwise I just
reuse the adapter and command. I am assuming that I should get better
performance this way because less command/adapter objects are created thus
less garbage collection. Is that a correct assumption?

Thanks,
Rob
 
the short answer is probably. There's no way to know how much garbage
collection is going to occur so technically speaking, that's not why it'd be
better. However, the fewer objects that are created, in general, the less
you would expect garbage collection, Instantiating objects has some
overhead while declarations don't have much at all. Where you can get into
real troulbe is when you are passing reference types all over the place....

With all that said, going for an approach where you have one adapter or a
few of them and you reuse them will probably yeild the best performance
 
Back
Top