Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Putting values into an array?

Posted on 1999-01-14
11
Medium Priority
?
188 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 60 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

618 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