Static Methods

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all, I have a Customers class that can be instantiated with a customerId , which causes private member vars to be populated. But I also want to simply make a call to a static method on the Customers class to get shipping addresses, avoiding the overhead of instantiating the class (with it's SQL customer lookup)

Customers.GetShippingAddresses(customerId

here's the signature

internal static DataSet GetShippingAddresses(int customerId

...................


The method is declared static; can I assume that calls to this method will not step on each other

Thanks
 
JM said:
Hi all, I have a Customers class that can be instantiated with a
customerId , which causes private member vars to be populated. But I also
want to simply make a call to a static method on the Customers class to get
shipping addresses, avoiding the overhead of instantiating the class (with
it's SQL customer lookup):
Customers.GetShippingAddresses(customerId)

here's the signature:

internal static DataSet GetShippingAddresses(int customerId)
{
...................
}

The method is declared static; can I assume that calls to this method will not step on each other?

Thanks

What do you mean by "step on eachother"? If you are working in a
single-threaded environment, and your Customers class doesn't maintain any
kind of instanced state, then having a collection of static members will
work just fine.

If your needs are more complex, you should look at the Singleton pattern.
Here's a good article for implementing singleton patterns in C#:

http://www.yoda.arachsys.com/csharp/singleton.html

Erik
 
Thanks for the reply, the class does maintain instance data for clients which instantiate it, but for the static methods I would simply pass an id and get a result. This is an ASP.NET app(sorry I didn't mention), so my question really is can 2 "simultaneous" aspnet_wp calls to this method cause unpredicatable results or are the calls queued?
Thanks again


----- Erik Frey wrote: -----

JM said:
Hi all, I have a Customers class that can be instantiated with a
customerId , which causes private member vars to be populated. But I also
want to simply make a call to a static method on the Customers class to get
shipping addresses, avoiding the overhead of instantiating the class (with
it's SQL customer lookup):
What do you mean by "step on eachother"? If you are working in a
single-threaded environment, and your Customers class doesn't maintain any
kind of instanced state, then having a collection of static members will
work just fine.

If your needs are more complex, you should look at the Singleton pattern.
Here's a good article for implementing singleton patterns in C#:

http://www.yoda.arachsys.com/csharp/singleton.html

Erik
 
JM said:
Thanks for the reply, the class does maintain instance data for clients which instantiate it, but for the static methods I would simply pass an id and get a result. This is an ASP.NET app(sorry I didn't mention), so my question really is can 2 "simultaneous" aspnet_wp calls to this method cause unpredicatable results or are the calls queued?

They are not queued, they are run on multiple threads. If the method
being called does not store/reference any instance data (which it can't
since it's static), and does not store/reference any static data, then
it inherently thread-safe on its own.

Where you will get into trouble is if this method uses resources outside
the method itself, like database connections. In that case, you will
have multiple threads making the same database calls, and if your
database connection is not thread-safe then you could have problems.
 
Back
Top