Enumerations to Array of (Type) Functions

Element6

Well-known member
Joined
Feb 16, 2010
Messages
85
Programming Experience
5-10
Just a random passing of information because I found the research for this to be complicated; so I figured I would save some other developers the trouble of figureing out how.

Even though it's possible to derive; I would be inclined to recommend that Microsoft make this into a .ToArray function for enummerations just to make it easier to do because I use this style of coding commonly.

I usually always use enummerations as a pass ID of some type with a calculated value using the n^2 notation to calculate the return values. It's easy to derive with squareroots what values are derived from a sum; but most of the time I use the Identifier String to denote a combo box drop down list view. I think this is fairly routine for most developers; since in all my coding or rework I have seen it used commonly by other developers also.

Anyway, I wanted to do some sort of comparisson array of strings to fill the combobox using the *.Items.AddRange() function and it requires an array pass (this typically shrinks the code down considerably) and if you want to mix and match arrays in segments it's easier. So here is the code; for doing so I am assuming that other developers use an "AddToArray" type function like me; if not you have to write the function or just replace the code with the actual adding to array.

Good luck, and I hope this limits your research time; if I had this it would have saved me a few hours.


VB.NET:
        ''' <summary>
        ''' Convert an enummeration to an array.
        ''' </summary>
        ''' <param name="ConvertTo">Convert to return type.</param>
        Public Shared Function EnumToArray(Of ArbitraryEnum)(ByVal ConvertTo As ConvertEnumToArrayOf)
            Dim returnValue As Object = Nothing

            Select Case ConvertTo
                Case ConvertEnumToArrayOf.AsString
                    Dim Values() As String = Nothing
                    For Each Enumeration In System.Enum.GetNames(GetType(ArbitraryEnum))
                        AddToArray(Values, Enumeration)
                    Next
                    Return Values
                Case ConvertEnumToArrayOf.AsInteger
                    Dim Values() As Integer = Nothing
                    For Each Enumeration In System.Enum.GetValues(GetType(ArbitraryEnum))
                        AddToArray(Values, Enumeration)
                    Next
                    Return Values
                Case ConvertEnumToArrayOf.AsEnumeration
                    Dim Values() As Object = Nothing
                    For Each Enumeration In System.Enum.GetValues(GetType(ArbitraryEnum))
                        AddToArray(Values, Enumeration)
                    Next
                    Return Values
            End Select

            Return returnValue
        End Function
 
Obviously; I use them.

Even though it's possible to derive; I would be inclined to recommend that Microsoft make this into a .ToArray function for enummerations just to make it easier to do because I use this style of coding commonly.

I keep forgetting to "prerequesit" that I am developing on multiple platforms; so I always develop according to JAVA or OLD C++ standards, .NET has a lot of cool functionality and built in array functions. But for the actual IT developers I am sure, they are not 100% Microsoft; so they need to port to SQL or JAVA or C++ or Pascal or whatever sometimes it's in house standards; their own languages; and the functions don't translate 100%.

I like .NET if the universe was 100% Microsoft it would be easy; but it's not.

I use wrappers to indicate where functionality is to be changed. I could have done x.GetValues or x.GetNames return; but in my other standards I have to do the for loop to get the functional code; so I use them as a base; in this case I need to translate to JavaScript I think or maybe it's going through an ASM reprocess; and I want to minimize the rewrite; so I just put it in the .NET code. I don't use .NET for anything other then RAD development. I do not use it for actual deployment and probably never will.

And the idea is that eventually I will fill the enummerated array with both values and String values then I'll write a generic ToString function or ToValue function. I am writeing a framework which is pseudo .NET style and I have to be careful to make sure it is portable to multiple languages. This is very common in the industry.

If your wondering why, I am sure you are. It's security related; in order to insure that hackers can't hack a system I use a proprietary backend compiler; that has multiple different standards - these standards translate at random intervals to different coding standards; My outputs are actually a trade secret and what I work from.

The Inputs I can use multiple coding styles including .NET but the actualy syntax is 100% translated into my own "hidden" language; which in turn has multiple different derived formats; and is established by a random generic key; which is created and assigned periodically; similar to the "DataKeys" that people use to secure servers.

The language is mathematical, so it can adjust very easily like LISP and uses fractals to base and derive it's syntax symbol maps. I do not use Base2 format; in fact I use Base2-Base99 Format which is completely insane; but it works so I don't complain and it's random intervaled at what times it's used and what times it's not.

So from a "Programmers" point of view. Yes, it's retarded and I agree. From a systems level. Makes complete sense. I'm a systems engineer not a programmer. Programming is for 12 year olds who want to learn to talk to a computer. Systems Engineering is for adults who want to keep the 12 years from messing up their computers :)
 
Last edited:
Obviously; I use them.
So you do. I didn't notice because they are hidden in so much redundant code. There are a number of issues with that code, not least of which the fact that it's completely redundant.
VB.NET:
Dim strings As String() = [Enum].GetNames(GetType(MessageBoxButtons))
Dim integers As Integer() = DirectCast([Enum].GetValues(GetType(MessageBoxButtons)), Integer())
Dim values As MessageBoxButtons() = DirectCast([Enum].GetValues(GetType(MessageBoxButtons)), MessageBoxButtons())
 
Now that I've read a bit more of your needlessly wordy post I see more of where you're coming from. That said, the vast majority of people who write VB.NET write it because they want VB.NET code. Writing a long, inefficient, unsafe method to do something that can be done in one line isn't going to save you from any 12 year olds.
 
*sigh* Again I will reitterate; this code gets translated outside the .NET environment; and the intent was for developers who use other platforms then just .NET.

The function is .NET specific; it doesn't exist in other languages.

I like the fact that it was there. It's very nice, and I stated that it was possible to derive in a single thread. But, as I said other developers will be finding the ToArray function easier to deall with.

.ToArray is just as easy as

VB.NET:
Dim SomeEnumerationArray as String = [SomeEnum].GetValues(GetType(SomeEnum))

However in my universe; which is obviously different then MicroSoft; which was the explicit statement. I cannot use .NET functions directly; that doesn't mean I don't need to use .NET it means I have to be able to pass the functions; and the method isn't unsafe. It's guarented and perfectly safe.

I don't view the universe from an SQL base platform. It's a smart idea; and works well; take it as a compliment. The function needs to be recreated on my platform; which is proprietary and different.
 
It does; again your not reading the thread, and after this I am leaving it alone.

In the real world; where microsoft doesn't rule; people use multiple languages, and in most cases they are using one principal language and multiple sub languages but cannot translate it to say "PHP" or "JAVA" or some other language.

Developers, who work on integration platforms will find the information useful because it will help them determine what is doing what; and they will write code to rework the ".NET" code. 9 times out of 10 developers in the world of reality and not microsoft dream world; have to do look up for code functions that are not common to general programming.

I am positive without a doubt some guy/gal will quick view through this thread on their search to figure out how MicroSoft .NET does something and compare it to their language and get a lot of information out about .NET functionality. Especially if they are new to .NET development and are coming from a C++ or ASM background and think in Arrays.
 
The intent of the code was to be used to write a would be ".ToArray()" jmcilhinney.

I know that it is inefficent because it's not taking advantage of the straight passing of the return from the GetNames/GetValues functions I stated this earlier.

If you don't know why I am doing this it's going to look stupid. I understand this. But I'm not going to give away the back end design of my Framework to satisify your curiosity. I will say that the syntax is serialized; and runtime created. Most of the underground non-mainstream developers don't use MS standards just an FYI and if you don't know this; your going to be clueless as to what we do.

So to clue you in, later I will replace the .NET functionality for

System.Enum.GetNames(GetType(ArbitraryEnum))
System.Enum.GetValues(GetType(ArbitraryEnum))

To remove enummeration referencing; because in JAVA enummerations are handled differently see Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects) . and I like generic portable code, but for now; .NET is ok just some little tweaks here and there.
 
Back
Top