繁体   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