Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help 1

Status
Not open for further replies.

MrMoocow

Programmer
May 19, 2001
380
0
0
US
Okay i have 9 squares and I am filling them with pictures from a picture clip control. THis code which i whipped up is supposed to randomize it.

Do Until i = 8
i = i + 1
begin:
card = Rnd2(1, 8)

If slots(card) = "picture" Then
GoTo begin
Else
pslot(card).Picture = data.GraphicCell(i)
slots(card) = "picture"
End If

Loop

But it stops responding.
Brad,
Free mp3 player,games and more.
 
My guess is that the reason it's not responding is because it's in an infinite loop. Try putting a &quot;Debug.Print slots(card)&quot; between the 'begin...GoTo begin' <shudders>. I presume Rnd2 is your own function, and is supposed to return a random number between (in this case) 1 and 8. If you're in a loop, that's where I'd start looking next - is it maybe never returning either a 1 or an 8...?

When you've fixed it, replace the 'begin:...GoTo begin' <shudders again> with this:
Do
card = Rnd2(1, 8)
Loop Until slots(card) <> &quot;picture&quot;

Or, better still, don't have a Do loop have it's exit condition dependent on a random generator at all - create a set of possible values, select randomly from them and delete them as they're are selected. That way you're sure of making a selection each time, rather than continually selecting until you hit one you haven't hit before.

gkrogers
P.S. You'll probably get more responses if you use a descriptive subject. The subject &quot;Need help&quot; is not descriptive :)
 
Oh, and you might also want to consider replacing the 'Do Until i = 8...Loop' with a 'For i = 1 to 8...Next i'.

gkrogers
 
I have a another question, and I don't want to start anew thread.

We all now that the dragdrop sub comes with these varables
Index As Integer, Source As Control, X As Single, Y As Single

Now i am moving a picture from a control array to one in the same array.

Now the index represents the source's index (source as in the thing u dragged)

Now how do I go about getting the destinations index.

Example

I drag index 1 onto index 0 so
index returns 0
what returns 1

Thanks
Brad,
Free mp3 player,games and more.
 
Ooh! A better random number generator...? Any chance of you posting the sorce code...? gkrogers
 
Its not that good but it works for me.

Function Rnd2(low As Single, high As Single) As Single
Randomize
Rnd2 = Rnd * (high - low) + low
place = Len(Rnd2) - 3
Rnd2 = Left(Rnd2, place)
End Function

There ye go. Brad,
Free mp3 player,games and more.
 
Ah, that'll be the problem - Rnd2 will never return 'high'. Why not write a TestRnd2 function which does, say, 1,000,000 calls and checks the distribution of the results.

Some other observations:
* You haven't used Option Explicit - it's a complete no-brainer to use Option Explicit in every module. You can make VB add this to every new module by setting &quot;Require Variable Declaration&quot; on in Tools|Options, on the Editor tab.
* You're using string functions to round off the result - use Int or Round instead.
gkrogers
 
This function was used when I was a complete begginer. and I don't like Option Explicit because I like using temporay varables with out declaring them...

About the random number i solved them by using 1 to 9, but that still doens't solve my question.

How do u get the index of the item being dragged.
I just got an idea though as i am typing this to set its index to a varable in its dragevent.

Any ideas?
Brad,
Free mp3 player,games and more.
 
Well, it sure ain't a better random function. If all you are trying to do is return a random number in a given range, try:
[tt]
Function Rnd2(low As Long, high As Long) As Long
Randomize
Rnd2 = Int((high * Rnd) + low)
End Function
[/tt]
This has the added advantage that there is no implicit casting going on (always dangerous when you don't realise it is happening)

And Option Explicit will save you a ton of pain in the long run that outweighs the advantage of being able to use temporary variable on the fly (presumably in the immediate window). I have a tiny module that contains public declarations for each intrinsic type for use in this sort of situations: eg StrTempImmediate as string, intTempImmediate as integer, lngTempImmediate as long, etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top