How to achieve unmodifiable object reference?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • edurazee
    New Member
    • Dec 2009
    • 13

    How to achieve unmodifiable object reference?

    Please consider the following problem.

    Code:
        public class Server
        {
            private string _serverName;
            public string ServerName
            {
                get { return _serverName; }
                //set { _serverName = value; }
            }
    
            private static Server myVar;
            public static Server CurrentServer
            {
                get { return myVar; }
                set { myVar = value; }
            }
    
            private Database _database;
            public Database Database
            {
                get 
                {
                    return _database; 
                }
            }
    
            public Server(string str)
            {
                _serverName = str;
    
                _database = new Database("Northwind");   
            }
        }
    
        public class Database
        {
            private string _databaseName;
            public string DatabaseName
            {
                get { return _databaseName; }
                set { _databaseName = value; }
            }
    
            public Database(string databaseName)
            {
                _databaseName = databaseName;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Server server = new Server("localhost");
    
                Server.CurrentServer = server;
    
                Console.WriteLine(Server.CurrentServer.ServerName);
                Console.WriteLine(Server.CurrentServer.Database.DatabaseName);
    
                Server.CurrentServer.Database.DatabaseName = "None";
    
                //Server server2 = Server.CurrentServer;
                //server2.ServerName = "None";
    
                Console.WriteLine(Server.CurrentServer.ServerName);
                Console.WriteLine(Server.CurrentServer.Database.DatabaseName);
    
                Console.ReadLine();
            }
    In this listings we see that,
    Code:
    Server.CurrentServer.Database
    can not be modified by setting new Database object to it. But surely the value of
    Code:
    Server.CurrentServer.Database.DatabaseName
    can be changed.

    Database-class's DatabaseName property must not be made read-only. Coz, it is essential for the greater picture of the project.

    It just needs to be unmodifiable from
    Code:
    Server.CurrentServer
    .

    How to achieve that?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    To me, this reads like it is right out of a text book.

    Bytes has a policy regarding assisting students with their homework.

    The short version is that the volunteers here can't help you with schoolwork.
    A) We don't know what material you have and have not learned in class.
    B) We don't know the guidelines you must follow.
    C) In the long run giving you the answers actually short changes your education.

    Comment

    • edurazee
      New Member
      • Dec 2009
      • 13

      #3
      I have graduated 2 years back. So I am not a student in any college/school anymore.

      I have just faced this problem while developing a code-generator this morning.

      So a terrible guess by you.

      Comment

      Working...