Click here to Skip to main content
15,877,573 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: How do I play audio files in vb.net Pin
Paul Conrad28-Nov-07 12:36
professionalPaul Conrad28-Nov-07 12:36 
Questionprint local rdlc report without showing Pin
Gulfraz Khan28-Nov-07 6:39
Gulfraz Khan28-Nov-07 6:39 
Questioncode for scanning and saving from my application Pin
er_niraj28-Nov-07 3:57
er_niraj28-Nov-07 3:57 
GeneralRe: code for scanning and saving from my application Pin
SekharOne6-Dec-07 22:53
SekharOne6-Dec-07 22:53 
QuestionEnumeration Question Pin
imonfiredammit28-Nov-07 3:52
imonfiredammit28-Nov-07 3:52 
AnswerRe: Enumeration Question Pin
Colin Angus Mackay28-Nov-07 4:06
Colin Angus Mackay28-Nov-07 4:06 
GeneralRe: Enumeration Question Pin
sgorozco28-Nov-07 16:41
sgorozco28-Nov-07 16:41 
AnswerRe: Enumeration Question Pin
sgorozco28-Nov-07 11:39
sgorozco28-Nov-07 11:39 
Hi Erica Smile | :)

It's not clear in your post if you are asking about enumerations or enumerators.
They sound similar, but are totally different concepts. However, since you are asking about the difference between them and "normal variables" I assume you mean enumerations.

Talking about Visual Basic (at least with version 2003), their purpose varies depending whether you are compiling with Option Strict On.

With Option Strict Off, there is little difference between an enumeration and and integral variable. The enumeration simply helps the editor's Code Completion engine to suggest possible values for a variable (something really helpful). However, with Option Strict On, they allow you to program more robust applications since you will be creating a new data type that will have a restricted number of possible valid values.

To illustrate the enumeration's superiority *when used in an Option Strict On context*, imagine you are writing some sort of media player application. Most probably you need to control the player's state (playing, stopped, recording, etc). One valid way of doing it using normal integral variables as you suggest would be the following:

<code>
  Const psStopped As Integer = 1
  Const psPlaying As Integer = 2
  Const psPaused As Integer = 3
  Const psRecording As Integer = 4</code>


Then, you could somewhere in the code have a variable for controlling the state

<code>
  Dim state as Integer
  ...
  ...
  ' Set the player's state to Playing
  state = psPlaying</code>


This works, however, since the state value is held on a variable of type Integer, you might inadvertently add the following code

state = 5  ' oops this is an undefined player state


This is valid code and will compile without a fuzz, yet you might put your application in an invalid state and get unwanted results since the value 5 is a valid Integer value, but it is not an expected player state in your app's logic.

However, if you rewrite your application using an enumeration AND compile with Option Strict On:

<code>
Option Strict On
...

   Public Enum PlayerStates
      Stopped = 1
      Playing = 2
      Paused = 3
      Recording = 4
   End Enum

   ...
   Dim state as PlayerStates
   ...
   ...
   ...
   state = PlayerStates.Playing;
   ...
   state = 5     ' This will give a compiler error, but ONLY IF compiling with 
                 ' Option Strict On

</code>

In this case, the compiler helped you avoid a possible programming error by disallowing assigning an invalid player state, according to the values defined in the PlayerStates enumeration.

This and other "defensive programming" options are available when compiling with Option Strict On, which is something I would recommend in the long run.

Hope I have clarified your doubt. Big Grin | :-D

Cheers,

Gerardo
QuestionCode to ZIP Files in VB.NET Pin
sameer.pandurangi28-Nov-07 2:29
sameer.pandurangi28-Nov-07 2:29 
AnswerRe: Code to ZIP Files in VB.NET Pin
pmarfleet28-Nov-07 2:56
pmarfleet28-Nov-07 2:56 
AnswerRe: Code to ZIP Files in VB.NET Pin
DigiOz Multimedia28-Nov-07 5:46
DigiOz Multimedia28-Nov-07 5:46 
GeneralRe: Code to ZIP Files in VB.NET Pin
Paul Conrad28-Nov-07 12:37
professionalPaul Conrad28-Nov-07 12:37 
QuestionProblem with Listbox code Pin
soniasan28-Nov-07 2:12
soniasan28-Nov-07 2:12 
AnswerRe: Problem with Listbox code Pin
Dave Kreskowiak28-Nov-07 5:03
mveDave Kreskowiak28-Nov-07 5:03 
GeneralRe: Problem with Listbox code Pin
Paul Conrad28-Nov-07 12:38
professionalPaul Conrad28-Nov-07 12:38 
QuestionSending Mail through Crystal Report. Pin
KETAN K.28-Nov-07 1:33
KETAN K.28-Nov-07 1:33 
AnswerRe: Sending Mail through Crystal Report. Pin
Tom Deketelaere28-Nov-07 3:28
professionalTom Deketelaere28-Nov-07 3:28 
QuestionPop Up Pin
Aparna.B28-Nov-07 1:11
Aparna.B28-Nov-07 1:11 
AnswerRe: Pop Up Pin
Vimalsoft(Pty) Ltd28-Nov-07 2:10
professionalVimalsoft(Pty) Ltd28-Nov-07 2:10 
GeneralRe: Pop Up Pin
Tom Deketelaere28-Nov-07 3:37
professionalTom Deketelaere28-Nov-07 3:37 
AnswerRe: Pop Up Pin
Dave Kreskowiak28-Nov-07 4:54
mveDave Kreskowiak28-Nov-07 4:54 
GeneralRe: Pop Up Pin
Paul Conrad28-Nov-07 12:40
professionalPaul Conrad28-Nov-07 12:40 
AnswerRe: Pop Up Pin
Paul Conrad28-Nov-07 12:39
professionalPaul Conrad28-Nov-07 12:39 
Questionstill having probs listing Windows Explorer using Shell Pin
GuildfordG28-Nov-07 0:47
GuildfordG28-Nov-07 0:47 
AnswerRe: still having probs listing Windows Explorer using Shell Pin
Dave Kreskowiak28-Nov-07 4:47
mveDave Kreskowiak28-Nov-07 4:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.