IMBack

Hi,

I got a logic question, want to confirm what i am doing is what I am thinking I am doing.

I created an abstract class that has static methods that talk to the database and return values..etc

Questions:

1. If I have a dataset that is static in an abastract class. Lets say 2 users access the static dataset at the same it in WEB application, and modify it ..or do whatever to it. the last user's data will be saved in the dataset and the first's user will be overritten. Is it corrent Please let me know if I am right or wrong

2. If 2 users access a static method at the same time and do different tasks and return different parameters depending what it does. Is the static method might fail because 2 users access the same static method at the sametime or is it going to act as two independent instances, and will not have a conflic.

or is it going to be like in the first question if it is correct

THank you for reading..



Re: Visual C# General Logic Question - (static/abstract keyword)

sthotakura

You will generally have porblems with static data(sharing data), however you will not have any issues with static methods (sharing the code).

Cheers,

Suresh





Re: Visual C# General Logic Question - (static/abstract keyword)

IMBack

Thanks for the reply,

so when a static method is been executed at the same time by multple users, it will create for example 2 instances on the heap with 2 different (independent) methods.

while static data (dataset) will be initialized on the heap and all users will be pointed to the same instance on the heap (and data will be overriten, by the next user)





Re: Visual C# General Logic Question - (static/abstract keyword)

sthotakura

>>so when a static method is been executed at the same time by multple users, it will create for example 2 instances on the heap with 2 different >>>>>> (independent) methods.

It will not create two methods - but it is a single execution path executed by 2 clients.. - If this static method uses any static data then obviously there will be a problem.





Re: Visual C# General Logic Question - (static/abstract keyword)

IMBack

Thanks it is clear now! Smile

Is it good to use static methods or is it only used for abstract classes.. ( if you could provide an example, that would be aswesome)

THANK YOU





Re: Visual C# General Logic Question - (static/abstract keyword)

MujDBA

IMBack wrote:

Thanks it is clear now!

Is it good to use static methods or is it only used for abstract classes.. ( if you could provide an example, that would be aswesome)

THANK YOU

Years ago, static classes were used to store global state data, but that's not so much the case anymore. Static methods are a great way of encapsulating functionality into classes that you don't necessarily want to instantiate. A good example of this are "helper" classes that just process "stuff".

Let's say you have a class that contains a bunch of methods for retrieving data from the database and filling data structures with that data. You don't necessarily want to instantiate that class every single time you need to access the database. So, you can declare the methods as static to access the functionality without needing to instantiate an actual object.

I'm on a team right now that is putting together one of the most complex projects I've ever worked on, and we used static methods all over the place. If we didn't, we'd have to instantiate too many objects at run time (figuratively speaking).

So in response to your question as to whether or not their use is a good practice, I would say that it simply depends upon your design. Static methods are wonderful tools at our disposal, but as with all tools you need to make sure you're using the right tool for the right job.





Re: Visual C# General Logic Question - (static/abstract keyword)

IMBack

Thank you for the example.

Another question, .. You said ".Static methods are a great way of encapsulating functionality into classes that you don't necessarily want to instantiate.". It makes sence.

The question is than why create none-static methods and write extra code to instantiate an object. why not have all methods as static methods.

I have read examples and i saw more objects been instantiated than used as static. why wouldn't they use static methods and not worry about instantiating. Am I missing something about objects been instantiated than used as static.

Thank you





Re: Visual C# General Logic Question - (static/abstract keyword)

Steve Py

If your methods don't need to access non-static member data then they are best served as static methods. (Even private static methods.) This is an optimization that tools like ReSharper pick up on.

I often create "Static Singleton" classes consisting only of static methods and a private constructor. (To enforce that it cannot be instantiated.)

Where static methods will fail you:
- When you have classes with data that represents the single object instance. Functions that access this data cannot be static. Static data is shared across object instances of the class.

- If you want to take advantage of inheritance. Static methods cannot be inherited. You need to use the proper 'Singleton' pattern (Non-static except for the static 'Instance') to create inherited Singleton classes.







Re: Visual C# General Logic Question - (static/abstract keyword)

Matthew Watson

IMBack wrote:

Thank you for the example.

Another question, .. You said ".Static methods are a great way of encapsulating functionality into classes that you don't necessarily want to instantiate.". It makes sence.

The question is than why create none-static methods and write extra code to instantiate an object. why not have all methods as static methods.

I have read examples and i saw more objects been instantiated than used as static. why wouldn't they use static methods and not worry about instantiating. Am I missing something about objects been instantiated than used as static.

Thank you



Well normally you have some data that you don't want to get changed if someone else calls the same method.

Imagine you had a BankAccount object that held you bank account details.
Then some other code was called for someone else's bank account that did something like:

BankAccount.Debit(10000.0);

That would debit EVERYONE'S account, including yours. Not good!