"Shared" Methods in asp.net files/classes

  • Thread starter Thread starter Jenna Schmidt
  • Start date Start date
J

Jenna Schmidt

I know that one of the benefits of using "Shared" methods
is you do not explicitly have to Dim as New object to
access the method.

Are there some other implications with memory and
concurrency issues with lets say 25-50 concurrent users?

What are the pros and cons of using this type of method?

Thanks so much,

Jenna Schmidt
 
The pro is that you can share singular instances of objects with multiple
threads/users.
The con is that you can share singular instances of objects with multiple
threads/users.

Shared/Static methods/properties are an easy way to cache data and a good
way to create helper type classes, but you must be careful to write thread
safe code when sharing state data in these methods/properties.
 
Jenna, often choosing shared members is done becuase its nicer to program
against. Consider the Environment class, which only has statics. It has a
lot fo related methods on it, but really, you're not generally going to be
manipulating 10 of those methods at the same time, but instead, you're going
to be using one at a time. In this scenario, its nice to not have to dim up
anew version of the class. The same goes for the Math class, and other
classes also.

Other times, it's just good sense. Consider this code:

Dim dt as DateTime = new DateTime()
dt = dt.Now()

That code really doesn't look that good, since Now() is NOTHING to do with
an instance, but specifically, is related to a concept independent of each
inidividual DateTime. which is why we instead do:

Dim dt as DateTime = DateTime.Now()

The key thing is, use shared members when data or functionality does not
vary on an instance by instance basis.

Regards,
Kit
 
Back
Top