How to do proper "sets" in C#?

  • Thread starter Erik Funkenbusch
  • Start date
E

Erik Funkenbusch

I've been thinking about different ways to address this problem, and I
figured i'd just toss this out and see if anyone has a good solution.

I've got database field of type char(1), this field can contain a number of
different values {O, P, B, L, C, 1, 2, 3, 4, 5, 6, 7 , 8, 9}

Now, I want to translate this to some form of typed variable in my data
abstraction layer.

My initial thought was to use an enum, with the proper names of each of
those types {Open = 'O', Postponed = 'P', etc..} but I discovered that you
can't base an enum on type char.

My next thought was to simply use a standard enum, and do some nasty big
switch statement logic (or array indexing, or a dictionary lookup of some
sort), but I don't like this if I don't have to. Further, I may have need
to get at the "real" value behind the enum from time to time.

Maybe I should just break down and create a data class that can accept both
enumeration values and "real" values?

What i'm really looking for is some kind of "set" logic. Any thoughs on
how to approach this?
 
J

Jani Järvinen [MVP]

Erik,
What i'm really looking for is some kind of "set" logic. Any thoughs on
how to approach this?

C# doesn't have a concept of sets, for example like Borland's Delphi for
..NET does, but why not use a bit mask to represent the elements in the set?

That is, an enum is useful if only one of the elements is going to be
"active" at one time. However, for sets you could use the bits in a, say, 32
bit integer to give you 32 elements. That should be quite enough for most
situations. Set operations like union or intersection could be achieved with
bitwise operators. However, you would still need a class to handle the
operations, but you could use operator overloading to make things look
simpler.

Alternatively, you could use lists and perhaps trees to simulate sets,
depending on your needs. See this page for discussion:

http://en.wikipedia.org/wiki/Set_(computer_science)

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
C

Christof Nordiek

Hi, Erik

you could use an enum with basetype ushort wich is implicitly convertable to
char
and has the same size.
this should work:
enum foo: short
{
Open = 'O',
Postponed = 'P
}

To get a char for a value you cann use explicit convertion:

foo bar = foo.Open;
char fuz = (char)bar;
 
K

Kevin Spencer

Casting:

public enum fieldChars : int
{
Open = (int) 'O',
PostPoned = (int) 'P'
}

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Expect the unaccepted.
 
E

Erik Funkenbusch

That is, an enum is useful if only one of the elements is going to be
"active" at one time. However, for sets you could use the bits in a, say, 32
bit integer to give you 32 elements. That should be quite enough for most
situations. Set operations like union or intersection could be achieved with
bitwise operators. However, you would still need a class to handle the
operations, but you could use operator overloading to make things look
simpler.

Actually, no. An enum works for me, I don't need multiple values (and
besides, i'd rather use a FlagsAttributed enum for that.

The big issue was having "friendly" and type-safe names for the field,
while still being able to get at the underlying data.

As others have presented, I can use an int or ushort to accomplish this.
 
J

Jani Järvinen [MVP]

Erik,
An enum works for me, I don't need multiple values (and
besides, i'd rather use a FlagsAttributed enum for that.

Okay, although I'd call a "set" something that can have multiple values.
As others have presented, I can use an int or ushort to accomplish this.

All's well, the main thing is that you got your issue solved.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
E

Erik Funkenbusch

Erik,


Okay, although I'd call a "set" something that can have multiple values.

Not necessarily. It might, or it might not. If you only have one value,
but it can be any value within a defined set, then the fact that the value
can only be one of those values is what makes it a set.

Technically, it's possible to force other values onto an enum, which is why
it can't be a true set. There's no rules that guarantee that a value lives
in the rules of the set.
 

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