0

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

Community
  • 1
  • 1
LoneXcoder
  • 2,121
  • 6
  • 38
  • 76
  • 1
    Side note: Mutable `struct` may make you cry. Please read http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil before allowing `set` on members of your `struct`. – Alexei Levenkov Dec 06 '12 at 17:31
  • @AlexeiLevenkov, trying to comprehend what exactly is mutable vs immutable , not realy understanding what's the difference , as building the struct was trial and error than adding `:this()` to it solved the problem for me, so if you could also supply an example to how you will code it propperly i will be glad! – LoneXcoder Dec 06 '12 at 17:54
  • 1
    `public string TableName { get; private set; }` – Alexei Levenkov Dec 06 '12 at 18:02
  • @AlexeiLevenkov trying to access the `TimeMeta.TableID` struct within the `SetTablesMetaDetails()`: Error- "set accessor is inaccessible" (tried it yesterday ..that's why i tried without private modifier, i was asking everyone here no reply) – LoneXcoder Dec 06 '12 at 18:22
  • 1
    `TimeMeta = new DBMetaDetails.DbTable("bla","bla")` does not work for you? – Alexei Levenkov Dec 06 '12 at 18:51
  • @AlexeiLevenkov,nop , as i commented last , `TimeMet.TableID =1` within the scope of `SetTablesMetaDetails()` red squggily line under TimeMeta.TableID saying :`The property or indexer.... cannot be used in this context because the set accessor is inaccessible`, thats as soon as i add private modifier , Any idea ALexei ?? http://stackoverflow.com/questions/2785551/initializing-properties-with-private-sets-in-net/2785576#2785576 have a look at this link for an answer as to proper usage with private – LoneXcoder Dec 06 '12 at 21:53
  • Just assign whole structure in `SetTablesMetaDetails`: `TimesMeta = new DBMetaDetails.DbTable(HTtIDs.TblTimes,...)` instead of `TimesMeta.TableID=HTtIDs.TblTimes;TimesMeta.TableName =...`. – Alexei Levenkov Dec 06 '12 at 21:56
  • @AlexeiLevenkov yea thanks i know i actualy . with your interruption (your first Comment) that helped me to come to a solution , i have searched for the answer because u insist that it should not give error , now **FOR ASSIGNMENT** no matter if you just put it somewhere like where you put all your globals , don't need a class dont need anything no method to initialize it (acutlly if i did put it in the `SetTablesMetaDetails()` scope it will not compile . though now there's no need for `SetTablesMetaDetails()` just put it with all globals , so now you can access it globally for reads only ! – LoneXcoder Dec 06 '12 at 22:13

2 Answers2

4

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.

René Wolferink
  • 3,558
  • 2
  • 29
  • 43
  • could you better explain or give a simple Example , as i find @Jmonsorno Answer to the right direction and your answer does apply to the struct but not addressing how to inintialize public `CustomersTid`(that is the public accessable value) with `CustomersMeta.TableID` struct private value – LoneXcoder Dec 06 '12 at 17:33
  • 1
    i did implement it with your version, a one that i did know about a day before this post, thought i did not know how to avoid the simple problem of the setter bing inaccsessible so i just threw it away (: had not time to deal with it , aventualy i did learn to use it so thanks for your help. – LoneXcoder Dec 07 '12 at 12:56
2

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; } }
Monso
  • 1,086
  • 8
  • 9
  • so now when i try to use /access `currProjData.CustomersTid` as a property, in some method around the application it will not be null or zero cause it is assigned(by `CustomersMeta.TableID`) while it's being called ? – LoneXcoder Dec 06 '12 at 17:41
  • i was asking you in my last comment something about the usage , as i didn't wait for more than 30 minutes to your reply i tried replacing my fields with the properties in your example , so just by replacing those fields with ypur code(propeties ) made the values get to be inside the variables(properties) before i tried it with your solution they were empty/null so your answer does help ! i dont know why though others imply to set the setter with private modifier cause the way suggested other than yours leads to error , so thanks alot , marked as correct one – LoneXcoder Dec 06 '12 at 18:39