|
-
May 18th, 2007, 03:52 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Or'ing Enum Values
Hi,
Let me start by applogizing if I used the wrong terminology when writing "Or'ing", it may be "And'ing". I just know I want it to work how the VB6 MsgBox parameters work.
Anyway, I am making a Library program that will help me organize my Movies, Books and Music and I was looking for a simple way to indicate the various formats that I may have for each item. I will be using a checked listbox to set the formats.
I started down the road of using Enums (see below), but I don't think this will allow me to or various formats together like:
Code:
oAudio.AudioFormat = CAudioItem.AudioFormatEnum.CD | CAudioItem.AudioFormatEnum.MP3;
I am thinking I may have to just define the AudioFormat property as an int and assign the multiple formats as indicated above, but that may not be any better.
So, am I going about this the wrong way? What would be the best way to load these values into the checked listbox, if I can use the Or'ing method.
Code:
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
CAudioItem oAudio = new CAudioItem();
oAudio.AudioFormat = CAudioItem.AudioFormatEnum.CD;
oAudio = null;
}
}
class CAudioItem
{
public enum AudioFormatEnum { NONE = 0, CD = 1, MP3 = 2, ALBUM = 4};
private AudioFormatEnum _audioFormat;
public AudioFormatEnum AudioFormat
{
get { return _audioFormat; }
set { _AudioFormat = value; }
}
}
Thanks,
CT
-
May 18th, 2007, 08:24 PM
#2
Re: Or'ing Enum Values
You're going about it exactly the right way except for one thing. If you want to be able to combine enumerated values you should apply the Flags attribute to the type declaration:
Code:
[Flags()]
public enum AudioFormat
{
None = 0,
CD = 1,
MP3 = 2,
Album = 4
}
To display enumerated values in a CheckedListBox you can do this:
C# Code:
private void Form1_Load(object sender, EventArgs e)
{
this.checkedListBox1.BeginUpdate();
foreach (AudioFormat format in Enum.GetValues(typeof(AudioFormat)))
{
this.checkedListBox1.Items.Add(format);
}
this.checkedListBox1.EndUpdate();
}
By using generic code like that you allow for any future changes in the enum without having to change the code. You may prefer to write more specific code though, particularly given that there's no point to displaying the 'None' value. In fact, that actually makes it less clear to the user. You may prefer to just do this:
C# Code:
private void Form1_Load(object sender, EventArgs e)
{
this.checkedListBox1.BeginUpdate();
this.checkedListBox1.Items.Add(AudioFormat.CD);
this.checkedListBox1.Items.Add(AudioFormat.MP3);
this.checkedListBox1.Items.Add(AudioFormat.Album);
this.checkedListBox1.EndUpdate();
}
Now you can get the user's selections at any time by calling a method like this:
C# Code:
private AudioFormat GetSelectedAudioFormats()
{
AudioFormat formats = AudioFormat.None;
foreach (AudioFormat format in this.checkedListBox1.CheckedItems)
{
formats |= format;
}
return formats;
}
You may or may not be aware that this line:is shorthand for this:
C# Code:
formats = formats | format;
Try selecting zero, one or more items in the CheckedListBox and then executing this line:
C# Code:
MessageBox.Show(this.GetSelectedAudioFormats().ToString());
If you do that with more than one selection you'll also see what the Flags attribute does for you. Without it you just get a number that is the sum of the values. With it you get a list of the value names.
Last edited by jmcilhinney; May 18th, 2007 at 08:29 PM.
-
May 19th, 2007, 01:29 AM
#3
Thread Starter
Addicted Member
Re: Or'ing Enum Values
Well, I'm glad to know I was at least headed in the right direction, so maybe that means something is sinking in.
Anyway, I saw an example of someone using the Flags attribute, but I never had a chance to look it up on MSDN to see what it actually did, but I'll be sure to research it more. And while I'm at it, I better lookup .BeginUpdate and .EndUpdate since I'm new to .NET WinForms.
I didn't know about the |= either, but I guess it's like += or -=, which should make the code cleaner.
Thanks again jmcilhinney, I'll be sure to go through this code thourghly. I'd give you more rep, but I have to spread it around first and that'll be hard to do since you seem to always have the answers I'm looking for. 
CT
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|