Dynamically Rename an Array to a string value from Another Variable Using VB 6

Posted on 2007-08-01
Last Modified: 2012-05-05
I have a vb6function that creates an array of random numbers.  I want to rename the array using a string value from another variable.  My purpose is to store an new array each time the function is run so the array does not get overwritten. I know I can pass the values to a file and name dynamicallly name the file but I don't want to write the file to disk.  Any help would be greatly appreciated.
Question by:wayneray
    LVL 92

    Expert Comment

    by:Patrick Matthews
    Hello wayneray,

    Why would it get overwritten?  What if the function, and how are you calling it?



    Author Comment

    The function is called from a question and answer program to build a random array of numbers for use in a sequence.  For example, we want to display responses to a question in random order. If there are four responses, the function populates intNewArray with 1 to 4 then randomizes the numbers. Sometimes I use the same sequence later in the program.  Other times, I need a new sequence.  I want to keep the old sequence for later use.  The function is below:

    Public Function rotate(ByVal txtVar As String, ByRef rotVarID As Long, rotType As Long, rotCount As Long) As Long

    Dim intNewArray() As Integer
    ReDim intNewArray(1 To rotCount) As Integer
    Dim intMaxIndex As Integer
    intMaxIndex = rotCount

    Dim x As Integer

    For x = 1 To rotCount

        intNewArray(x) = x
    Next x

    'Build New Array based on rotation type

    If rotType < 5 Then

            Dim i As Integer
            Dim j As Integer
            Dim tmp As Integer

            ' Randomize the array.
                For i = 1 To rotCount - 1
            ' Pick a random entry.
                    j = Int((rotCount - i + 1) * Rnd + i)

            ' Swap the numbers.
                    tmp = intNewArray(i)
                    intNewArray(i) = intNewArray(j)
                    intNewArray(j) = tmp
                  Next i
    End If

    End Function
    LVL 85

    Accepted Solution

    You could store the arrays in a Collection.

    Author Comment

    Thanks for the tip. It is what I needed.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now