简体   繁体   中英

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

i am stuck with a simple variable assignment , the only condintion to make it littl complex for me is that i need the struct values to be private , so they wont be modified alswhwere

and to be able to use values of the struct but in a safe way, i am trying to use public readonly variables . so that's how i can share information with the application in read only mode , shouldn't it be simple ?

what am i missing ?

as the application starts in Page_Load i am calling 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(); 
}
  • The struct , will be used to assign values :
            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; }
                }
            }
  • The current project class to hold values
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;
}

My Problem is that i need those two sets of tables ( Tid & TblName ) details to be exposed to rest of application but as application starts it calls the SetTablesMetaDetails()

and the four last lines does not execute , i've tried moving this section into SetTablesMetaDetails() but it's not the correct syntax , i am geting errors ,

What is the correct way to acomplish assignment of CustomersTid ? (offcorse rest of the 3 as well)

public static readonly int CustomersTid = CustomersMeta.TableID;

this is what i am missing cause i don't know how to get it initialized in same way the struct above does ... preferebly in one method call

If you want to modify them locally, but read them globally, add a modifier to the setter:

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

Could also be internal or protected if you want to.

Use Properties

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; } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM