Discussion Locked

Use static members in your C# .NET apps

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

This week's .NET e-newsletter discusses using the static modifier in your C# .NET applications. What are your thoughts on static members? Do you utilize them in your C# .NET apps?

If you aren't subscribed to the .NET TechMail, visit our e-newsletter subcenter to subscribe to this free e-newsletter:
http://nl.com.com/MiniFormHandler?brand=bu ilder&subs_channel=bldr_front_door&list_id=e605& ; tag=fb

* Please delete any extra spaces that appear when you paste this link into your browser.

4 replies

by mamil

In contrast to this article, this feature is definitely available in VB.NET, only there it's called 'Shared' instead of 'Static'.

by alg

Modules in VB.NET are like sealed classes and all the methods in it are considered Shared (static). Additionally, methods described in modules MAY be called without the classname.

Sample to match that in the article but in VB:

Public Class TestClass
Public Shared Sub TestMethod()
Console.WriteLine("Static Test Method")
End Sub
End Class

Public Class SampleTest
Public Shared Sub Main()
TestClass.TestMethod()
Console.ReadLine()

TestMethod2() ' VB.NET doesn't require the module name
Console.ReadLine()

SampleModule.TestMethod2() ' but it does allow it
Console.ReadLine()

End Sub
End Class

' OR

Public Module SampleModule ' equivalent of a sealed class

Public Sub TestMethod2() ' equivalent of a shared method
Console.WriteLine("Static Test Method2")
End Sub
End Module

by Tony Patton

Yes, the article should have noted that the same feature may be used in VB.NET but the syntax is different with the static keyword replaced by the shared keyword.

I apologize for the confusion.

-Tony.

by mohan

Hi, Thank you for explaining "Static" in Classes. Is it advisable to use static methods in my Database Logic layers?

For example, I have classes for Customer, Agent, Purchase Order, Purchase Request and so on.. Can I write static methods to add, delete, query, update etc.. etc... Is it advisable? What are the disadavantages in using them in ASP.NET with post back and all?

Thanks in advance.

Mohan

This discussion is closed to new replies.