簡體   English   中英

試圖將類成員公開為只讀

[英]trying to expose a class member to be public and readonly or a public constant

我堅持使用簡單的變量賦值,對我來說使其復雜的唯一條件是我需要將struct值設為私有,因此不會對其進行修改。

為了能夠使用該結構的值,但以一種安全的方式,我正在嘗試使用public readonly變量。 因此,這就是我可以在只讀模式下與應用程序共享信息的方式,這不是很簡單嗎?

我想念什么?

當應用程序在Page_Load啟動時,我正在調用SetTablesMetaDetails()

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
      .... some other App inits here
    }

    else
    {

    }    

    // this method should be the one that instanciates the DbTable struct
   //..thus sets the values of tables "Name" and "ID"
   currProjData.setTablesReferences(); 
}
  • struct ,將用於分配值:
            public class DBMetaDetails
            {
                public struct DbTable
                {
                    public DbTable(string tableName, int tableId): this()
                    {
                        this.TableName = tableName;
                        this.TableID = tableId;
                    }

                    public string TableName { get;  set; }
                    public int TableID { get;  set; }
                }
            }
  • 當前項目類要保存值
public static class currProjData 
{
    static DBMetaDetails.DbTable CustomersMeta = new DBMetaDetails.DbTable();
    static DBMetaDetails.DbTable TimesMeta = new DBMetaDetails.DbTable();

    public static void SetTablesMetaDetails()
    {

        TimesMeta.TableID = HTtIDs.TblTimes;
        TimesMeta.TableName = HTDB_Tables.TblTimes;

        CustomersMeta.TableID = HTtIDs.TblCustomers;
        CustomersMeta.TableName = HTDB_Tables.TblTimeCPAReport;

    }

    public static readonly int CustomersTid = CustomersMeta.TableID;
    public static readonly string CustomersTblName = CustomersMeta.TableName;

    public static readonly int TimesTid = TimesMeta.TableID;
    public static readonly string TimesTblName = TimesMeta.TableName;
}

我的問題是我需要將這兩套表( TidTblName )詳細信息公開給其他應用程序,但是在應用程序啟動時,它將調用SetTablesMetaDetails()

並且最后四行沒有執行,我嘗試將本節移至SetTablesMetaDetails()但是語法不正確,出現錯誤,

完成CustomersTid分配的正確方法是什么? (同時取消其余3個)

public static readonly intCustomersTid = CustomersMeta.TableID;

這就是我所缺少的原因,因為我不知道如何以上面的結構相同的方式對其進行初始化...優選地在一個方法調用中

如果要在本地修改它們,但要全局閱讀它們,請向設置器添加一個修飾符:

public string TableName { get;  private set; }
public int TableID { get;  private set; }

如果需要,也可以是internalprotected

使用屬性

public static int CustomersTid { get { return CustomersMeta.TableID; } }
public static string CustomersTblName { get { return CustomersMeta.TableName; } }

public static int TimesTid  { get { return TimesMeta.TableID; } }
public static string TimesTblName  { get { return TimesMeta.TableName; } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM