Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Help...Arrays


Reply
 
Thread Tools Display Modes
  #1  
Old 03-07-2004, 01:13 AM
solon solon is offline
Newcomer
 
Join Date: Mar 2004
Posts: 2
Question Help...Arrays


Here is some code that I have written, that I wish to use as a random name generator. I would like the program to pick a name at random, then remove it from the array so that it cant be chosen again until all the others have been drawn.

Private Sub Form_KeyDown(keycode As Integer, shift As Integer)
random
End Sub
Private Sub random()
Dim max As Integer
Dim term As Integer
Dim temp As Integer
Dim x(12) As Variant
x(0) = "Huy"
x(1) = "Fershad"
x(2) = "James"
x(3) = "Matthew"
x(4) = "Rudolf"
x(5) = "Tim"
x(6) = "Steve"
x(7) = "David"
x(8) = "Martin"
x(9) = "John"
x(10) = "John"
x(11) = "Daniel"
x(12) = "Nathan"

'max = (x.Count - 1)
' Could use that one!
max = (x - 1)
While (max >= 0)
term = (Rnd * max)
temp = x(max) = x(term)
x(term) = temp
max -1
Wend
End Sub

I can't work out why it won't work, but seeing as though I am a Java programmer, I really don't have much hope!

Please help me "clean up" my code!

Thanks, Solon
Reply With Quote
  #2  
Old 03-07-2004, 01:21 AM
BryanN BryanN is offline
Banned
 
Join Date: Feb 2004
Posts: 278
Default

In Form_Load, put the statement
Randomize
this iwll mkae a random number. for the names, you may want to simply use a listbox.
Reply With Quote
  #3  
Old 03-07-2004, 01:37 AM
solon solon is offline
Newcomer
 
Join Date: Mar 2004
Posts: 2
Default

I understand that, but thanksyou.

The problem lies in the line

max = (x - 1)

Well the program wont compile and i think there MAY be problems after that but it hanst picked them up yet, it is stuck on that one!

It says:

"Type Mismatch"

Thanks...
Reply With Quote
  #4  
Old 03-07-2004, 03:18 AM
thingimijig thingimijig is offline
Senior Contributor

* Expert *
 
Join Date: Jul 2003
Posts: 1,300
Default

have a look at billsoos tutorial on random numbers here

thingimijig.
Reply With Quote
  #5  
Old 03-07-2004, 05:03 AM
Mathijsken's Avatar
Mathijsken Mathijsken is offline
Contributor
 
Join Date: May 2003
Location: Oostkamp - Belgium
Posts: 730
Lightbulb

I can see that you're not used to VB at all, fyi VB doens't support short-notations like max =- 1 or other java-stuff VB is a bit lame in comparison with Java
I've re-written your code a bit:


Code:
Option Explicit
'This is a 'home-made'-type, like a string or variant. The reason i use this,
' is that i don't have to declare to arrays: Chosen() and Names(), but an array
' of this private type
Private Type Names
	 Name As String
	 Chosen As Boolean
End Type
Const NrOfNames As Integer = 12
Dim X(NrOfNames) As Names
Dim strRandom(NrOfNames) As String 'This will contain all the names in a random order.
Private Sub random()
Dim intTemp As Integer, i As Integer
	X(0).Name = "Huy"
	X(1).Name = "Fershad"
	X(2).Name = "James"
	X(3).Name = "Matthew"
	X(4).Name = "Rudolf"
	X(5).Name = "Tim"
	X(6).Name = "Steve"
	X(7).Name = "David"
	X(8).Name = "Martin"
	X(9).Name = "John"
	X(10).Name = "John"
	X(11).Name = "Daniel"
	X(12).Name = "Nathan"
	
	For i = 0 To NrOfNames
		Do While strRandom(i) = ""
	
			Randomize
			intTemp = Int(Rnd * NrOfNames)
	
			If X(intTemp).Chosen = False Then
				strRandom(i) = X(intTemp).Name
				X(intTemp).Chosen = True
			End If
			DoEvents
	   Loop
	Next i
End Sub
The idea is:
1) You loop through a new array of names (strRandom). We'll put our names, randomly selected in this one.
2) You get a random index of the original X array and check wether this one has been chosen before or not. If he has been chosen before pick another random number and another,... untill we get one that hasn't been chosen yet. Then we simply pass the found name to the new array (strRandom).

I don't know what has to happen with the names, tell me and I'l explain you how to acces the random array,or find out yourself, that's up to you I guess

Mathijsken
__________________
Fear is the first step towards intolerance.
Reply With Quote
  #6  
Old 03-07-2004, 05:35 AM
tinyjack's Avatar
tinyjack tinyjack is offline
Captain TJ

Forum Leader
* Expert *
 
Join Date: Jun 2003
Location: England
Posts: 1,664
Default

Your initial problem was that arrays in VB do not have a Count property, you need to use the UBound function, so:

Code:
max = (x.Count - 1)
 
'becomes
max = UBound(x)
HTH

TJ
__________________
Oh dear, I need a beer.
Online Motorsport Game
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creation of arrays dynamically to store data aNTi General 4 10-20-2003 08:16 AM
Can this be done (Regarding Arrays) ? jsjw Word, PowerPoint, Outlook, and Other Office Products 3 03-14-2003 06:16 AM
access info within array(s) jobrandes General 11 12-02-2002 10:16 AM
Question about arrays vstat General 2 01-20-2002 11:21 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->