473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Enum or a class

Hi Everyone,

I have writing a class that represents a object on a screen. The object has
normal things like X,Y, Width, Height, ZOrder, and it also has drawing
attributes and flags.

It has properties for setting and getting values from instance variables
that represent the above attributes of the class.
For the flags, which are bit values, I have 3 methods, one for setting, one
for removing and one for testing for the flags.

public abstract class ScreenItem
{
....
private uint flags;

public void SetFlags(uint flags)
{
this.flags |= flags;
}

public void RemoveFlags(uint flags)
{
this.flags &= ~flags;
}

public bool HasFlag(uint flag)
{
return (this.flags & flag) == flag;
}
....
}

now the question is should the flags be defined as an enum or another class
with const uints? What is the correct way of defining such a thing in C#

public abstract class ScreenItem
{
....
public enum FlagType { Selectable = 1, Movable = 2, Sizable = 4,
Editable = 8 };
....
}

or

public abstract class ScreenItem
{
....
public class FlagType
{
public const uint Selectable = 1;
public const uint Movable = 2;
public const uint Sizable = 4;
public const uint Editable = 8;
}
....
}

Thanks
AliR.
Jul 29 '08 #1
3 1848
See "FlagsAttribute" in MSDN. Here's one way to do it (very quickly written)

public abstract class ScreenItem
{
public bool Selectable
{
get
{
return GetFlag(Flags.Selectable);
}
set
{
SetFlag(Flags.Selectable, value);
}
}

private bool GetFlag(Flags flag)
{
Debug.Assert(flag != Flags.None);
return ((m_Flags & flag) == flag);
}

private void SetFlag(Flags flag, bool on)
{
Debug.Assert(flag != Flags.None);

if (on)
{
m_Flags |= flag;
}
else
{
m_Flags &= ~flag;
}
}

[Flags]
private enum Flags
{
None,
Selectable = 0x1
}

private Flags m_Flags;
}
Jul 29 '08 #2
It should be implemented as an enum. Its location is really
immaterial, since they can technically be global.

I do agree that FlagsAttribute should be used.

I have written a few classes with an Add and Remove method for flags.
It is of course just a nicety for non-bit-sauvy individuals.

You may also consider making the enum private to the base class of
your widgets and simply having properties that turn the bits on and
off.

private enum FlagType { Selectable = 1, ... }

private FlagType flags;

public bool Selectable
{
get { return HasFlag(FlagType.Selectable); }
set
{
if (value)
{
SetFlag(FlagType.Selectable);
}
else
{
UnsetFlag(FlagType.Selectable);
}
}
}

Tada!
Jul 29 '08 #3
Thanks for the replys.

AliR.

"AliR (VC++ MVP)" <Al**@online.nospamwrote in message
news:gU****************@flpi149.ffdc.sbc.com...
Hi Everyone,

I have writing a class that represents a object on a screen. The object
has normal things like X,Y, Width, Height, ZOrder, and it also has drawing
attributes and flags.

It has properties for setting and getting values from instance variables
that represent the above attributes of the class.
For the flags, which are bit values, I have 3 methods, one for setting,
one for removing and one for testing for the flags.

public abstract class ScreenItem
{
....
private uint flags;

public void SetFlags(uint flags)
{
this.flags |= flags;
}

public void RemoveFlags(uint flags)
{
this.flags &= ~flags;
}

public bool HasFlag(uint flag)
{
return (this.flags & flag) == flag;
}
....
}

now the question is should the flags be defined as an enum or another
class with const uints? What is the correct way of defining such a thing
in C#

public abstract class ScreenItem
{
....
public enum FlagType { Selectable = 1, Movable = 2, Sizable = 4,
Editable = 8 };
....
}

or

public abstract class ScreenItem
{
....
public class FlagType
{
public const uint Selectable = 1;
public const uint Movable = 2;
public const uint Sizable = 4;
public const uint Editable = 8;
}
....
}

Thanks
AliR.


Jul 30 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
14
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
13
by: toton | last post by:
Hi, I have some enum (enumeration ) defined in some namespace, not inside class. How to use the enum constant's in some other namespace without using the whole namespace. To say in little...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.