Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Array name is in a variable

Array name is in a variable

Scheduled Pinned Locked Moved .NET (Core and Framework)
data-structuresbeta-testinghelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    peacefulmember
    wrote on last edited by
    #1

    I have some arrays declared as, Dim upperCases() As String = {"ALPHA", "BETA", "GAMMA"} Dim lowerCases() As String = {"alpha", "beta","gamma" } ...many more arrays I need to read appropriate array based on the parameter passed to the function Function getText(ByVal pos As Integer, ByVal arrayName As String) As String 'I want to do something like Return arrayName(pos) End Function Could anybody help me please?

    D 1 Reply Last reply
    0
    • P peacefulmember

      I have some arrays declared as, Dim upperCases() As String = {"ALPHA", "BETA", "GAMMA"} Dim lowerCases() As String = {"alpha", "beta","gamma" } ...many more arrays I need to read appropriate array based on the parameter passed to the function Function getText(ByVal pos As Integer, ByVal arrayName As String) As String 'I want to do something like Return arrayName(pos) End Function Could anybody help me please?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      If you're looking at doing something like this, you most certainly don't need it. If your code is smart enough to pass in the correct array name and element number, it's smart enough to just do Dim str As String = upperCases(x) without going to a function to do it. You cannot do what you want, how you to do it. It would take using Reflection methods to do something like this. It would also be a very expensive operation to return a value, so if performance is an issue, this isn't they way to do it. You'd have to do something like this:

      Public Function getText(ByVal pos As Integer, ByVal arrayName As String) As String
          Select Case arrayName
              Case "upperCases"
                  Return upperCases(pos)
      
              Case "lowerCases"
                  Return lowerCases(pos)
      
              Case Else
                  Throw New ArgumentException("Invalid array name passed!")
          End Select
      End Function
      

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      P 1 Reply Last reply
      0
      • D Dave Kreskowiak

        If you're looking at doing something like this, you most certainly don't need it. If your code is smart enough to pass in the correct array name and element number, it's smart enough to just do Dim str As String = upperCases(x) without going to a function to do it. You cannot do what you want, how you to do it. It would take using Reflection methods to do something like this. It would also be a very expensive operation to return a value, so if performance is an issue, this isn't they way to do it. You'd have to do something like this:

        Public Function getText(ByVal pos As Integer, ByVal arrayName As String) As String
            Select Case arrayName
                Case "upperCases"
                    Return upperCases(pos)
        
                Case "lowerCases"
                    Return lowerCases(pos)
        
                Case Else
                    Throw New ArgumentException("Invalid array name passed!")
            End Select
        End Function
        

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        P Offline
        P Offline
        peacefulmember
        wrote on last edited by
        #3

        Dave, Thank you for reply. When you say to do something like, Dim str As String = upperCases(x) That is what I am tring to do, which does not work. To make sure that prog pass the correct array name I have created an Enum that has all the array names, which I access as Enum AllArrays EmployeeGrade EmployeeDepartment EmployeePrivileges EmployeeSlab EmployeeAgeGroup End Enum Dim arrNames As String() = System.Enum.GetNames(GetType(AllArrays)) Dim readArray As String = colorNames(AllArrays.EmployeeDepartment) I get the correct array name to access in readArray. Now, when I try to read it using readArray(0), It returns the first character of array name i.e. "E". readArray(1) returns "m" and so on. I understand it is happening because readArray is a type string, but is there a way to do what I want to do, other than using select case or If..Else. Thanks again.

        D 1 Reply Last reply
        0
        • P peacefulmember

          Dave, Thank you for reply. When you say to do something like, Dim str As String = upperCases(x) That is what I am tring to do, which does not work. To make sure that prog pass the correct array name I have created an Enum that has all the array names, which I access as Enum AllArrays EmployeeGrade EmployeeDepartment EmployeePrivileges EmployeeSlab EmployeeAgeGroup End Enum Dim arrNames As String() = System.Enum.GetNames(GetType(AllArrays)) Dim readArray As String = colorNames(AllArrays.EmployeeDepartment) I get the correct array name to access in readArray. Now, when I try to read it using readArray(0), It returns the first character of array name i.e. "E". readArray(1) returns "m" and so on. I understand it is happening because readArray is a type string, but is there a way to do what I want to do, other than using select case or If..Else. Thanks again.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          peacefulmember wrote:

          but is there a way to do what I want to do,

          I already told you, no.

          peacefulmember wrote:

          other than using select case or If..Else.

          The only other method is to use Reflection, but it's incredibly slow. You really need to rethink this design. You're treating variable names as an integral part of the Data and Business Logic, which you should NEVER do, nor do you ever need to do it in a proper design. You're combining functions that really should be seperated. You're needlessly trying to get a single function to return a value from a number of arrays when this is never necessary. If you want functionality like this, put your data into DataTables.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups