473,750 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on use of Static Methods

In another post Kevin Spencer stated: "one should be careful of using static
fields, properties, and methods, by understanding what the implications of
such are (e.g. locking static variables when changing because they are not
thread-safe)."

Can someone please elaborate on the pros and cons of using static methods?
(In VB they are called Shared methods.) The MS DAAB uses Shared methods.
This means that all Data Access calls go through this block of code without
instantiating an instance. I assumed that this would be a highly scalable
solution. Is this assumption correct? Or are there drawbacks to using shared
methods that reduce the scalability? Do multiple users ever "step on each
other"? For example, can UserA end up with UserB's Datareader?

Appreciate any advice.
--
Joe Fallon

Nov 18 '05 #1
4 2049
"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:u3******** *****@TK2MSFTNG P10.phx.gbl...
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of
such are (e.g. locking static variables when changing because they are not
thread-safe)."

Can someone please elaborate on the pros and cons of using static methods?
(In VB they are called Shared methods.) The MS DAAB uses Shared methods.
This means that all Data Access calls go through this block of code without instantiating an instance. I assumed that this would be a highly scalable
solution. Is this assumption correct? Or are there drawbacks to using shared methods that reduce the scalability? Do multiple users ever "step on each
other"? For example, can UserA end up with UserB's Datareader?


Shared fields or properties are shared by all users. That's the big problem.

I don't know of a problem with accessing Shared methods which do not refer
to Shared data.
--
John Saunders
johnwsaundersii i at hotmail
Nov 18 '05 #2
Generally static methods should never be a problem. Statics are nice
precisely because you don't need and instance ref first, although I suspect
that the overhead of creating an instance would be minimal in the first
place. More than anything it's about the convenience of having those methods
available directly without setting up an instance. If your methods don't
require class level data and they're self contained they're good canidates
for static methods.

it's static data you have to be potentially aware of for potential
multi-threading lock issues as any data that is static is shared by the
entire ASP.Net application and its AppDomain. I use static data quite a bit
in my apps for storing configuration data and a number of one time loaded
references to reused data of all sorts. If data is read only or only updated
under very controlled circumstances statics can be very efficient as it's
simply a direct reference. With read-only scenarios there's little to worry
about although you still have to be aware that some objects of the framework
itself may use internal data to manage their state which can cause odd
behavior when multiple threads access these objects. Remember that ASP.Net
handles each incoming request on a separate thread and each of those threads
shares the same property data.
+++ Rick ----

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
----------------------------------
Making waves on the Web
"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:u3******** *****@TK2MSFTNG P10.phx.gbl...
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of
such are (e.g. locking static variables when changing because they are not
thread-safe)."

Can someone please elaborate on the pros and cons of using static methods?
(In VB they are called Shared methods.) The MS DAAB uses Shared methods.
This means that all Data Access calls go through this block of code without instantiating an instance. I assumed that this would be a highly scalable
solution. Is this assumption correct? Or are there drawbacks to using shared methods that reduce the scalability? Do multiple users ever "step on each
other"? For example, can UserA end up with UserB's Datareader?

Appreciate any advice.
--
Joe Fallon


Nov 18 '05 #3
Generally static methods should never be a problem. Statics are nice
precisely because you don't need and instance ref first, although I suspect
that the overhead of creating an instance would be minimal in the first
place. More than anything it's about the convenience of having those methods
available directly without setting up an instance. If your methods don't
require class level data and they're self contained they're good canidates
for static methods.

it's static data you have to be potentially aware of for potential
multi-threading lock issues as any data that is static is shared by the
entire ASP.Net application and its AppDomain. I use static data quite a bit
in my apps for storing configuration data and a number of one time loaded
references to reused data of all sorts. If data is read only or only updated
under very controlled circumstances statics can be very efficient as it's
simply a direct reference. With read-only scenarios there's little to worry
about although you still have to be aware that some objects of the framework
itself may use internal data to manage their state which can cause odd
behavior when multiple threads access these objects. Remember that ASP.Net
handles each incoming request on a separate thread and each of those threads
shares the same property data.
+++ Rick ----

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
----------------------------------
Making waves on the Web
"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:u3******** *****@TK2MSFTNG P10.phx.gbl...
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of
such are (e.g. locking static variables when changing because they are not
thread-safe)."

