Please consider the following problem.
In this listings we see that,
can not be modified by setting new Database object to it. But surely the value of
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
.
How to achieve that?
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();
}
Code:
Server.CurrentServer.Database
Code:
Server.CurrentServer.Database.DatabaseName
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?
Comment