user2602079
user2602079

Reputation: 1403

Creating an instance without endless instantiation

The line: mdv = new MapDesignerView();creates an endless instantiation (infinite loop). See in code below:

public partial class MapDesignerView : Form
{
    public  MapDesignerView mdv;
    public  Map map;
    public  MapController mapController;
    public MapConstructor mapConstructor;
    MouseEventHandler detectMouse;

    public MapDesignerView()
    {
        mdv = new MapDesignerView();
        map = new Map(mdv);
        mapController = new MapController(map);
        mapConstructor = new MapConstructor(mapController);
        detectMouse = new MouseEventHandler(mapController);
        InitializeComponent();
    }
}

As you can see I need to make an instance of the MapDesignerView class inside the mapDesignView class, to pass to another class constructor. How do I pass this form to the Map constructor in a different way so it is not endlessly instantiating?

Upvotes: 3

Views: 109

Answers (3)

M.Babcock
M.Babcock

Reputation: 18965

If you really want to pass an instance of MapDesignerView to itself, you can do so by using a constructor with a contract like:

public MapDesignerView(MapDesignerView map)

Upvotes: 2

Guvante
Guvante

Reputation: 19223

mdv = new MapDesignerView();
map = new Map(mdv);

This creates a new object (eventually causing the stack to overflow)

Instead what you want is

map = new Map(this);

And by extension you no longer need the mdv variable.

Upvotes: 1

SLaks
SLaks

Reputation: 887867

It sounds like you're looking for this, which refers to the current instance.

Upvotes: 6

Related Questions