آیا بهتر نیست که به جای اینکه از متغیرهای ثابت استفاده کنیم ( که بالاخره اگر تعدادشان در برنامه ، زیاد شود ، حافظه اشغال می کنند ) ، از متد استفاده کنیم؟
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
publicpartialclass_Default : System.Web.UI.Page
{
/* */
constint a1 = 1;
constint a2 = 2;
conststring b1 = "b1";
conststring b2 = "b2";
conststring b3 = "b3";
constbool c1 = true;
constbool c2 = false;
/* */
protectedvoid Page_Load(object sender, EventArgs e)
{
this.ListBox1.Items.Add(a1.ToString());
this.ListBox1.Items.Add(a2.ToString());
this.ListBox1.Items.Add(b1.ToString());
this.ListBox1.Items.Add(b2.ToString());
this.ListBox1.Items.Add(b3.ToString());
this.ListBox1.Items.Add(c1.ToString());
this.ListBox1.Items.Add(c2.ToString());
/**/
this.ListBox1.Items.Add("");
this.ListBox1.Items.Add("------------- ");
this.ListBox1.Items.Add("");
/**/
this.ListBox1.Items.Add(this.get_const_int("a1").ToString());
this.ListBox1.Items.Add(this.get_const_int("a2").ToString());
this.ListBox1.Items.Add(this.get_const_string("b1").ToString());
this.ListBox1.Items.Add(this.get_const_string("b2").ToString());
this.ListBox1.Items.Add(this.get_const_string("b3").ToString());
this.ListBox1.Items.Add(this.get_const_bool("c1").ToString());
this.ListBox1.Items.Add(this.get_const_bool("c2").ToString());
}
/* */
privateint get_const_int(string a_Name)
{
if (a_Name.Equals("a1"))
return 1;
else
return 2;
}
/* */
privatestring get_const_string(string a_Name)
{
if (a_Name.Equals("b1"))
return"b1";
elseif (a_Name.Equals("b2"))
return"b2";
else
return"b3";
}
/* */
privatebool get_const_bool(string a_Name)
{
if (a_Name.Equals("c1"))
returntrue;
else
returnfalse;
}
/* */
}
خود برنامه ی اصلی را ضمیمه کردم.