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

Posted on 2007-08-01
Medium Priority
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
  • 2
LVL 93

Expert Comment

by:Patrick Matthews
ID: 19613019
Hello wayneray,

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



Author Comment

ID: 19613094
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 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 19613746
You could store the arrays in a Collection.

Author Comment

ID: 19622326
Thanks for the tip. It is what I needed.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

864 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