Can someone please elaborate on the pros and cons of using static methods?
(In VB they are called Shared methods.) The MS DAAB uses Shared methods.
This means that all Data Access calls go through this block of code without instantiating an instance. I assumed that this would be a highly scalable
solution. Is this assumption correct? Or are there drawbacks to using shared methods that reduce the scalability? Do multiple users ever "step on each
other"? For example, can UserA end up with UserB's Datareader?

Appreciate any advice.
--
Joe Fallon


Nov 18 '05 #4
Rick,
Thanks for the comments.
Just what I was hoping to hear.
--
Joe Fallon

"Rick Strahl [MVP]" <ri********@hot mail.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Generally static methods should never be a problem. Statics are nice
precisely because you don't need and instance ref first, although I suspect that the overhead of creating an instance would be minimal in the first
place. More than anything it's about the convenience of having those methods available directly without setting up an instance. If your methods don't
require class level data and they're self contained they're good canidates
for static methods.

it's static data you have to be potentially aware of for potential
multi-threading lock issues as any data that is static is shared by the
entire ASP.Net application and its AppDomain. I use static data quite a bit in my apps for storing configuration data and a number of one time loaded
references to reused data of all sorts. If data is read only or only updated under very controlled circumstances statics can be very efficient as it's
simply a direct reference. With read-only scenarios there's little to worry about although you still have to be aware that some objects of the framework itself may use internal data to manage their state which can cause odd
behavior when multiple threads access these objects. Remember that ASP.Net
handles each incoming request on a separate thread and each of those threads shares the same property data.
+++ Rick ----

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
----------------------------------
Making waves on the Web
"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:u3******** *****@TK2MSFTNG P10.phx.gbl...
In another post Kevin Spencer stated: "one should be careful of using

static
fields, properties, and methods, by understanding what the implications of such are (e.g. locking static variables when changing because they are not thread-safe)."

Can someone please elaborate on the pros and cons of using static methods? (In VB they are called Shared methods.) The MS DAAB uses Shared methods.
This means that all Data Access calls go through this block of code

without
instantiating an instance. I assumed that this would be a highly scalable solution. Is this assumption correct? Or are there drawbacks to using

shared
methods that reduce the scalability? Do multiple users ever "step on each other"? For example, can UserA end up with UserB's Datareader?

Appreciate any advice.
--
Joe Fallon



Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2168
by: Tom Dacon | last post by:
I'm curious to see if anyone has an opinion on this little design question - I'm doing a computational astronomy library in C#, purely for my own use, and one of the things that happens regularly is conversion of coordinates from one frame of reference to another: spherical coordinates in the ecliptic frame of reference, spherical coordinates in the equatorial frame of reference, 3D rectangular coordinates in either, galactic coordinates,...
5
3630
by: allison | last post by:
Can someone point me to a guide on when to use static methods versus instance methods in a class? For instance, is it bad design to have a static method that creates an instance of another class? I am looking for a good explanation. Thanks in advance
3
1426
by: Amadelle | last post by:
Hi All and thanks in advance, I wanted to know when is a good idea to use a static class (with static constructor) and when to use instance classes? I have read couple of articles on line and the general example is when you want to write a log file use static class ...but they don't say why? My specific question is that I have a validation class with some data validation methods which will be called through out the program and I am...
44
4224
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
11
1606
by: dhnriverside | last post by:
Hi peeps Ok, so I thought I'd have a go at making a console app in VS2k5... I haven't written any windows apps for years, let alone dos apps (been web programming) and I've hit a dumb error... "An object reference is required for the nonstatic field, method or property " This is occuring in my main function...
6
1458
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and interfere with one another. My question is, for each static method, do I need to lock access to only one call at a time? I've noticed that Microsofts Data Application block also uses static methods for its data access calls - for example execute...
5
3195
by: wrecker | last post by:
Hi all, I have a few common methods that I need to use at different points in my web application. I'm wondering where the best place would be to put these? I think that I have three options. 1. I can create a common module like common.vb in my project and put all the functions in there. 2. Create a utility class and create the common functions as shared
15
2159
by: dn | last post by:
I'm starting an n-tier application with an ASP.NET 2.0 presentation layer, a business layer, a data access layer, and a SQL Server 2005 database, and I have a question. In the business and data access layers, should I use static methods? Or, should I code the classes so that they have to be instantiated? I've seen examples of both, and I'm not sure which is the "preferred" or "best" way. Thanks.
18
1928
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two requests at the same time. The first one stops execution as the second continues. If I place either an alert between the two requests or run the second through a setTimeout of only 1 millisecond, they both work. You can see a working example here:...
0
8999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4712
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.