Why do I have to place my enum outside the class.

T

Tony Johansson

Hi!

At the end I have a fully working custom control that shows a traffic light
where
I can select a StateColourTrafficLight property in this object and change
the colour on the traffic light to red or yellow or green. This work
perfect.

There is one this that I don't fully understand and that is if I move this
row
* public enum TrafficLight { red, yellow, green}*
within the class. I get the following error when I try to open the form
designer where I will use the custom control.
"The field 'red' could not be found on the target object. make sure that the
field is defined as an instance variable on the target object and has the
correct scope"

So what do I have to change if I want to have this row
public enum TrafficLight { red, yellow, green}
within the class instead of outside the class or perhaps that row
must be located outside the class.
So as a summary I don't understand why this row
* public enum TrafficLight { red, yellow, green}*
must be placed outside the class ?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lab5
{
public enum TrafficLight { red, yellow, green}

public partial class MyCustomControl : Control
{
private TrafficLight stateColourTrafficLight;
private const int circleSize = 80;
private const int borderSize = 10;

public TrafficLight StateColourTrafficLight
{
get { return stateColourTrafficLight; }
set { stateColourTrafficLight = value; }
}

public MyCustomControl()
{
InitializeComponent();
stateColourTrafficLight = TrafficLight.red;
base.Size = new Size(110, 265);
}

protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
pe.Graphics.FillRectangle(Brushes.Black, new Rectangle(5, 5, 90,
270));

switch (stateColourTrafficLight)
{
case TrafficLight.red:
pe.Graphics.FillEllipse(Brushes.Red, new
Rectangle(borderSize, borderSize, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, 95, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, 180, circleSize, circleSize));
break;

case TrafficLight.yellow:
pe.Graphics.FillEllipse(Brushes.Yellow, new
Rectangle(borderSize, 95, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, borderSize, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, 180, circleSize, circleSize));
break;

case TrafficLight.green :
pe.Graphics.FillEllipse(Brushes.Green, new
Rectangle(borderSize, 180, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, borderSize, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, 95, circleSize, circleSize));
break;
}
}
}
}

//Tony
 
M

Mark Stevens

The simple answer is you don't. I have managed to reproduce your
problem by doing the following:

1. Place the control (as posted) in a project and compile.
2. Drop the control on a form. The control draws correctly.
3. Move the enum into the class and compile

I now get the error in the form designer file.

You can get this to work by deleting the control instance from the
form and then dropping it back on the form.

The system automatically generated the code to create the control when
it was first dropped on the form (with the enum outside the class). It
is not intelligent enough to work out that the when I move the enum it
should also update the designer file.

Hope this helps,
Mark


There is one this that I don't fully understand and that is if I move this
row
* public enum TrafficLight { red, yellow, green}*
within the class. I get the following error when I try to open the form
designer where I will use the custom control.
"The field 'red' could not be found on the target object. make sure that the
field is defined as an instance variable on the target object and has the
correct scope"

So what do I have to change if I want to have this row
public enum TrafficLight { red, yellow, green}
within the class instead of outside the class or perhaps that row
must be located outside the class.
So as a summary I don't understand why this row
* public enum TrafficLight { red, yellow, green}*
must be placed outside the class ?
--
|\ _,,,---,,_ A picture used to be worth a
ZZZzzz /,`.-'`' -. ;-;;, thousand words - then along
|,4- ) )-,_. ,\ ( `'-' came television!
'---''(_/--' `-'\_)

Mark Stevens (mark at thepcsite fullstop co fullstop uk)

This message is provided "as is".
 
T

Tony Johansson

Mark Stevens said:
The simple answer is you don't. I have managed to reproduce your
problem by doing the following:

1. Place the control (as posted) in a project and compile.
2. Drop the control on a form. The control draws correctly.
3. Move the enum into the class and compile

I now get the error in the form designer file.

You can get this to work by deleting the control instance from the
form and then dropping it back on the form.

The system automatically generated the code to create the control when
it was first dropped on the form (with the enum outside the class). It
is not intelligent enough to work out that the when I move the enum it
should also update the designer file.

Hope this helps,
Mark



--
|\ _,,,---,,_ A picture used to be worth a
ZZZzzz /,`.-'`' -. ;-;;, thousand words - then along
|,4- ) )-,_. ,\ ( `'-' came television!
'---''(_/--' `-'\_)

Mark Stevens (mark at thepcsite fullstop co fullstop uk)

This message is provided "as is".

No I don't understand what I can do to fix this.
Can you explain again what I can do ?
I just want to be able to place the enum within the class if it's possible ?

//Tony
 
F

Family Tree Mike

Hi!

At the end I have a fully working custom control that shows a traffic light
where
I can select a StateColourTrafficLight property in this object and change
the colour on the traffic light to red or yellow or green. This work
perfect.

There is one this that I don't fully understand and that is if I move this
row
* public enum TrafficLight { red, yellow, green}*
within the class. I get the following error when I try to open the form
designer where I will use the custom control.
"The field 'red' could not be found on the target object. make sure that the
field is defined as an instance variable on the target object and has the
correct scope"

So what do I have to change if I want to have this row
public enum TrafficLight { red, yellow, green}
within the class instead of outside the class or perhaps that row
must be located outside the class.
So as a summary I don't understand why this row
* public enum TrafficLight { red, yellow, green}*
must be placed outside the class ?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lab5
{
public enum TrafficLight { red, yellow, green}

public partial class MyCustomControl : Control
{
private TrafficLight stateColourTrafficLight;
private const int circleSize = 80;
private const int borderSize = 10;

public TrafficLight StateColourTrafficLight
{
get { return stateColourTrafficLight; }
set { stateColourTrafficLight = value; }
}

public MyCustomControl()
{
InitializeComponent();
stateColourTrafficLight = TrafficLight.red;
base.Size = new Size(110, 265);
}

protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
pe.Graphics.FillRectangle(Brushes.Black, new Rectangle(5, 5, 90,
270));

switch (stateColourTrafficLight)
{
case TrafficLight.red:
pe.Graphics.FillEllipse(Brushes.Red, new
Rectangle(borderSize, borderSize, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, 95, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, 180, circleSize, circleSize));
break;

case TrafficLight.yellow:
pe.Graphics.FillEllipse(Brushes.Yellow, new
Rectangle(borderSize, 95, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, borderSize, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, 180, circleSize, circleSize));
break;

case TrafficLight.green :
pe.Graphics.FillEllipse(Brushes.Green, new
Rectangle(borderSize, 180, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, borderSize, circleSize, circleSize));
pe.Graphics.FillEllipse(Brushes.Gray, new
Rectangle(borderSize, 95, circleSize, circleSize));
break;
}
}
}
}

//Tony

The enum should have no problem being placed inside the class. To
external callers, the enum is called MyCustomControl.TrafficLight, not
just TrafficLight, though.
 
T

Tony Johansson

Mark Stevens said:
The simple answer is you don't. I have managed to reproduce your
problem by doing the following:

1. Place the control (as posted) in a project and compile.
2. Drop the control on a form. The control draws correctly.
3. Move the enum into the class and compile

I now get the error in the form designer file.

You can get this to work by deleting the control instance from the
form and then dropping it back on the form.

The system automatically generated the code to create the control when
it was first dropped on the form (with the enum outside the class). It
is not intelligent enough to work out that the when I move the enum it
should also update the designer file.

Hope this helps,
Mark

I works now when I followed our advice.

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top