Results 1 to 30 of 30

Thread: [RESOLVED] Enumerator, What is it?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Resolved [RESOLVED] Enumerator, What is it?

    Hey all, brand new to VB.net

    I am learning VB so that I can program Revit, the API chart has a list of the objects I can manipulate but I don't understand the chart yet but will soon enough.

    In the chart, what sticks out first to me is Enumerable, Enumerator and Enumeration. No clue to what this is or what it means. Some of the (3) listed above have the letter 'I' as in IEnumerator. (is there a differece? and what is it?)

    I did a search and kind of understand that it a group, or catalog or catagory, maybe???

    I searched this forum and found some (future useful) information about Enums but my lack of a straight forward definision limits my understanding.

    Can someone please explain in non-tech language with a genric explainaiton and maybe a very elementry example so I can build my understanding and derive more useful questions from.

    Thanks

  2. #2
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Enumerator, What is it?

    generally an enemurator is type that can be sorted, less commonly it is used as Enum which is when you want a list of constants that have names. The I prefix means it is an Interface.

    an example would be a List(Of Integer) this list could have random numbers added to it and then sorted into numerical order.

    Almost any array is Enumerable and most Lists are

    Enumeration also allows items within the structure (be it a list or an array or an IEnumerable or whatever enumerable) to be searched.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Enumerator, What is it?

    There's a difference between Enumerator/Enumerable and Enumeration.

    An Enumeration is basically a list of values which you give a humanly readable name. You can declare one like this
    Code:
    Public Enum CarBrands
       Ford = 0
       Toyota = 1
       Mitsubishi = 2
       'etc...
    End Enum
    Now, in the code, instead of using 'hardcoded' values like 0, 1, and 2, you can use the names Ford, Toyota and Mitsubishi instead. Suppose you have an object 'car' for which you need to set a Brand property. You could make the brand a String, but that may not be desirable (different users may type 'Ford' or 'ford' or 'Fort' even (lol). While they all mean the same thing, your application will not recognize them as the same brand). Instead, you could have codes (0, 1, 2) that denote the brands instead.

    You could hardcode the values, like this
    Code:
    car.Brand = 1
    but as you can see, anyone reading that code will think "Huh? What is 1?".

    Using the enumeration above, you can use this
    Code:
    car.Brand = CarBrands.Toyota
    and the problem is solved.


    Internally (or 'behind the scenes'), the code will still use the integers 0, 1, and 2. The enumeration simply makes the code more readable.

    Of course, you could also forget about the enumeration and simply use (constant?) fields, like this
    Code:
    Private Const Ford = 0
    Private Const Toyota = 1
    Private Const Mitsubishi = 2
    Now, you can also use
    Code:
    car.Brand = Toyota
    So why do we bother with enumerations? The reason is simple: an enumeration is an actual type, and you can specify that type as the type of an argument, or as the return type for a function, etc.
    So, the 'Brand' property on our Car object would not be declared as an Integer (which it actually will be behind the scenes), but as a property of type CarBrands.

    You could also have a function that needs a brand as an argument, like a function that returns the number of car models each brand has. You could do this using integers:
    Code:
    Public Function GetNumberOfCarModels(ByVal brand As Integer) As Integer
       Select Case brand
          Case 0
             Return 15
          Case 1
             Return 32
          Case 2
             Return 23
       End Select
       Return -1   'if the brand is not 0, 1 or 2
    End Function
    Or, much better, you can use the CarBrands enumeration:
    Code:
    Public Function GetNumberOfCarModels(ByVal brand As CarBrands) As Integer
       Select Case brand
          Case CarBrands.Ford
             Return 15
          Case CarBrands.Toyota
             Return 32
          Case CarBrands.Mitsubishi
             Return 23
       End Select
       Return -1   'if the brand is not 0, 1 or 2
    End Function
    See? Much better isn't it?



    Then, you mentioned Enumerable and Enumarator. Those are quite different concepts. I think think there's anything called 'Enumerable' however, only IEnumerable. The I signifies that IEnumerable is an interface and not a class. I don't have time to go into the importance of interfaces right now, but just think of them as lists of what a certain class (a class that implements the interface) needs to implement.

    Any class implementing IEnumerable must implement some method that allow a For loop to run. For example, an array, or a List(Of T), or a Collection, etc, all implement IEnumerable, and that is the reason you can use a For loop on those types. You cannot use a For loop on an Integer, because an Integer does not implement IEnumerable.

    Finally, Enumerator is a concept used in IEnumerables. It is basically the object that 'walks' through the collection. For example, in a For loop
    Code:
    For i As Integer = 0 To 4
    the variable i could be called the enumerator (though I think technically that is a bit general).

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: Enumerator, What is it?

    Thank you both for your reply

    Nick

    I almost understand (or think I do), I will need time to read and re-read your post to digest.

    But wanted to thank you for such an involved and detailed post. You gave me exactly what I asked for.

    5 stars

  5. #5
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: Enumerator, What is it?

    Quote Originally Posted by NickThissen View Post
    Any class implementing IEnumerable must implement some method that allow a For loop to run. For example, an array, or a List(Of T), or a Collection, etc, all implement IEnumerable, and that is the reason you can use a For loop on those types. You cannot use a For loop on an Integer, because an Integer does not implement IEnumerable.
    Nick, I think you meant to say the For...Each loop not the For Loop.

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: Enumerator, What is it?

    Nick,

    Reading your post, I have some questions (as you would expect)

    So does Enumeration create a group such as CarBrand and allows you to add different car brands to this newly created group? Then the program can recognize either the (human name) such as Toyota or recongnize the numerical value of 1 as Toyota as well.

    So when the program runs the user can either enter the numerical value 1 or the human name of Toyota and it mean the same thing.

    But since a human can type either Toyota or toyota the program can catch this difference in sytex caps or no caps and still apply the 1 value to run the program from. (I don't quite follow this part on how a human can type different things and the progam still catch it)


    Because of the numerical value, the program can do mathmatical functions to a string since the value is 1 vs the name Toyota. As in your example of returning the number of car models in your Case function example. (the 1 can work as a string and/or a interger?)

    I have more questons but I feel my questions above might be confusing, I will stop here for now.
    Last edited by VB_begginer; Mar 24th, 2010 at 01:22 PM.

  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Enumerator, What is it?

    Quote Originally Posted by VB_begginer View Post
    So does Enumeration create a group such as CarBrand and allows you to add different car brands to this newly created group? Then the program can recognize either the (human name) such as Toyota or recongnize the numerical value of 1 as Toyota as well.
    It's not really a 'group', but just a list of values that you gave a recognizable name. You can add a value to that list whenever you want, or you can change the name or value of an existing item. That is another advantage of using enumerations vs hardcoding the values: if you decide, for whatever reason, that Toyota should now get value 3, you only need to change it in the enumeration declaration (and even then, the actual value means very little in most cases, so perhaps this doesn't make much sense after all). In the rest of your code, you should be using CarBrands.Toyota, which had a value of 1 before, but now has a value of 3.

    Quote Originally Posted by VB_begginer View Post
    So when the program runs the user can either enter the numerical value 1 or the human name of Toyota and it mean the same thing.
    Almost. The name CarBrands.Toyota means 1 for the compiler, ultimately. You can enter the values directly (see below), but really, you shouldn't. Why would you want to? That takes away all the advantages of using the enumeration in the first place. If you have an enumeration, then use it. Otherwise, should you wish to edit a value, you would have to go through all of your code just in case you used the hardcoded value somewhere instead of the enumeration member!

    Quote Originally Posted by VB_begginer View Post
    But since a human can type either Toyota or toyota the program can catch this difference in sytex caps or no caps and still apply the 1 value to run the program from. (I don't quite follow this part on how a human can type different things and the progam still catch it)
    Actually, the programmer is now 'forced' to type Toyota, because Toyota is more or less like a variable name, and VB will automatically change the casing if it is incorrect. So, if you type
    Code:
    car.Brand = CarBrands.toyota
    it will be converted to Toyota automatically, as VB does always. The same does not hold for C#, where 'toyota' and 'Toyota' are two different identifiers, and typing 'toyota' will then give an error (that the name 'toyota' is not declared in the enumeration CarBrands).

    I think if you simply try it out (try my example or one of your own) then you may understand it better. For example, VB will also show you the Intellisense list, from which you can simply choose the appropriate value:


    You may have come across this before. There's loads of enumerations in the framework, such as the MessageBoxButtons/Icons/etc:


    Quote Originally Posted by VB_begginer View Post
    Because of the numerical value, the program can do mathmatical functions to a string since the value is 1 vs the name Toyota. As in your example of returning the number of car models in your Case function example. (the 1 can work as a string and/or a interger?)
    In the example, the value is an Integer. Enumerations use Integers by default, but you can tell them to use some other types if you want. You cannot use strings however, only 'integral' types, which basically means integer numbers such as Integer, Long, UInt, Short, Byte, etc.

    And yes, the program can do mathematical operations on the value of an enumeration member. However, that is not how they are usually used, and really, it is only a 'coincidence' because enumerations are just Integers behind the scenes. Therefore, you can do this
    Code:
            Dim cb As CarBrands = CarBrands.Mitsubishi
    
            Dim x As Integer = cb + 1
            Dim y As Integer = CarBrands.Ford
    You can directly assign a CarBrands to a variable of type Integer. This is an exception for enumerations, because you cannot do that for any other types (except if you have Option Strict off, which you should not).


    However, there is barely any use for this, because, as I said above, the actual values of the enumeration members should not matter to the programmer at all. The idea is that the coder can use the humanly readable name (Ford), and not worry about which value it actually represents (0).

    For a different example, suppose you are reading data from some device, and that device can return different error codes. Devices usually return cryptic codes, such as error 109535 meaning that the hardware is faulty (or whatever, I'm just making this up). If you have to use those values in your code, that would get really annoying, because you probably won't remember them. Instead, you make one enumeration that captures these errors
    Code:
    Public Enum Errors
       Disconnected = 13
       Overflow = 91239
       HardwareFault = 109535
    Then, instead of using the values, you can let your coders use these names (Disconnected, Overflow and HardwareFault) instead. They don't need to care about the actual values, they just use the names.

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: Enumerator, What is it?

    I think I just had a holy moment!

    I think I understand. An Enum is a list... (as you said)

    You can change this list in one place instead of searching thru all the code to find each instance. When you change the Enum it is a global change.

    The number means nothing to me but to the machine it means everything, I only care about the name that makes sense to me (the human)

    In your Case example the "Return" just happened to be an interger that you specified. No calculation was preformed because no calculation was assigned. (it was just an example)?

    The example you gave me showing the CarBrand.Ford, CarBrand .Toyota, CarBrand.Mitsubishi and compareing to the MessageboxButton.Ok Cleared a lot up for me, I have seen it and I now know what it is, can I assume that these Intellisense lists are Enumerations?


    The Error example cleared a lot up too. Most of my research on Enums lead me to people talking about Error messages, which led me to believe the Enum had something to do with Errors and maybe that was what the "E" in Enum stood for. Like Error Number. But you just use Enum to list the errors.

    (On a side note about error messages. When I get an error message and it say error 199903, now I know there is an english equivilant, I wish programmers would put the english equivialant vs the number, I go back to your example of, HUH?) Drives us users nuts.


    I will need to play around with this but I have enough info to understand what I read now.


    Thanks
    Last edited by VB_begginer; Mar 24th, 2010 at 03:10 PM.

  9. #9
    Hyperactive Member wornways's Avatar
    Join Date
    Mar 2010
    Posts
    261

    Re: Enumerator, What is it?

    Quote Originally Posted by Megalith View Post
    generally an enemurator is type that can be sorted, less commonly it is used as Enum which is when you want a list of constants that have names. The I prefix means it is an Interface.

    an example would be a List(Of Integer) this list could have random numbers added to it and then sorted into numerical order.

    Almost any array is Enumerable and most Lists are

    Enumeration also allows items within the structure (be it a list or an array or an IEnumerable or whatever enumerable) to be searched.
    Oh! Like a list of constants? Like what you find in OpenMode? (Append = 8, Binary = 32, Input = 1, Output = 2, and Random = 4). Is OpenMode an "Enum"?
    In an effort to abolish the writing of "Spaghetti Code",
    some fools got together and invented Spaghetti Syntax.

  10. #10
    Hyperactive Member wornways's Avatar
    Join Date
    Mar 2010
    Posts
    261

    Re: Enumerator, What is it?

    This is a great topic.
    In an effort to abolish the writing of "Spaghetti Code",
    some fools got together and invented Spaghetti Syntax.

  11. #11
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Enumerator, What is it?

    Quote Originally Posted by wornways View Post
    Oh! Like a list of constants? Like what you find in OpenMode? (Append = 8, Binary = 32, Input = 1, Output = 2, and Random = 4). Is OpenMode an "Enum"?
    Yes, as you can clearly see from the first result on google when searching for 'msdn openmode'.

    Kinda offtopic, but OpenMode is really an old way to open a file. You should really look into the System.IO namespace. To read a file you can simply do
    Code:
    Dim contents As String = System.IO.File.ReadAllText(path)
    For more advanced reading you can use a StreamReader
    Code:
    Using reader As New System.IO.StreamReader(path)
       '...
    End Using

  12. #12
    Hyperactive Member wornways's Avatar
    Join Date
    Mar 2010
    Posts
    261

    Re: Enumerator, What is it?

    Quote Originally Posted by NickThissen View Post
    Yes, as you can clearly see from the first result on google when searching for 'msdn openmode'.

    Kinda offtopic, but OpenMode is really an old way to open a file. You should really look into the System.IO namespace. To read a file you can simply do
    Code:
    Dim contents As String = System.IO.File.ReadAllText(path)
    For more advanced reading you can use a StreamReader
    Code:
    Using reader As New System.IO.StreamReader(path)
       '...
    End Using
    For simply reading the contents of a text file, I imagine your right. But for holding a file open for the duration of a session, I think a different approach is necessary. I'm still learning about such approaches. I've found the binary FileStream to be effective thus far. In fact, I'm starting to really like it.
    In an effort to abolish the writing of "Spaghetti Code",
    some fools got together and invented Spaghetti Syntax.

  13. #13
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Enumerator, What is it?

    You can use a StreamReader for that. I used the Using block syntax in my example, which implicitly closes the file at the End Using line. If you don't want to close the file, don't use a Using block and simply don't call the Close method. I assume you know that an open file cannot be accessed by anything else so you should always close the file ultimately.

  14. #14
    Hyperactive Member wornways's Avatar
    Join Date
    Mar 2010
    Posts
    261

    Re: Enumerator, What is it?

    Quote Originally Posted by NickThissen View Post
    You can use a StreamReader for that. I used the Using block syntax in my example, which implicitly closes the file at the End Using line. If you don't want to close the file, don't use a Using block and simply don't call the Close method. I assume you know that an open file cannot be accessed by anything else so you should always close the file ultimately.
    Of course. I've already written routines that handle the FileStream migration. If File1 is selected, it is the file used by the session and is therefore open (thus locked) so that nothing else can mess with it. Once another file is selected, File2 becomes the active file after File1 is closed. The same global variable is used between them.

    It's working great.
    In an effort to abolish the writing of "Spaghetti Code",
    some fools got together and invented Spaghetti Syntax.

  15. #15
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Enumerator, What is it?

    Going back to enumerators, as nick mentioned any type which can be used in a for each loop has to be of IEnumerator or it will produce an error. LINQ is heavily dependent on Enumerator, Enumerable and queryable types can be interchanged. You can take a type of IEnumerator from a LINQ query and put it in an array or a list, this list will be enumerable (can be used with a for each loop), often you will find ForEach as an intellisense option, this is where you can use lambda functions and the likes in order to create the desired new list (i wont go into this now though). Heres an example of a LINQ query which then implements the enumerable property:-

    first lets create an array of words
    Code:
    Dim Words As String = "Long long ago in a galaxy far far away"
    Dim WordArray() As String = Split(Words)
    now lets say we wish to find all the words in this array which contain the letter o, we can do this
    Code:
    ' First the querry
    Sequence = _
       From n In WordArray _
       Where n.Contains("o") _
       Select n
    
    ' Now the enumerator
    For Each Name As String In Sequence
       Console.WriteLine(Name)
    Next
    As you can see the result of the query is an enumarable type and can be used in a for each loop. the result of this as displayed on the console would be
    Code:
    Long
    long
    ago
    Last edited by Megalith; Mar 25th, 2010 at 06:09 AM.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  16. #16

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: Enumerator, What is it?

    Meg

    Is this an example of the program building an Enumeration? Building a List. The Enumerator is stepping thru the Enumeration looking for the words with the letter "o" in it.


    If so this seems to be how the find and replace works for a word editing function in a word or text document.

    Am I on the right track?


    Is "Long long ago in a galaxy far far away" an Array? String Array?

    Dim WordArray() As String = Split(Words) , Does this break down the string into words? Does the space between the words signify where the split is?

    From n In WordArray _ , What does the "n" mean or do? This part lost me

  17. #17
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Enumerator, What is it?

    Quote Originally Posted by VB_begginer View Post
    Is this an example of the program building an Enumeration? Building a List. The Enumerator is stepping thru the Enumeration looking for the words with the letter "o" in it.
    It does not 'build' an Enumeration, it 'builds' an Enumerable. The difference is significant. I explained what an Enumeration is above, and it has nothing to do with loops or Enumerables.


    Quote Originally Posted by VB_begginer View Post
    Is "Long long ago in a galaxy far far away" an Array? String Array?
    It's a String.

    Quote Originally Posted by VB_begginer View Post
    Dim WordArray() As String = Split(Words) , Does this break down the string into words? Does the space between the words signify where the split is?
    Yes, the Split method splits a string into parts. Usually one would specify the delimiter (a comma, a slash, a space, etc), but (I guess) if you don't specify a delimiter than space is used by default. The result of the Split function is a string array.

    Quote Originally Posted by VB_begginer View Post
    From n In WordArray _ , What does the "n" mean or do? This part lost me
    This is LINQ, a new feature in VB that allows you to run 'queries' on objects that can be enumerated (Enumerables as you have called them, not Enumerations). The syntax is very similar to SQL if you happen to know that.
    The first part (the From line) signifies what kind of data you need and gives that data a name (n).

    If you see
    Code:
    From n As String In WordArray _
    Where n.Length > 10 _
    Select n
    you can read that as
    Code:
    Look at each String in the WordArray array, and call that string n
    If the length of n > 10, then select n and add it to the result
    The result is a collection (IEnumerable probably) of strings.

  18. #18

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: Enumerator, What is it?

    so the "n" is just a variable. Meg could have used "f" or "ff" or "gg", if Meg so desired.



    "Yes, the Split method splits a string into parts" , "The result of the Split function is a string array."
    -----
    So the Split method turn the string into an Array or uses the parts for the string in an Array, kind a sort of?




    "It does not 'build' an Enumeration, it 'builds' an Enumerable. The difference is significant. I explained what an Enumeration is above, and it has nothing to do with loops or Enumerables."
    ------
    Yes, I think I understand. I got confussed. Enumeration is a list, I promise I do remember this.

    So the program is building an Enumerable, which is the String Array. The "n" is the Enumerator which walks thru the Enumerable (the string Array) looking for words with the letter "o" in it. The space is a default delimiter.


    An Enumeration is: (from previous lesson)
    Carbrands
    Carbrands.Ford
    Carbrands.Toyta
    etc...

    Which I can see now has nothing to do with Enumerator or Enumeratable.

    Thanks again Nick





    I need to learn how to use the Quote message in reply feature for this forum. Sorry for the lack of not using it...

  19. #19
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Enumerator, What is it?

    Quote Originally Posted by VB_begginer View Post
    so the "n" is just a variable. Meg could have used "f" or "ff" or "gg", if Meg so desired.
    Indeed could have been anything at all just like calling the query Sequence could have been anything
    Quote Originally Posted by VB_begginer View Post
    "Yes, the Split method splits a string into parts" , "The result of the Split function is a string array."
    -----
    So the Split method turn the string into an Array or uses the parts for the string in an Array, kind a sort of?
    yes i needed to create an array as an example of how to make an enumerable type from a query, this seemed a good way so you can see the whole process and as you mention show another enumerable which you recognised
    Quote Originally Posted by VB_begginer View Post
    "It does not 'build' an Enumeration, it 'builds' an Enumerable. The difference is significant. I explained what an Enumeration is above, and it has nothing to do with loops or Enumerables."
    ------
    Yes, I think I understand. I got confussed. Enumeration is a list, I promise I do remember this.
    lol indeed, totally different beasts like hot can mean good looking or can mean temperature
    Quote Originally Posted by VB_begginer View Post
    So the program is building an Enumerable, which is the String Array. The "n" is the Enumerator which walks thru the Enumerable (the string Array) looking for words with the letter "o" in it. The space is a default delimiter.
    Well kind of, I create an Array which is enumerable, I run a query which produces a queryable type, which if you remember i said is closely related (you can have queryable that isnt enumerable) then i use the enumerable part of the queryable type in the For Each loop, had my query not been enumerable then this would have given an error
    Quote Originally Posted by VB_begginer View Post
    An Enumeration is: (from previous lesson)
    Carbrands
    Carbrands.Ford
    Carbrands.Toyta
    etc...

    Which I can see now has nothing to do with Enumerator or Enumeratable.
    yes indeed and about Enums, you can do an Enum without using numbers
    Code:
    Public Enum As CarBrands
        Ford
        Toyota
        Mistsubishi
    End Enum
    where Ford would be 1 Toyota 2 and Mitsubishi 3, if i had done Ford = 0 then Toyota and mitsubishi would have been 1 and 2. Each item is by default one higher than the previous item, i could have started with -100000 if i wanted, conversely they can be numbered by assigning with any integer value at all in any order (500, -200, 300)
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  20. #20

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: Enumerator, What is it?

    Meg

    So in my Revit software, lets say when I tag a door, window or add sheets etc...

    The tag numbers start with a default of 100 and the next door added is 101, 102, 103 etc....

    If I change the number to the door to say 133 the next door I add is automaticly 134, 135 etc...

    I can assume that this is an Enum?


    Now just so I am not confused.... An Enum is short for Enumeration?


    If the above is true with the door, window, etc.. I think you have just opened my eyes to something.

    Thanks Meg

  21. #21

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: Enumerator, What is it?

    I think the door thing is just a counter not an Enum

    I am trying to hard to relate what you are telling me to what I am familiar with. And geting a bit excited because I am learning something new.

  22. #22
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Enumerator, What is it?

    An 'Enum' is just a programming constructs and has no meaning when your application is done. It is just something to make the programming easier for you. I don't know how else to explain it to you.

    If you were using those values in your code as such
    Code:
    Public Enum Tags
       Door = 100
       Window = 101
       Roof = 102
    End Enum
    then use them like this
    Code:
    Dim t As Tags = Tags.Window
    then you are using an enumeration (= Enum).

  23. #23

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: Enumerator, What is it?

    Thanks Nick

    I do understand. I am just over thinking it now. Like I said got excited and typed before thinking.
    My door tags are just counters.


    Both of you have been great help to me. Five star

  24. #24
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: Enumerator, What is it?

    you need to get it out your head that an enumerator is the same as enumeration. to simplify these 2 ideas into single paragraphs then:-

    An Enumeration (Enum) is useful when you wish to have a selection of constants available in a related way. As soon as you type CarBrand and a dot you get all the choices to use. Enum is related to constants. It is a list of constants of type Integer.

    An Enumerator is something that can be counted 1 by 1 hence For Each because each item of the list (or array or collection or whatever type with enumerable) can be accessed due to its enumerable property.

    An enumeration (Enum) does not have enumerable properties although it could be used on an array of integers which is enumerable.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  25. #25
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Enumerator, What is it?

    I haven't read this whole thread so I may be repeating some things, but here's my summary anyway:

    To enumerate something is to assign numbers to its parts. An enumeration is a type whose members each have a number assigned to them. The members are used in code but the numbers are used under the hood for efficiency. An enumerator is an object that assigns sequential numbers to the items in a list and provides access to those items in order of those numbers. An enumerable is a list that can be enumerated by an enumerator.

    Enumerators and enumerables are all classes. Every enumerator implements the IEnumerator interface, while every enumerable implements the IEnumerable interface. The interfaces define what functionality the classes must have and the classes each implement that functionality is an appropriate way.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  26. #26
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Enumerator, What is it?

    Im very interesting in learning this while I think I can use it massively...

    If I'm understand this discussion why then I can not do something like this?
    VB.Net Code:
    1. Public Enum CarBrands
    2.         Ford = 0
    3.         Toyota = 1
    4.         Mitsubishi = 2
    5.     End Enum
    6.  
    7.     Function testFunc(ByVal CarBrands As Integer) As Double
    8.         Select Case CarBrands
    9.             Case Ford       'underlined error: Name Ford is not declared
    10.                 Return 777
    11.             Case Toyota     'underlined error: Name Toyota is not declared
    12.                 Return 888
    13.             Case Mitsubishi 'underlined error: Name Mitsubishi is not declared
    14.                 Return 999
    15.         End Select
    16.     End Function
    17.  
    18.     Sub testMeth()
    19.         MsgBox(testFunc(CarBrands.Ford).ToString)
    20.         'i expect to get "777"
    21.     End Sub
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  27. #27
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Enumerator, What is it?

    Quote Originally Posted by Zeljko View Post
    Im very interesting in learning this while I think I can use it massively...

    If I'm understand this discussion why then I can not do something like this?
    VB.Net Code:
    1. Public Enum CarBrands
    2.         Ford = 0
    3.         Toyota = 1
    4.         Mitsubishi = 2
    5.     End Enum
    6.  
    7.     Function testFunc(ByVal CarBrands As Integer) As Double
    8.         Select Case CarBrands
    9.             Case Ford       'underlined error: Name Ford is not declared
    10.                 Return 777
    11.             Case Toyota     'underlined error: Name Toyota is not declared
    12.                 Return 888
    13.             Case Mitsubishi 'underlined error: Name Mitsubishi is not declared
    14.                 Return 999
    15.         End Select
    16.     End Function
    17.  
    18.     Sub testMeth()
    19.         MsgBox(testFunc(CarBrands.Ford).ToString)
    20.         'i expect to get "777"
    21.     End Sub
    First off, you wouldn't declare the method parameter as type Integer. The whole point of enumerations is that you DON'T use Integers or Strings. Each enumeration is a fully-fledged type, like a class or structure. You declare variables, parameters, etc, as that type.

    As for the question, Ford, etc, are members of the CarBrands type. As with all members, you have to qualify them with either the type name or an object reference, depending on whether they are Shared or not. In this case they are Shared, so it's the type name you need:
    vb.net Code:
    1. Function testFunc(ByVal brand As CarBrands) As Double
    2.     Select Case brand
    3.         Case CarBrands.Ford
    4.             Return 777
    5.         Case CarBrands.Toyota
    6.             Return 888
    7.         Case CarBrands.Mitsubishi
    8.             Return 999
    9.     End Select
    10. End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  28. #28
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Enumerator, What is it?

    WOW
    Thanks jmcilhinney, again your help was fast and accurate...

    I have another subquestion about it:
    While I espect to have many enumerations, where is the best place to put them all so it is all in one place for easy editing, sorting, finding... (I was thinking in direction like separate class or module. Is that recomended here?)?
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  29. #29
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Enumerator, What is it?

    Quote Originally Posted by Zeljko View Post
    WOW
    Thanks jmcilhinney, again your help was fast and accurate...

    I have another subquestion about it:
    While I espect to have many enumerations, where is the best place to put them all so it is all in one place for easy editing, sorting, finding... (I was thinking in direction like separate class or module. Is that recomended here?)?
    Like I said, enumerations are themselves first-class types in .NET, just like classes and structures. There's no more reason to declare an enumeration inside a class than there is to declare another class inside a class. You only nest types if it specifically makes sense to do so, i.e. the nested type will only be used inside the parent type so it's declared private, or else the nested type is only ever used in conjunction with the parent type, e.g. ListViewItem.ListViewSubItem. As such, your enumerations deserve their own code file, just like a class. Given that their declarations are generally small, I usually create a code file name Enumerations.vb and declare all my enumerations together in there.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  30. #30
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Enumerator, What is it?

    Thanks for explaining to me. That lest sentence made me open VS and try it and it worked. I think I will do the same practice like you do with enums...

    I had intention of giving you reps but 'spreading' is a world class problem today.
    Instead I give you
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width