Danny Beckett
Danny Beckett

Reputation: 20806

Defining custom type wrongly?

In the following code, I'm trying to define a custom type:

public class WindowPosition
{
    public static WindowPosition Below;
    public static WindowPosition Right;
}

private void ChildWindow(Form Form, WindowPosition Position)
{
    Form.Location = new Point(
        Position == WindowPosition.Right ? this.Location.X + this.Width : 0,
        Position == WindowPosition.Below ? this.Location.Y + this.Height : 0
    );

    Form.Show();
}

private void buttonNew_Click(object sender, EventArgs e)
{
    ChildWindow(new New(), WindowPosition.Below);
}

The code is supposed to make the New form open directly below the main form - but instead it's opening here:

a

New's StartPosition is set to Manual.

I think I'm defining the type improperly. How can I properly define it?

Or otherwise what is the problem, or am I approaching this the wrong way?

Upvotes: 3

Views: 107

Answers (1)

Rob G
Rob G

Reputation: 3526

You want an Enum, not a class.

enum WindowPosition {
     Right,
     Bottom
}

Reference it like so: WindowPosition.Right

What you're doing is declaring a class, then saying it has 2 static members of the same type as it, which isn't wholly unreasonable for some different applications, but it won't work for this.

The reason it doesn't work with your code is because neither of them are assigned anything, and so they both return null, which would make WindowPosition.Right == WindowPosition.Left return true.

Upvotes: 7

Related Questions