Please give an advice in the following case !

I usually write a module class in my windows apps like this :
Public class Module
{
public static OleDbConnection cnn=new OleDbConnection(ConfigurationSettings.AppSettings["connectionString"]);
public static getData(string sql)
{
OleDbDataAdapter da=new OleDbDataAdapter(sql,cnn);
DataSet ds=new DataSet();
try
{
da.Fill(ds);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
da.Dispose();
}
return ds.Tables[0];
}
}
But some people use the other way :
They just create a Public Module class and no function is static.
I'd like to know the comparision between 2 above ways.
Should I use many static functions for my App or not.
Please give me an explanation and advice !

Thanks a lot !



Answer this question

Please give an advice in the following case !

  • Liu Feng

    You should use static classes when the subject of your coding requires a singleton or if you want to group together a bunch of helper functions.

    - Let's say that you want to write a class for managing config. Should be static or not depends on the target of the class and how it will be consumed: if you write a generic accessor for reading from an XML file and this may be reused clearly you won't create it as static, if you write a module that holds some cfg data and other external modules need access to that data it's a good choice to avoid those modules any initialization or creation code, so in this case it makes sense to use a static singleton where you have the possiblity to have an init section (constructor) and a finalization section (IDisposable pattern)

    - A great use of the static classes is also as a container for helper methods, this short excerpt should explain much metter tah a lot of words:

    public static class StringUtils {
    public static string PadLeft{...}
    public static string PadRight{...}
    ...
    }
    public static class XmlUtils {
    public static string InsertElement{...}
    public static string InsertTerminalElement{...}
    ...
    }


  • Maheep

    I think static funtion is not popular in C#. 

    static function may save your RAM much because of no creating a new object and it runs imediately when your program starts. YourClassname.yourstatic

    to understand what is needed for static and what is needed for non static , you can take a look around C# Class. the classes like Color, string are often in static ...

    hope that it helps you something  



  • magcianaux

    Hi pvcphuc,

    Please mark the post(s) you felt helped you the most as answer, so when others search on the topic, they will know that these posts were helpful. Thanks.


  • jwraith

    Thanks for your all reply much !


  • Greg J. Brown

    IMHOThe other posts answered the question concering the nature of using statics, but I am going to ask you to think about the future of the code, which I believe is of greater importance.

    Can one maintain or expand a design that will be implented Is it extensible for future coding enhancements

    Check out this forum post on Architecting a DAL (Data Access Layer) read my post on creating an extensible and maintainable DAL layer...

    The point I am going after is that if your design, whether it uses statics or not, is not the problem...both are good methods...but is the design buildable, maintainable and extensible in the future

    I find that designs that are easy to debug and unit test out and allow for future expansion are the best. It takes more time and code, but it pays off when one revistis the code in the future.



  • Please give an advice in the following case !