Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 227
user2602079 Avatar asked Jul 11 '26 02:07

user2602079


2 Answers

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

like image 50
SLaks Avatar answered Jul 18 '26 03:07

SLaks


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)
like image 21
M.Babcock Avatar answered Jul 18 '26 03:07

M.Babcock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!