Better VBA 4 - Using Enums (Enumerations) in VBA

Поділитися
Вставка
  • Опубліковано 16 жов 2024
  • Enums are groups of numeric constants. They take the concept of constants one step further. I show when and how use then to improve the maintainability of your code.
    The web-page to the Better VBA series:
    codekabinett.co...

КОМЕНТАРІ • 27

  • @user12345654
    @user12345654 5 років тому +2

    Very helpful lesson. But are you sure first enum variable is assigned 1 and not 0 by default?

    • @codekabinettcomen
      @codekabinettcomen  5 років тому +1

      You are correct. By default Enums will start with 0 for the first enum item. - I usually define explicit values for the enum items, that's why I made that mistake. Thank you very much for pointing this out!

  • @werner191052
    @werner191052 3 роки тому +1

    For decades of years I did not understand the sense of Enums, although I to read books and Online Helps in Access. After watching this great Video tutorial all things are clear!! Thank you very much.

  • @SimpleExcelVBA
    @SimpleExcelVBA 3 роки тому +1

    I need to admit that I was not aware about that I can use Enum as sub/function parameter. It can be soo useful! Thanks!

  • @MD-xs6fx
    @MD-xs6fx 3 роки тому +1

    Always wondered what enum meant and how to use it. Thanks again.

  • @LearnAccessByCrystal
    @LearnAccessByCrystal 5 років тому +1

    always good to be explicit :) another good video on giving thought to code. I can see where enumerations are a great benefit when used as type for parameters ... nice to be prompted with choices.
    I've put comments in the code specifying values for parameters and what they mean, never giving a though to intellisense ... but as you demonstrate, I can see where enums could be extremely useful to supply choices when coding.
    I've read code with enums, and understand the logic -- just never saw any benefit till you show it so explicitly. I have an application I can use them in right now, and just might! Thanks for the idea and clear demonstration, Phillip

  • @michaelkrailo5725
    @michaelkrailo5725 4 роки тому +1

    Very nice, I just went back through some of my code and was able to get rid of the comments and replace with ENUM. Much more readable now.

  • @LosoIAm
    @LosoIAm 2 роки тому +1

    Awesome tutorial, many thanks!

  • @hamzaamir1218
    @hamzaamir1218 4 роки тому +1

    Excellent... Phillip, very easy and understandable english..i really appreciate it and will follow for my learning to excel VBA.

  • @wilsonfineart
    @wilsonfineart 5 років тому +1

    I used to use Enums, but stopped for some reason. Now, I will go back and use them more often.

  • @userme2803
    @userme2803 3 роки тому

    Very good video, thank you. I have a question please, XlLineStyle is enum in excel VBA. Is XlLineStyle part of any class in VBA? where is located compare to other VBA object hierarchy. Thanks again.

  • @chileanwey
    @chileanwey 3 роки тому +1

    thank you Philip! do you recommend any book for vba programming in access?

    • @codekabinettcomen
      @codekabinettcomen  3 роки тому

      I've got a book recommendation page for Access and VBA here: codekabinett.com/rdumps.php?Lang=2&targetDoc=access-vba-book-recommendations
      Once you got a solid foundation on Access/VBA you should look for books that focus on writing good, readable, error proof and maintaiable code. Unfortunately, I don't know any such that is dealing with VBA.

  • @davegoodo3603
    @davegoodo3603 7 років тому

    I had no idea you could do this, thanks Phil.

  • @mjohnstone2399
    @mjohnstone2399 5 років тому +1

    Some things to add for Enum
    1) I think enum values are of data type Long which is worth noting if values require conversion to say Integer etc.
    2) Can also have an enumeration of the Enum values by adding at the end [_First] and [_Last] eg
    Public Enum CustomerSegment
    NormalCustomer = 1
    VIPCustomer = 2
    [_First] = NormalCustomer
    [_Last] = VIPCustomer
    End Enum
    Sub TestEnum
    Dim customerType As CustomerSegment
    For customerType = CustomerSegment.[_First] To CustomerSegment.[_Last]
    SendCustomerReports(customerType)
    Next
    End Sub

    • @codekabinettcomen
      @codekabinettcomen  5 років тому +1

      Thank you for 1), that's worth mentioning. - Not sure about 2), though. It's rather rare to iterate over all values of an enum. For .. Next works here by converting the first and last value to a long. That's not going to work when the enum values are NOT a gap-less sequence of integers.

    • @mjohnstone2399
      @mjohnstone2399 5 років тому

      @@codekabinettcomen Thanks for point 2) It only works for if the values declared within the Enum are sequential www.cpearson.com/excel/Enums.aspx. That makes it limiting for validation of input of enum values.
      Enjoying going through your videos some really interesting advanced topics.
      Ps Was hoping there was one on IEnumVariant

    • @codekabinettcomen
      @codekabinettcomen  5 років тому

      What exactly have you in mind with IEnumVariant? - I did nothing on that topic yet, but there is a chance that I've got something planned in that regard already.

    • @mjohnstone2399
      @mjohnstone2399 5 років тому

      @@codekabinettcomen Sounds very interesting.
      Ultimately to implement IEnumerable/ICollection/IList classes which will probably require a general-purpose IEnumVariant. Eg for extending the Dictionary project github.com/MarkJohnstoneGitHub/VBA-IDictionary, which could be used with repositories containing various list implementations that say could be used for eg a VBA JSON parser etc.
      I've done a bit of research thou most code is in VB6 and requires a "bit" of work to get running in VBA. Probably the easiest method is creating an IDL which don't have any experience thou looks reasonably simple, once you get past the learning curve. (another idea for a video creating IDL and tlb files).
      It gets complicated if attempting to do it solely in VBA when dealing with different data types i.e. Strings, Objects. It be nice to see both implementations as doing it VBA gives good insight to what is occurring under the hood.
      Eg To extend the dictionary project to make it enumerable, "likely" to require the ability to select the enumerator other than the default by key, ie. a dictionary entry (key, value pair), as the Scripting.Dictionary doesn't allow enumeration by item.
      I'll have to organize the IEnumVariant research done a few months back and being meaning to focus back on it.

  • @tdtrecordsmusic
    @tdtrecordsmusic 4 роки тому +1

    cool stuff

  • @jgojiz
    @jgojiz 4 роки тому +1

    I didn't know you could call the enum with dot syntax and then the values like in objects notation

  • @rajtorane6119
    @rajtorane6119 2 роки тому +1

    ❤️

  • @hamzaamir1218
    @hamzaamir1218 4 роки тому

    want to learn VBA and i will be seeking your support

  • @zlee4019
    @zlee4019 4 роки тому +1

    Nice videos, but I am sick of the music at the start. Please play music from Mozart or Vivaldi in future videos. Thanks.

    • @codekabinettcomen
      @codekabinettcomen  4 роки тому

      Noted. - I will think about the music. Replacing it with Vivaldi or Mozart is rather unlikely though.

    • @zlee4019
      @zlee4019 4 роки тому +2

      @@codekabinettcomen - Your videos are cool, they deserve cool music, such as: ua-cam.com/video/T22GJNt1SbE/v-deo.html

  • @walerij
    @walerij 4 роки тому

    Очень интересно. Раскрываются с точками словно как свойства.