Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Class with static members and methods
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Class with static members and methods
Divers
Thread ID:
00977883
Message ID:
00977883
Vues:
72
I "borrowed" a class from a CodeProject article related to setting and getting configuration settings. Settings are stored in an XML file and I can set and get name-value pairs (similar to an ini file).
The class is mad up of some static members and static property and some static methods. Something like this:
public class ConfigOpt
{
  private static DataSet dsOptions;
  private static string _ConfigFileName;
  public static string ConfigFileName
  {
    get
      {
  	return _ConfigFileName;
      }
  }

  public static void Initialize(string paraConfigFile)
  {
	_ConfigFileName = paraConfigFile;
	dsOptions = new DataSet("ConfigOpt");

	if (File.Exists(paraConfigFile))
	{
		dsOptions.ReadXml(paraConfigFile);
	}
	else
	{
		DataTable dt = new DataTable("ConfigValues");
		dt.Columns.Add("OptionName", System.Type.GetType("System.String"));
		dt.Columns.Add("OptionValue", System.Type.GetType("System.String"));
		dt.Columns["OptionName"].ColumnMapping = MappingType.Attribute;
		dt.Columns["OptionValue"].ColumnMapping = MappingType.SimpleContent;
		dsOptions.Tables.Add(dt);
	}
  }

  public static void WriteOptions()
  {
	dsOptions.WriteXml(_ConfigFileName, XmlWriteMode.WriteSchema);
  }

  public static string GetOption(string paraOptionName)
  {
	DataView dv = dsOptions.Tables["ConfigValues"].DefaultView;
	dv.RowFilter = "OptionName='" + paraOptionName + "'";

	if (dv.Count > 0)
	{
		return dv[0]["OptionValue"].ToString();
	}
	else
	{
		return "";
	}
  }

  public static void SetOption(string paraOptionName, string paraOptionValue)
  {
	DataView dv = dsOptions.Tables["ConfigValues"].DefaultView;
	dv.RowFilter = "OptionName='" + paraOptionName + "'";
			
	if (dv.Count > 0)
	{
		dv[0]["OptionValue"] = paraOptionValue;
	}
	else
	{
		DataRow dr = dsOptions.Tables["ConfigValues"].NewRow();
		dr["OptionName"] = paraOptionName;
		dr["OptionValue"] = paraOptionValue;
		dsOptions.Tables["ConfigValues"].Rows.Add(dr);
	}
  }
}
I have removed some of the overloads I created. This class works really well, but I am currious to find out more about how a class that has only static members works. Here are some of the questions I have:
1. Is there a penalty to create a class like this compared to creating a class with non-static members where I have to create an instance of the class in order to use it?
2. After I run the Initialize() method how long will the dataset dsOptions be in memory?
3. I added a constructor, with a messagebox telling me that I was in the constructor, to the class. I thought the constructor would be executed the first time a static method in the class was executed, but the constructor was never executed unless I created an instance of the class (but there was no benefit to creating an instance).

Any comments or thoughts about classes with only static members are welcome.

Thanks,
Einar
Semper ubi sub ubi.
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform