Read And Write Properties In ASP.NET

In this article I have covered a few topics like,

  1. Intializing the properties from.

    i). web.config file
    ii).session variables

  2. Readonly properties.
  3. Readonly & Write properties.
In this example we have two properties.
  1. ResultsForPage
  2. PriceMode

[Readonly properties]ResultsForPage - it is a readonly property. This means we can take the value from the property, but we can't re-asign the value to the property, we have intialized this property by using session variable.

[Readonly & Write properties]PriceMode - it is a read and write property. we have intialized this property through web.config file.

web.Config Code...

In order to access the values from web.config file. we should intialize the values to any variable in <appSettings> tag.
  1. <?xml version="1.0"?>  
  2. <!--  
  3.   
  4. For more information on how to configure your ASP.NET application, please visit  
  5.   
  6. http://go.microsoft.com/fwlink/?LinkId=169433  
  7.   
  8. -->  
  9. <configuration>  
  10.     <system.web>  
  11.         <compilation debug="true" targetFramework="4.5" />  
  12.         <httpRuntime targetFramework="4.5" /> </system.web>  
  13.     <appSettings>  
  14.         <add key="Constr" value="server=10101010101;UID=sa;PWD=mypassword;DATABASE=EMPLOYEE;" />  
  15.         <add key="PriceMode" value="INR" />  
  16.         <add key="isValidCustomer" value="false" /> </appSettings>  
  17. </configuration>  

webform.aspx.cs Code...

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. public partial class _Default: System.Web.UI.Page {  
  8.     protected void Page_Load(object sender, EventArgs e) {  
  9.         Session["PageCount"] = 30;  
  10.         int resultcount = StaticData.ResultsForPage;  
  11.         // StaticData.ResultsForPage = 20; ------> Can't be asigned to ResultsPage.  
  12.         // 'ResultsForPage' property is a readonly propery. So we can't assign the value to this property.  
  13.         string price_mode = StaticData.PriceMode;  
  14.         StaticData.PriceMode = "USD"//-----> We can assign the value here.  
  15.     }  
  16. }  

C# Class file Code...

  1. using Microsoft.VisualBasic;  
  2. using System;  
  3. using System.Collections;  
  4. using System.Collections.Generic;  
  5. using System.Data;  
  6. using System.Diagnostics;  
  7. using System.Web.Configuration;  
  8. using System.Web.UI;  
  9. using System.Web;  
  10. public class StaticData {  
  11.     public static string Price_Mode = "";  
  12.     private readonly static int ResultsFor_Page = Convert.ToInt32(HttpContext.Current.Session["PageCount"]);  
  13.     public static int ResultsForPage {  
  14.         get {  
  15.             return ResultsFor_Page;  
  16.         }  
  17.     }  
  18.     // We can't place readonly keyword directly to propertyin C#  
  19.     //public static readonly int ResultsForPage  
  20.     //{  
  21.     // get { return Convert.ToInt32(HttpContext.Current.Session["PageCount"]); }  
  22.     //}  
  23.     private static string Price_mode = System.Web.Configuration.WebConfigurationManager.AppSettings["PriceMode"].ToString();  
  24.     public static string PriceMode {  
  25.         get {  
  26.             return Price_mode;  
  27.         }  
  28.         set {  
  29.             Price_mode = value;  
  30.         }  
  31.     }  
  32. }