Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3334716
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:56:09+00:00 2026-05-17T23:56:09+00:00

Given an enum similar to this: Friend Enum TestValue As Int32 tstNotSet = -1

  • 0

Given an enum similar to this:

Friend Enum TestValue As Int32
    tstNotSet = -1
    tstA = 0
    tstB = 1
    tstC = 2
    tstD = 3
    tstE = 4
    tstF = 5
    tstG = 6
    tstH = 7
    tstI = 8
    tstJ = 9
    tstK = 10
    tstL = 11
    tstM = 12
End Enum

And an Array similar to this:

Dim TestValues() As String = {"A", "B", "C", "D", "E", "F", "G", 
                              "H", "I", "J", "K", "L", "M"}

And a string is fed in as input in some form similar to (assume it is already stored to a variable):

Dim tmpInput As String = "ADFGHJLM"

And a Sub/Method in another arbitrary class that takes in as input an array of one of more of the Enums from TestValue, based on the input string tmpInput. Basically, I want to walk the tmpInput variable, and for each character, map out its equivalent member in the Enum, so that I can pass it to this Sub in the other object. The string array TestValues and the Enum TestValue (yes, the names could be done better, but don’t let that bother you too much) are laid out to match each other explicitly.

So I basically want to search the array for the matching letter, and use its index offset to know which Enum I want to map to that letter. My current code uses a large Select Case statement, but that’s just ugly (although, performance tests show it to be rather speedy, even in the debug build).

The purpose of this test case is to provide an example of a mechanism I use in a project I’m working on. In this project, objects have a ReadOnly property that returns a string of letters composed from TestValues. It also has a Sub that accepts an array of one or more Enums from TestValue that sets a private member in the object that is used by the aforementioned ReadOnly property. The purpose was to store an array of smaller integer values (the Enums) rather than an array of strings for the object’s internal functionality. So I had to create a way to map back and forth between the string array and the enum.

Yes, it’s easily doable with the many different collections available in .NET, but I feel those are too heavyweight for my needs, as many of my objects have enums as small as two values, hence, arrays. I borrowed the trick from a similar example used in C to be able to select a string from a const array based on an index offset.

Anyways, as I’ve discovered, searching arrays in VB.NET is not trivial. There is apparently no simple command like TestValues.GetIndex(tmp(i)), where tmp(i) is a variable holding a single character (String, not Char), that would return say, ‘8’ if tmp(i) was set to ‘I’. Instead, methods like Array.FindIndex require using delegates/predicates, something I haven’t fully wrapped my head around just yet (they seem like function pointers from C).

So what’s the best way, other than constantly looping over the array for every input character, to locate the index offset based on the stored value? Is the method I highlight sane or insane (hint: it’s a hold-over from VBA code)? Is there a more efficient way?

Oh, and yes, the ReadOnly property does check that the internal members are NOT set to tstNotSet before attempting to read from the TestValues array. That’s why that Enum member exists.

Thanks!


EDIT: Hopefully this doesn’t muddle the explanation up too much, but here’s an example, as simplified as I can get it, of how the look up currently operates using the array, enum, and input string as defined above:

    Dim n As Int32 = 0
    Dim Foobar(0 to 12) As TestValue

    For Each s As String In tmpInput
        Select Case Char.ToUpper(CChar(s))
            Case CChar(TestValues(tstA))
                Foobar(n) = tstA
                n += 1

            Case CChar(TestValues(tstB))
                Foobar(n) = tstB
                n += 1

            Case CChar(TestValues(tstC))
                Foobar(n) = tstC
                n += 1

            Case CChar(TestValues(tstD))
                Foobar(n) = tstD
                n += 1

            Case CChar(TestValues(tstE))
                Foobar(n) = tstE
                n += 1

            Case CChar(TestValues(tstF))
                Foobar(n) = tstF
                n += 1

            Case CChar(TestValues(tstG))
                Foobar(n) = tstG
                n += 1

            Case CChar(TestValues(tstH))
                Foobar(n) = tstH
                n += 1

            Case CChar(TestValues(tstI))
                Foobar(n) = tstI
                n += 1

            Case CChar(TestValues(tstJ))
                Foobar(n) = tstJ
                n += 1

            Case CChar(TestValues(tstK))
                Foobar(n) = tstK
                n += 1

            Case CChar(TestValues(tstL))
                Foobar(n) = tstL
                n += 1

            Case CChar(TestValues(tstM))
                Foobar(n) = tstM
                n += 1
        End Select
    Next

As noted in my comment to Jon Skeet, this construct, along with the rest of the Object’s components, executes 100,000 times in a profiling loop in ~570ms (rough average of 3-5 runs).

Exchanging the above construct out with a smaller Array.IndexOf construct loops 100,000 times in ~630ms (again, 3-5 runs, rough average, the whole Object). The new construct looks like this:

Dim p As Int32

p = Array.IndexOf(TestValues, s)
If p <> tstNotSet Then
    Foobar(n) = DirectCast(p, TestValue)
    n += 1
End If
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:56:09+00:00Added an answer on May 17, 2026 at 11:56 pm

    I’m afraid I found your question extremely hard to understand, but is Array.IndexOf what you’re looking for?

    Dim index = Array.IndexOf(TestValues, tmp(i))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an enum that has assigned values, what is the best way to get
Given the following java enum: public enum AgeRange { A18TO23 { public String toString()
Given a (source) patch file, what's the easiest way to apply this patch on
Given a specific DateTime value, how do I display relative time, like: 2 hours
Given a Python object of any kind, is there an easy way to get
Given a select with multiple option's in jQuery. $select = $(<select></select>); $select.append(<option>Jason</option>) //Key =
Given a DateTime representing a person's birthday, how do I calculate their age in
Given an absolute or relative path (in a Unix-like system), I would like to
Given 2 rgb colors and a rectangular area, I'd like to generate a basic
Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.