Solved

Putting values into an array?

Posted on 1999-01-14
11
185 Views
Last Modified: 2010-05-03
I have a loop statement which generates 5 random numbers and puts them into a variable called "value". Now how would i go about putting each value after generated into an array.
After each value goes into the array i would like to perform a check to see if that number already exists in the array.

Ive never worked with arrays - so i really need some help!

thanks
0
Comment
Question by:sheets1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 10

Accepted Solution

by:
caraf_g earned 30 total points
ID: 1456162
1 fill the array with values that you can be sure will not be generated in your random generator. For example, if your random generator generates numbers from 0 to 4, fill the array with -1s.

2 Declare a "flag" field (for instance a boolean) and set it to False to indicate that no duplicate was found

3 Every time you generate a random number, use a For.. Next loop to loop through all the array elements. If any of them is equal to the one you've just generated, change the flag field to True

4 If the flag field is still False after you've gone through the loop, add the generated value to the array.

Important:
Always re-set the flag field before you enter the For.. Next loop, otherwise it might still be True from the previous time.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1456163
PS - other experts: the solution is far from elegant and definitely not efficient but I think sheet1 is a new VB user (I hope I'm not insulting you now sheet1!) and I believe that this way of doing it is easy and a good exercise.
0
 
LVL 2

Expert Comment

by:cartti
ID: 1456164
An array is like a set of variables with the same name but referred to by an index.

With your example, to create an array do the following at the top of the sub procedure:

Dim my_var(1 To 5)

There are a few are other variations on declaring an array, but this one is suficient. To input a value to an array do the following:

my_var(1) = CInt(Rnd * 100)

Replace my_var(1) with my_var(counter)           This is where counter is the counting variable in a loop. This would give you the option of not having to display 5 similar lines of code when one will suffice.

To check a value with one in an array, do an If statement to test for your conditions. E.g.

If my_var(counter) = compare_value Then msgbox "compare_value is equal to my_var(counter)"

I post this as a comment rather than an answer because I think there is a better way of looking up a value in an array. I think this can be done using collections.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 2

Expert Comment

by:cartti
ID: 1456165
caraf_g,

In your hasteness to answer the question, you forgot about the definition of an array. See my last paragraph. Do you know a way ?
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1456166
Hello cartii,

Sorry about the haste but I'm actually teaching at the moment so I have to do this in my breaks. But yes, you're right. It can be done with collections.

Dim objCollection As Collection

Set objCollection = New Collection

Dim Value As Integer

'Let's assume you've calculated a new value with your Random Generator

'We'll try to add the new value to the Collection. But it may already be in there. To allow for this, we define a key for each collection element by using CStr on the Value. Then, if the value is already in the collection, the add will fail so we'll have to monitor for this:
On Error Resume Next
Err.Clear
objCollection.Add Value, CStr(Value)
If Err.Number = 457 Then
    'Value already in the collection
    'Do something to re-calculate Value for example and try again.
End If

'If you want to limit yourself to 5 entries:
If objCollection.Count = 5 Then
    'Hurray - Done!
End If

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1456167
Oh... PS - After the check If Err.Number = 457 you should really re-set the On Error:
On Error Goto 0
So that any other error will cause a normal "crash". Otherwise, errors might go unnoticed.
0
 

Author Comment

by:sheets1
ID: 1456168
caraf_g,

youre array steps seems sensible but i need help (code) on:

-how to add values to an array
-how i actually do loop through and check values in an array.

thanks
0
 

Author Comment

by:sheets1
ID: 1456169
caraf_g,

youre array steps seems sensible but i need help (code) on:

-how to add values to an array
-how i actually do loop through and check values in an array.

thanks
0
 

Author Comment

by:sheets1
ID: 1456170
caraf_g,

youre array steps seems sensible but i need help (code) on:

-how to add values to an array
-how i actually do loop through and check values in an array.

thanks
0
 

Author Comment

by:sheets1
ID: 1456171
caraf_g,

youre array steps seems sensible but i need help (code) on:

-how to add values to an array
-how i actually do loop through and check values in an array.

thanks
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1456172
To define an Array with 5 elements of Integers

Dim ArrayName(1 to 5) As Integer

To loop through this array
Dim intCounter As Integer

For intCounter = 1 To 5
    'Do something with array element number intCounter
Next

To do something with an individual element of the array
(Examples)
ArrayName(intCounter) = 4
MyVariable = ArrayName(intCounter)
If ArrayName(intCounter) = 3 Then
    'Do something
End If

The Array will be defined with 5 elements from the start, so you don't need to "add" values to it.

If you must, however you can always

Dim ArrayName() As Integer

Then let's say your array has already got 5 elements (indices 1 to 5) and you want to add a sixth one:

ReDim Preserve ArrayName(1 To 6) As Integer

The Preserve keyword makes sure you don't lose the elements 1 to 5 that you have already got.

Now - off you go and work out how to piece all of this together.

Good luck!

PS - I'm teaching people to code as one of the things I do for a living so I hate giving people pre-cooked solutions. You have to have something left to work out for yourself.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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 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…

738 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