-5

I am creating a windows application written in C#. I created a class Palette which defines an enumerated type of property: direction {up, down, right, left}. Inside another class of type Form1 I created an instance p of Palette.

Here is a simplified version:

namespace WindowsFormsApplication1
{
    public class Palette
    {
        public Direction _direction
        {
            set { this._direction = value; }
            get { return this._direction; }
        }
    }

    public partial class Form1 : Form
    {
        private Palette p;
        public enum Direction
        {
            Left, Right, Up, Down
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            p = new Palette { _direction = Direction.Up };
        }
    }       

Then i think, p.direction should also be an enumerated type, is not it?

My software does not think that because I use it to compare p.direction! = Direction.up.

It thinks the type on the left is Palette._direction. The right type is enum.

How can i do it?

rene smith
  • 83
  • 1
  • 9
  • 4
    Could you post the code in question? It's hard for me to understand what the issue is exactly. – Wyatt Shuler Oct 13 '17 at 16:07
  • 1
    Please add punctuation. I'm not going to struggle to read the question. – itsme86 Oct 13 '17 at 16:09
  • well I really want to post my code. but the website just don't allow me . – rene smith Oct 13 '17 at 16:44
  • "well I really want to post my code" Please do. You should be able to put the text right in the question. Indent each line with 4 spaces to make the markdown engine recognize it as a code block. – itsme86 Oct 13 '17 at 16:44
  • because every time I add the picture, it said my post had some text which is similar to code. I tried to change it ,but i failed. – rene smith Oct 13 '17 at 16:46
  • Yeah, please don't post code as a picture. That limits its searchability for future visitors that might be encountering a similar issue. – itsme86 Oct 13 '17 at 16:46
  • Can you post the _exact_ error message? Is it possible that you have `Direction` defined more than once in your code? – Chris Dunaway Oct 13 '17 at 19:20
  • @renesmith - I rewrote your question to make it clearer. Can you confirm that my changes are correct? For the future, you're much more likely to get helpful answers if your question contains a [mcve] -- a **minimal** example of code that fully demonstrates your problem. – dbc Oct 13 '17 at 19:30
  • thanks ,it is more clear ,but 不是很正确, Because there should also be a Direction of enum type which is defined by me in the Palette class – rene smith Oct 14 '17 at 03:17
  • @ChrisDunaway I define Direction in both Palette and Form1. I want to show the picture, it show the error more clearly than i told you , but i just cannot . the website don't allow. Sorry – rene smith Oct 14 '17 at 03:22
  • 1
    @renesmith - code and error messages must be included in your question as text, not as images. See [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/q/285551) for why. In addition, you might want to take the [tour] and read [ask] for tips on how to ask questions that are likely to get answered. – dbc Oct 14 '17 at 03:50

1 Answers1

0

The reason your code does not compile as expected is that you have declared the enum Direction to be a nested type, nested inside the outer type Form1, while the referring type Palette is not so nested. A nested type is a type defined within a class or struct. When referred to by code within that outer type, its name can be used directly. I.e. inside Form1 you can refer to your enum by simply doing Direction.Up. But outside the containing type it is necessary to use the full name of the nested type, which is Form1.Direction.

Thus, to fix your problem, you could:

  1. Move Direction outside of Form1, making it a top-level type in the WindowsFormsApplication1 namespace.

    This is recommended if you are going to use Palette in multiple different forms.

  2. Make both Palette and Direction be nested types inside Form1.

    This is recommended if Palette is only used inside Form1 or needs access to access private or protected members of Form1.

  3. In Palette use the full name for Direction:

    public Form1.Direction _direction { get; set; }
    

    While this will compile it is not recommended as it is awkward and inconsistent.

Incidentally, you have an unrelated problem in your code, namely that your _direction property has an infinite recursion. You should modify it to be an auto-implemented property or make the manually created backing field have a different name than the property.

Thus a fixed version of your code might look like:

public class Palette
{
    Direction _direction;

    public Direction Direction
    {
        set { this._direction = value; }
        get { return this._direction; }
    }
}

public enum Direction
{
    Left, Right, Up, Down
}

public partial class Form1 : Form
{
    private Palette p;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        p = new Palette { Direction = Direction.Up };
    }
}

Since your full code apparently compiles, you may have some other type WindowsFormsApplication1.Direction whose name would conflict with that of your nested Direction type if you were to move it out of Form1. If so, and you want to keep Palette outside of Form, your options are:

  1. In the event they are actually duplicates, simply eliminate the inner nested type.

  2. If they are not duplicates, rename the Direction enum to something else, say DirectionType.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • 其实是事实 I define Direction in Form1 and Palette. – rene smith Oct 14 '17 at 03:39
  • @renesmith - Well that would be your mistake. You need to define it in only one place. Your original question did not show that by the way. In the future, when asking a question please try to include a [mcve] that demonstrates your problem. In this case your question did not show both definitions for `Direction`. – dbc Oct 14 '17 at 03:48
  • oh, but why it is wrong to define it in two class? – rene smith Oct 14 '17 at 03:56
  • @renesmith - because c# is [nominally typed](https://en.wikipedia.org/wiki/Nominal_type_system) rather than [structurally typed](https://en.wikipedia.org/wiki/Structural_type_system). The two enum types `WindowsFormsApplication1.Palette.Direction` and `WindowsFormsApplication1.Form1.Direction` are differently named so they are different types. The fact that their contents are identical is irrelevant. – dbc Oct 14 '17 at 04:00
  • nested type and non-nested type cannot compile? even if they are both enum type? And if I want to compare it , i need to be sure they are the same kind of enum type if I want to judge if they are the same, right? – rene smith Oct 14 '17 at 04:19
  • @renesmith - You can't just assign one to the other because they're not the same type, and c# is a [statically and strongly typed language](https://stackoverflow.com/questions/859186/why-is-c-sharp-statically-typed). Remove the duplication. Otherwise you have to [convert](https://stackoverflow.com/q/1818131). – dbc Oct 14 '17 at 04:23
  • 分配?我没有这样做。你是说创建一个新变量吗?总之,由于他们是在两个类中创建的枚举变量,因此不是同一种类型,从而不能进行比较,我的理解对吗? – rene smith Oct 14 '17 at 04:26
  • @renesmith - well your code didn't compile and was incomplete so who knows, I took my best guess as to what you were doing. If you were passing an argument of type `WindowsFormsApplication1.Form1.Direction` to the `Palette` constructor that expects an argument of the type `indowsFormsApplication1.Palette.Direction` then it's the same thing. And since it appears I wasn't able to guess your actual problem from your code fragments I'll be deleting this answer; good luck! – dbc Oct 14 '17 at 04:29
  • you don't need to do it. I just a beginner of C#. So I think i am trying to understand you answer. thanks you! – rene smith Oct 14 '17 at 04:35
  • @renesmith - then if the question is answered please do [mark it as such](https://meta.stackexchange.com/questions/147531/how-mark-my-question-as-answered-on-stackoverflow). – dbc Oct 14 '17 at 04:49
  • and there is another thing I am confused. "_direction property has an infinite recursion." I don't understand. and if I change it into public Direction Direction { set { this._direction = value; } get { return this._direction; } } I don't know if there will be ambiguity of Direction. Because it can be a type and also can be a member, right? when i changed it as you told me ,it showed WindowsFormsApplication1.Palette.Direction and WindowsFormsApplication1.Palette.Direction contains ambiguity – rene smith Oct 14 '17 at 10:48