Get tips on how to efficiently work with Visual Studio. In this weekly series, Walt Ritscher explores a variety of topics, including underused IDE tools, application development suggestions, and more.
- [Narrator] In this tip, I'll look at the enumeration type and show some simple enhancements. I've got a couple of enums defined here in this enums dot cs file. The first one we'll look at is the months enum and later we'll look at the days enum. I'm using this in a class, this tour class. The idea is that these are the tours our company offers and the tours are available in certain months. That's our available months property and there's also the available days property. And the type of the available month is my enum value months.
So then I can use that over here by saying substantiate the tour on line 13. On line 14 I set the tour name. And then on line 15 I specify the available months using the months enumerator value for May. Now the actual numeric value that's stored in here is, let me show you, it's over here, it's going to be the value of five. Switchback over here. When I go and call available months dot to string, I'll get the string representation of the month.
I want to see what that looks like. It says May. Now next what I want to do is I want to store several months. This is available for June and August. So I use the order operator here and I specify June or August together. And those values add up to six and eight so that's fourteen. Looks like that.
That's showing the underlying numeric value that's stored in there, but that doesn't tell me the names of the enumerated values, so let's fix that. There's a attribute in dot net called the flags attribute. I put it here on the line before the enumeration. And that tells dot net when you call to string to return the names of both of the enumerator values with a comma between them, like this.
Next thing we want to look at is the actual numeric values I've stored in here. When you start using flags, a lot of developers like to do bit wise operations on flag values. So using the numbers I did here, one through 12, don't work well with bit wise operations. So what instead we typically do, is we convert these over to powers of two. And that's what I've done in this enum here. I've got my enum days. And it starts at zero and then goes one, two, four, eight, 16, 32, and so on.
That works exactly the same. But now I have more flexibility on the kind of operations I can perform on the enumeration values itself. So let me go back to program and I'll show you how we use days. Down here I'm seeing available days equal days dot Sunday, star value of 64 in there. And again I'll go back and take a look at that value of 64. And then down here I'm saying, available days equal Monday, Wednesday, and Thursday. So that totals up to eight plus four plus one or 13.
Now the binary representation of that looks like this. We're switching on these bits, this bit here, this bit here, and the right most bit. There's the first day. And there's the other three days that are stored in there. So that's how you would do powers of two. Nothing very exciting there, it's self explanatory. But I want to look at a couple other ways some developers like to declare their powers of two. We'll go back over here and collapse this.
This is another common way of doing it, using hexi-decimal values. So I do, one, two, four, eight, and then hex 10, 20, and 40, perhaps more readable. But my favorite's this last one. This is using the left shift operator. So let's show you that. And I'll select these two items and press F1 on my keyboard, which will take me over to the Microsoft help page. And it says that the left shift operator shifts its' first operand left, by the number of bits specified by the second operand.
So this is zero, the value of zero with no shift. Monday is a value of one with no shift. Pop this open here so we can see what we've going on for the next one. So now we're taking a value of one and we're shifting it one position. So it's here, and then we shift it over one. And you see why this works? We keep shifting it one additional time and it just moves over. So you shift it one, then two, then three, and four, and so on.
But the nice thing about this is you get a nice clean syntax. I think this is really readable. The one caveat with this approach is that you can only do 31 of these shifts before you run out of space. If that's the case, then you'll need more than that, then you'll need to convert your enum to use a different integal type. You need to use the unside int.
Skill Level Intermediate
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.