user973511
user973511

Reputation: 329

Does a Form work like a pointer?

Today I tried to create a reusable Framework, and I had no problem to do this... I created a new file "Game.cs" containing the class Game:

class Game
{
    Form Form;

    public Game(Form Form, int Width, int Height)
    {
        //Set Form
        this.Form = Form;
        this.Form.MaximizeBox = false;
        this.Form.FormBorderStyle = FormBorderStyle.Fixed3D;
        this.Form.Size = new Size(Width, Height);
    }
}

Then I add this file into a new Form project, Framework_Demo, using VisualStudio and it contains this:

namespace Framework_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Here I created a new Object game using class Game
            Game game = new Game(this, 600, 600);
        }
    }
}

Maybe this is simple and my question could be obsolete, but why when I debug Framework_Demo appears his Form but also with the properties I set in the costructor of Game class? It's not a problem but I want to know what happens when I set: this.Form = Form. It's really working like a pointer? Can someone explain this behaviour to me? Thank You!

Upvotes: 3

Views: 890

Answers (2)

Shadow Wizard
Shadow Wizard

Reputation: 66388

Yes it's working as kind of pointer by keeping reference to the form itself.

As long as the game instance lives, any change done in that class to its Form member will affect the actual form.

Upvotes: 1

Teoman Soygul
Teoman Soygul

Reputation: 25742

class Game
{
    Form Form;
}

Here the Form field is just a reference to another form, where you get that reference in the constructor of Game class. This is generally done to keep a reference to the owner form inside a child form. When you set Form properties in the constructor, you're actually modifying the owner form.

Upvotes: 2

Related Questions