PC Review


Reply
Thread Tools Rate Thread

How can I update multiple object references at once?

 
 
Jocke
Guest
Posts: n/a
 
      21st Sep 2005
Hello

I'm working on an application where some common objects are consumed
thorough the application. The issue is that such a common object - say some
kind of graphics object - shall be replaced by another object now and then,
and then all references should be updated at once. Say I reference the same
graphics object ten different places in the application. How can I create a
new graphics object and then make all of the ten references point to this
new object?

Well, I know I could do this by calling a GetNewestGraphicsObject() method
instead of having references directly to the commonly used obejcts. Then
this intermediate method returns the newest object. But is there a way to
achieve this without using methods?


 
Reply With Quote
 
 
 
 
Joanna Carter \(TeamB\)
Guest
Posts: n/a
 
      21st Sep 2005
"Jocke" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...

> I'm working on an application where some common objects are consumed
> thorough the application. The issue is that such a common object - say

some
> kind of graphics object - shall be replaced by another object now and

then,
> and then all references should be updated at once. Say I reference the

same
> graphics object ten different places in the application. How can I create

a
> new graphics object and then make all of the ten references point to this
> new object?
>
> Well, I know I could do this by calling a GetNewestGraphicsObject() method
> instead of having references directly to the commonly used obejcts. Then
> this intermediate method returns the newest object. But is there a way to
> achieve this without using methods?


Take a look at the Observer pattern. The Subject object can change state and
any other objects that are Observers of that Subject will be notified when
the change occurs. The idea of a multicast delegate as used in .NET events
fulfils this pattern perfectly.

Joanna

--
Joanna Carter
Consultant Software Engineer


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Sep 2005
Jocke <(E-Mail Removed)> wrote:
> I'm working on an application where some common objects are consumed
> thorough the application. The issue is that such a common object - say some
> kind of graphics object - shall be replaced by another object now and then,
> and then all references should be updated at once. Say I reference the same
> graphics object ten different places in the application. How can I create a
> new graphics object and then make all of the ten references point to this
> new object?
>
> Well, I know I could do this by calling a GetNewestGraphicsObject() method
> instead of having references directly to the commonly used obejcts. Then
> this intermediate method returns the newest object. But is there a way to
> achieve this without using methods?


Could you actually have an object with the same interface, which just
proxies to whatever the most current real object is? That would be my
first thought as to the best way round it.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Scott Roberts
Guest
Posts: n/a
 
      21st Sep 2005

"Jocke" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello
>
> I'm working on an application where some common objects are consumed
> thorough the application. The issue is that such a common object - say

some
> kind of graphics object - shall be replaced by another object now and

then,
> and then all references should be updated at once. Say I reference the

same
> graphics object ten different places in the application. How can I create

a
> new graphics object and then make all of the ten references point to this
> new object?
>
> Well, I know I could do this by calling a GetNewestGraphicsObject() method
> instead of having references directly to the commonly used obejcts. Then
> this intermediate method returns the newest object. But is there a way to
> achieve this without using methods?


Does a "property" count as a "method"?

public class CommonGraphicsObject
{
private static GraphicsObject _currentObject = null;

public static GraphicsObject CurrentObject
{
get { return _currentObject; }
set { _currentObject = value; }
}
}

Then you just reference CommonGraphicsObject.CurrentObject everywhere.

Similar to Joanna's suggestion, you could fire an event in the setter if all
references needed to know when a new object was set.

OTOH, this is very, very similar to calling GetNewestGraphicsObject() -
which you wanted to avoid.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple references to only 1 object??? Pieter Microsoft VB .NET 10 16th Oct 2007 02:54 PM
multiple references to only 1 object??? Pieter Microsoft Dot NET Framework 10 16th Oct 2007 02:54 PM
multiple references to only 1 object??? Pieter Microsoft Dot NET Framework Forms 10 16th Oct 2007 02:54 PM
multiple references to only 1 object??? Pieter Microsoft Dot NET 10 16th Oct 2007 02:54 PM
Why Multiple References to the Same Object george r smith Microsoft C# .NET 2 7th Nov 2003 02:34 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:13 AM.