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 !
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 !

Please give an advice in the following case !
Liu Feng
- 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
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
Greg J. Brown