I don't really think there is some method to do this… Anyway… How can I replace one object with another everywhere in the program? It would be like all the references to an old object start to point to a new one.


IF you mean in the code, you can do a find an replace on the objects name. So find objectA Replace with ObjectB

+1  A: 

you cannot easily query the heap with reflection or something else, you could however track all your objects using a list of weak references and find your object that way...

Tim Mahy
Thanks for pointing out to weak references. Looks googlable and so I'm going to learn more.
+1  A: 

What I do is rename the old object then let the compiler's build errors show me everywhere I need to make the change.

I think you meant to say, "what I do is rename the old *class* then..." A class is the definition, an object is the instantiation of it.
Chris Lively
I needed runtime solution. Sorry for not clearly defined question.

It depends upon your IDE. In Eclipse, for example, there is a "Refactor" tab at the top that will let you rename the highlighted class and update all references to it in your package (make sure you check the "update references" tab).

+2  A: 

If this is at runtime, have a look at implementing a proxy or a wrapper class for the instance you may be potentially replacing.

There are other more complex ways of achieving the same effect, this is the simplest.

class Program {
    public void DoReplace() {

        // all instances in the program hold references to the proxy 

        // instantiate the default 
        ReplaceableProxy replaceable = new ReplaceableProxy();

        // some references to the replaceable are aquired

        // later
        replaceable.SetPoxiedClass(new OtherImplementation());

        // now all instances still hold a reference to the same proxy
        //  but the implementation has changed internally since the indirection is encapsulated 
        //  in the proxy class

public interface IReplaceable {
    void DoStuff();

public class ReplaceableProxy : IReplaceable { // this is the proxy

    private IReplaceable _Replaceable = new OriginalImplementation();

    public void DoStuff() {

    internal void SetPoxiedClass(IReplaceable replaceable) {

        _Replaceable = replaceable;

public class OriginalImplementation : IReplaceable {

    public void DoStuff() {

public class OtherImplementation : IReplaceable {
    public void DoStuff() {
Remoting? I mean all those “marshal by refs”. Or are you talking about some architectural decision/pattern?

If the access modifier on the object is set to Friend, Protected Friend or Public and the object reference is not read only (that is, it's mutable), then you can simply re-assign it.

In VB:

Public strSQLConnect as String = "CONNECT....."

'blah... blah... using the variable... hours later


I'm not sure that this is what you want, but maybe it'll help you.


If you want to replace instances of ClassA with instances of ClassB (as in your example above) then go with @MaLio's solution above... otherwise (i.e. if you want to replace all references to Instance1 of ClassA with references to Instance2 of ClassA) then ...

public class Reference<T> {
   private T _instance;

   public Reference( T instance ) {
      _instance = instance;

   public T Instance {
      get { return _instance; }

   public event EventHandler<ReferenceReplacedEventArgs> ReferenceReplaced;

   public void ReplaceWith( T newInstance ) {
      T oldInstance = _instance;
      _instance = newInstance;
      if( ReferenceReplaced != null ) {
         ReferenceReplaced(this, new ReferenceReplacedEventArgs(oldInstance, newInstance));

   public class ReferenceReplacedEventArgs : EventArgs {
      private readonly T _oldInstance;
      private readonly T _newInstance;

      public ReferenceReplacedEventArgs( T oldInstance, T newInstance ) {
         _oldInstance = oldInstance;
         _newInstance = newInstance;

      public T OldInstance {
         get { return _oldInstance; }

      public T NewInstance {
         get { return _newInstance; }