473,385 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to achieve unmodifiable object reference?

Please consider the following problem.

Expand|Select|Wrap|Line Numbers
  1.     public class Server
  2.     {
  3.         private string _serverName;
  4.         public string ServerName
  5.         {
  6.             get { return _serverName; }
  7.             //set { _serverName = value; }
  8.         }
  9.  
  10.         private static Server myVar;
  11.         public static Server CurrentServer
  12.         {
  13.             get { return myVar; }
  14.             set { myVar = value; }
  15.         }
  16.  
  17.         private Database _database;
  18.         public Database Database
  19.         {
  20.             get 
  21.             {
  22.                 return _database; 
  23.             }
  24.         }
  25.  
  26.         public Server(string str)
  27.         {
  28.             _serverName = str;
  29.  
  30.             _database = new Database("Northwind");   
  31.         }
  32.     }
  33.  
  34.     public class Database
  35.     {
  36.         private string _databaseName;
  37.         public string DatabaseName
  38.         {
  39.             get { return _databaseName; }
  40.             set { _databaseName = value; }
  41.         }
  42.  
  43.         public Database(string databaseName)
  44.         {
  45.             _databaseName = databaseName;
  46.         }
  47.     }
  48.  
  49.     class Program
  50.     {
  51.         static void Main(string[] args)
  52.         {
  53.             Server server = new Server("localhost");
  54.  
  55.             Server.CurrentServer = server;
  56.  
  57.             Console.WriteLine(Server.CurrentServer.ServerName);
  58.             Console.WriteLine(Server.CurrentServer.Database.DatabaseName);
  59.  
  60.             Server.CurrentServer.Database.DatabaseName = "None";
  61.  
  62.             //Server server2 = Server.CurrentServer;
  63.             //server2.ServerName = "None";
  64.  
  65.             Console.WriteLine(Server.CurrentServer.ServerName);
  66.             Console.WriteLine(Server.CurrentServer.Database.DatabaseName);
  67.  
  68.             Console.ReadLine();
  69.         }
  70.  
In this listings we see that,
Expand|Select|Wrap|Line Numbers
  1. Server.CurrentServer.Database
can not be modified by setting new Database object to it. But surely the value of
Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. Server.CurrentServer
.

How to achieve that?
Dec 16 '09 #1
2 1935
tlhintoq
3,525 Expert 2GB
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.
Dec 16 '09 #2
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.
Dec 16 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Chris S. | last post by:
I'm trying to make a graphical editor and browser for Pickled files. One aspect I'm not sure about is how to detect multiple references to the same data. For instance, say I had the Pickled...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
14
by: JKop | last post by:
class Cow { public: void EatGrass() const {} }; class Brow { private:
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
3
by: Jake Barnes | last post by:
37 Signals has built some awesome software with some features I wish I knew how to imitate. When I'm logged into my page (http://lkrubner.backpackit.com/pub/337271) any item that I mouseOver I'm...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.