Static methods in asp.net
hay,
i understand the problem of the static variable inside a class in the web application,
now i try to create a simple class to deal with SQL Server for example,
so i created Static Method inside this class,
this static method used in insert, update and delete from database,
i wounder if this is safe, and there is no problems will appear ?
Re: Static methods in asp.net
now, if it is not safe, so how could i use JQuery Ajax and the server side,
if the only way to use webMethod is to name it as static :S
Re: Static methods in asp.net
Can you confirm exactly what you mean by "safe"? What context are you referring to?
Gary
Re: Static methods in asp.net
well, when 2 clients called the static method to execute a sql statement @ the same time,
are there any problem @ this time ?
for example client 1 sql statement is " select * from login"
client 2 sql statement is "select * from login where login id = 1"
is there is a chance that client 2 get the result of client 1?
i know i didn't explain it well
i wish you understand me :)
Re: Static methods in asp.net
Hello,
What I think you are referring to is known as concurrency, and you can read more about this here:
http://www.15seconds.com/issue/030604.htm
There is a chance, if you are using shared resources that you can run into issues where two threads are trying to access the same resources. In order to guard against this you can use techniques such as "lock" to block one thread until the other one has completed processing.
Gary
Re: Static methods in asp.net
no that's not what i mean :)
i mean when i use static methods in asp.net, the method work per user or per application "like static variables"
i hope i clear what i mean
Re: Static methods in asp.net
Hello,
No, I am not sure that I understand what you are getting at.
If you declare a method as static, it means that the method refers to a single location and type instead of an instance of that type that was allocated on the managed heap. It has nothing really to do with users.
Gary
Re: Static methods in asp.net
so if i 2 users called this method, there will be no interference of data ?
Re: Static methods in asp.net
No, that is not what I am saying.
A static method does NOT prevent the situation that you are describing.
If you are trying to prevent issues with concurrency, then you need to look into doing it properly.
Gary
Re: Static methods in asp.net
i am so sorry Gary :)
so what is the big use for the Static methods ?
Re: Static methods in asp.net
As I have already mentioned, when you declare a method of a class as Static you can have access to that method without having to create an instance of that class (or more specifically, that type).
I would suggest that you take a look here:
http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx
Gary