Results 1 to 9 of 9

Thread: [RESOLVED]Help with translation from .NET + explanation

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    11

    [RESOLVED]Help with translation from .NET + explanation

    Can anyone please translate this to Classic VB(5)
    VB Code:
    1. Public Sub slumpa()
    2.         j = Int(35 * Rnd() + 1)
    3.         For i = 0 To 6
    4.             Do While Array.IndexOf(lotto, j) > -1
    5.                 j = Int(35 * Rnd() + 1)
    6.             Loop
    7.             lotto(i) = j
    8.         Next i
    And can somebody perhaps explain what the 'Do While Array.IndexOf(lotto, j) > -1' part does (I know that the purpose of the code is to give unique values to the variables in the array, but I don't know what it 'does' more precisely). Thanks a lot
    Last edited by Ruckus_Gary; Nov 27th, 2006 at 11:50 AM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Help with translation from .NET + explanation

    I didn't test it so give it the try but in general code you posted generates random number, then it checks if current value already exists in the array and if not it will assign to current array item.
    "Translated" version does pretty much the same thing:
    VB Code:
    1. 'VB.Net - Array.IndexOf Method:
    2. 'Searches for the specified object and returns the index
    3. 'of the first occurrence within the entire one-dimensional
    4.  
    5. Dim i%, j%, k%
    6. Dim blnExist As Boolean
    7.  
    8.     For i = 0 To 6
    9.         blnExist = False
    10.         Do While blnExist = False
    11.             j = Int(35 * Rnd() + 1)
    12.             For k = 0 To UBound(lotto)
    13.                 If lotto(k) = j Then
    14.                     blnExist = True
    15.                     Exit For
    16.                 End If
    17.             Next k
    18.         Loop
    19.         lotto(i) = j
    20.     Next i
    21.  
    22. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    11

    Re: Help with translation from .NET + explanation

    It seems to be looping infinitely when I try it in .NET. Aslo, what does the >-1 mean?
    Last edited by Ruckus_Gary; Nov 27th, 2006 at 03:35 AM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    11

    Re: Help with translation from .NET + explanation

    Bump. I really need help with this.

  5. #5

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    11

    Re: Help with translation from .NET + explanation

    I see. But what I can't figure out is why your solution loops infinitely. Everything seems logical.

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Help with translation from .NET + explanation

    Random doesn't mean totally unique so there is agreat chance that item already exist in the array.
    What you can do is clear array before re-populating it:
    VB Code:
    1. [B]Erase lotto[/B]
    2. For i = 0 To 6
    3.     '...
    4. Next i

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    11

    Re: Help with translation from .NET + explanation

    *EDIT* I solved it
    VB Code:
    1. For i = 0 To 6
    2.             exists = False
    3.             Do While exists = False
    4.                 j = Int(35 * Rnd() + 1)
    5.                 exists = True
    6.                 For k = 0 To 6
    7.                     If array(k) = j Then
    8.                         exists = False
    9.                         Exit For
    10.                     End If
    11.                 Next k
    12.             Loop
    13.             array(i) = j
    14.         Next i
    It's currently a little un organized withe the exists variable but I'll fix it.
    Thanks a lot for the help
    Last edited by Ruckus_Gary; Nov 27th, 2006 at 11:50 AM.

  9. #9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width