Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Class with static members and methods
Message
De
18/01/2005 10:12:35
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00977883
Message ID:
00978085
Vues:
48
Einar,

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

Static properties and methods are handy sometimes. The only "penalty" I can see is that it remains in memory, so going overboard is probably not good ... but then again, only one copy of these static members ever exist, no matter how many instances of the class are created. So, if it's something that gets used from a lot of different places in your app, it's a good candidate for using static members.

Your example is a good. Another example: we make use of statics in several codes datasets that we use throughout our app. Once the dataset is filled, it's members are available throughout the app and are particularly useful for the datasource to ComboBox's. They are Typed DataSets, so we have additional static methods defined in the class.

>2. After I run the Initialize() method how long will the dataset dsOptions be in memory?<

Until your app closes.

>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).<

Well, no, there's no benefit to creating an instance unless you have code that needs initializing. In your case, you created an Initialize method. You could have just as easily put that code in the constructor and instantiated the class only once (like when your app starts). It would have amounted to the same thing.

~~Bonnie



>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
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform