Every Program Is A New Language

Perhaps that’s going a bit far, but it proves a point. A less extreme comparison would be “every program has a dialect”. In any case, every app or game has a “glossary” of terms almost like any fantasy world has it’s own lexicon. To an outsider it’s almost a foreign language.

With coding, every Function becomes a word that can start a sentence. A Function’s Parameters become “words” of that sentence, especially when using Constants like Enumerators. These allow you to store a value as an Integer while treating it like a word, which is faster to a processor, but obvious to the coder.

Thanks to Enums, the number itself doesn’t matter, and can even be changed later without effort. You can store them as Strings in XML, and TryParse them back to Integers. As long as you don’t treat them as numbers, changing the numbers or their sequence shouldn’t break anything.

For instance, using the following Enum as an example, you would never want to refer to Genus.Player + 1 in order to get to Genus.Enemy, because if you ever add another entry after Genus.Player, then Genus.Player + 1 won’t be referring to Genus.Enemy any more.

    Public Enum Genus
        Player
        Enemy
        PowerUp
        Bullet
    End Enum

Similarly, you wouldn’t want to loop through a portion of the Enum, unless you have absolutely no plans of ever changing the Enum Values or their sequence. Looping through all of them is acceptable, because changes like that wouldn’t include or exclude any of them.

To get the total number of elements in an Enum, use this helper Function:

    Public Function EnumCount(EnumType As Type) As Integer
        Return [Enum].GetValues(EnumType).Length
    End Function

Because Enum is the Keyword used in order to Declare an Enum, in any other instance you have to refer to it with [Square Brackets]. You can also use System.Enum instead, if you want to avoid brackets entirely.

To use EnumCount(), call GenusCount = EnumCount(GetType(Genus)). Passing an Enum itself to a Function isn’t possible, although I was able to declare a Function with a Parameter As [Enum], but calling EnumCount(Genus) wasn’t acceptable somehow. So I guess you can’t avoid using GetType(), which is what allows you to pass an Enum to a Function as a Type Object, rather than an Enum.

Anyway, you would then call For i = 0 to GenusCount - 1 to Loop through the whole series. Of course, this will only work properly if you use the default numbering system, starting with 0 and going up with each Enum Variable. If you specify the Values of the Enum Variables, the numbers won’t line up for a loop.

    For Each This As Genus In [Enum].GetValues(GetType(Genus))
        Console.WriteLine($"{This.ToString} = {Int(This)}")
    Next

As you can tell, this lists off the Values in the Enum, using an Interpolated String to print [Name] = # for each one. The Int() Function casts the Enum so that it prints properly, but when you use If This = Genus.Player you don’t need to cast to Interger.

Leave a Reply