Page 1 of 1

Enums:- The Good & The Bad Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,466
  • Joined: 29-May 08

Posted 08 November 2010 - 09:50 AM

Enums

A Enum is simply a type-safe constant.


Public Enum CardSuit
  Spades
  Clubs
  Diamonds
  Hearts
End Enum


Public Enum CardFace
  Two
  Three
  Four
  Five
  Six
  Seven
  Eight
  Nine
  Ten
  Jack
  Queen
  King
  Ace
End Enum



Let's define a simple structure that utilises the two enums (CardSuit & CardFace).
Spoiler


The Good

The good thing about enums it makes understand your easier.
Dim Card_A = PlayingCard.CreateNew(CardFace.Ace, CardSuit.Spades)


You can easily till I'm a creating a new instance of the Playing Card Structure with the value Ace Of Spades.


]The Bad
This is valid.
  Dim Card_B = PlayingCard.CreateNew(CardSuit.Diamonds, CardFace.Two)


Which is the Four of Spades.
This because enums are that they are evaluated as the underlying type, which my case it is the default Integer.

This also explains the need for the follow section of code in CreateNew
 If Not [Enum].IsDefined(GetType(CardFace), Face) Then Throw New ArgumentOutOfRangeException
 If Not [Enum].IsDefined(GetType(CardSuit), Suit) Then Throw New ArgumentOutOfRangeException


If it was for this, it could be any valid integer.

What would be better if the enum type information was preserved and respected.
e.g. I defined that it should an CardFace and not an Integer.


Edit:

Forgot to mention you can define an Enum which is treated like a sets of bits.
<Flags>
Enum 
 LowBit
 MiddleBit
 OtherBit
 HighBit
End Enum


Example
Dim myFlag = LowBit + HighBit + OtherBit


Note: A word of warning though. Don't change the ordering if you save a value containing an enum as it will change its meaning.

This post has been edited by AdamSpeight2008: 21 August 2014 - 09:32 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Enums:- The Good & The Bad

#2 Guest_Joe Enos*


Reputation:

Posted 08 November 2010 - 11:47 AM

I believe
Option Strict On
will prevent your code from compiling if you try to pass the wrong type in. Or of course you could switch over to C# or another language that requires full type safety by default.
Was This Post Helpful? 0

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,466
  • Joined: 29-May 08

Posted 08 November 2010 - 12:40 PM

View PostJoe Enos, on 08 November 2010 - 06:47 PM, said:

I believe
Option Strict On
will prevent your code from compiling if you try to pass the wrong type in.

Not always true.
  Dim Card_c = PlayingCard.CreateNew(0, 0)


Is allowed, no type casting.
Was This Post Helpful? 0
  • +
  • -

#4 jtenos  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 5
  • Joined: 31-May 06

Posted 09 November 2010 - 07:57 AM

Good point - funny, I never realized that you can do this in C# either without explicit casting until I just tried it. I guess I've never had a scenario where I wanted to pass a regular integer into a method expecting an enum - just seems like a bad idea most of the time. Maybe it would work in the data layer to store an integer in the DB while treating it as an enum in code - but I think I'd still put the explicit casting in the code just to keep it readable.

But at least "Option Strict On" or C# will still prevent you from accidentally passing in the wrong enum. Definitely better than nothing.

View PostAdamSpeight2008, on 08 November 2010 - 11:40 AM, said:

View PostJoe Enos, on 08 November 2010 - 06:47 PM, said:

I believe
Option Strict On
will prevent your code from compiling if you try to pass the wrong type in.

Not always true.
  Dim Card_c = PlayingCard.CreateNew(0, 0)


Is allowed, no type casting.

Was This Post Helpful? 0
  • +
  • -

#5 mangel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 10

Posted 15 February 2011 - 03:27 PM

Couldn't you create a card structure having two properties typed using the two enums. Then a deck of cards would be created
with generics as below.

Dim CardDeck as List of Type Card

The card deck will be type safe. Then the CardDeck's Add method would ensure type safety.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1