# Visual Basic help

**URL:** <https://boards.straightdope.com/t/visual-basic-help/218772>\
**Category:** Factual Questions\
**Created:** [December 13, 2003, 12:54pm UTC](https://boards.straightdope.com/t/visual-basic-help/218772 "2003-12-13T12:54:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beastal](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@Beastal](https://boards.straightdope.com/u/Beastal)\
**Post date:** [December 13, 2003, 12:54pm UTC](https://boards.straightdope.com/t/visual-basic-help/218772/1 "2003-12-13T12:54:38Z")

</div>

Hello everyone.

I am trying to write a program in Visual Basic (the program is the game of blackjack, to be precise), and am having trouble finding a way to code the program so that once a card is removed from the deck, it will not reappear until the next shuffle. I downloaded a tutorial that suggested using _collections_ to do this… I have only just started learning Visual Basic and have had no exposure with collections until now.

The tutorial I downloaded gave the following example of using collections to randomize a list of items, and then selecting them, making sure there are no repeats. In this example, there are 6 names, instead of a deck of cards being used.

> [@](#):
>
> Private Sub Command1\_Click()  
> Dim colNames As Collection  
> Dim colJumbled As Collection  
> Dim i As Integer
> 
> Set colNames = New Collection  
> Set colJumbled = New Collection
> 
> colNames.Add “James”  
> colNames.Add “John”  
> colNames.Add “Karin”  
> colNames.Add “Natasha”  
> colNames.Add “Susan”  
> colNames.Add “Sven”
> 
> 'Continue until there are no more  
> ’ entries left in the original list.  
> Do While colNames.Count \> 0  
> 'Randomly select a name from colNames.  
> i = Rand(1, colNames.Count)

The program halts once it gets to that last line of code, and says:

> [@](#):
>
> Compile error:
> 
> Sub or function not defined

… whilst highlingting “Rand” as the culprit.

What’s the problem?

I am still only in the beginner’s learning phase of Visual Basic, so please keep any responses as lay as possible ;).

Thank you.

---

<div class="post-metadata">

**Author:** ![Carcosa](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Carcosa](https://boards.straightdope.com/u/Carcosa)\
**Post date:** [December 13, 2003, 1:02pm UTC](https://boards.straightdope.com/t/visual-basic-help/218772/2 "2003-12-13T13:02:18Z")

</div>

> [@](#):
>
> \*Originally posted by Beastal \*  
> \*\*:  
> … whilst highlingting “Rand” as the culprit.  
> \*\*

Change “Rand” to “Rnd”.

---

<div class="post-metadata">

**Author:** ![Beastal](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@Beastal](https://boards.straightdope.com/u/Beastal)\
**Post date:** [December 13, 2003, 1:07pm UTC](https://boards.straightdope.com/t/visual-basic-help/218772/3 "2003-12-13T13:07:59Z")

</div>

Tried that already…

---

<div class="post-metadata">

**Author:** ![Armilla](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Armilla](https://boards.straightdope.com/u/Armilla)\
**Post date:** [December 13, 2003, 1:08pm UTC](https://boards.straightdope.com/t/visual-basic-help/218772/4 "2003-12-13T13:08:24Z")

</div>

The “Rand” function must be defined elsewhere in the program - there’s no such function in VB. It looks like the example is incomplete.

Replacing “Rand” with “Rnd” won’t do it. Rnd only takes one (optional) parameter, not two.

---

<div class="post-metadata">

**Author:** ![Armilla](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Armilla](https://boards.straightdope.com/u/Armilla)\
**Post date:** [December 13, 2003, 1:14pm UTC](https://boards.straightdope.com/t/visual-basic-help/218772/5 "2003-12-13T13:14:32Z")

</div>

Try adding this function:

```auto

Private Function Rand(lLBound As Long, lUBound As Long) As Long
Rand = Int((lUBound + 1 - lLBound) * Rnd) + lLBound
End Function

```

---

<div class="post-metadata">

**Author:** ![Carcosa](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@Carcosa](https://boards.straightdope.com/u/Carcosa)\
**Post date:** [December 13, 2003, 1:14pm UTC](https://boards.straightdope.com/t/visual-basic-help/218772/6 "2003-12-13T13:14:51Z")

</div>

> [@](#):
>
> \*Originally posted by Armilla \*  
> \*\*The “Rand” function must be defined elsewhere in the program - there’s no such function in VB. It looks like the example is incomplete.
> 
> Replacing “Rand” with “Rnd” won’t do it. Rnd only takes one (optional) parameter, not two. \*\*

Good point.

Ok. What Armilla said. Try the function here:

[URL=http://www.vbexplorer.com/VBExplorer/random/random\_numbers\_1.asp]

---

<div class="post-metadata">

**Author:** ![Beastal](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@Beastal](https://boards.straightdope.com/u/Beastal)\
**Post date:** [December 13, 2003, 1:35pm UTC](https://boards.straightdope.com/t/visual-basic-help/218772/7 "2003-12-13T13:35:38Z")

</div>

Ok, inserting Armilla’s code got it past that line, but the program stops again at…

> [@](#):
>
> i = Rand(1, colNames.Count)
> 
> ```
> 'Copy the name into colJumbled.
> colJumbled.Add colNames(i)
> 
> 'Remove the name from the original list
> ' so that it won't be selected again.
> colNames.Remove i
> 
> ```
> 
> Loop
> 
> 'Print the jumbled list onto the form.  
> Me.Cls  
> For i = 1 To colJumbled.Count  
> Me.Print colJumbled(i)  
> Next
> 
> 'Invite the first two in the jumbled list.  
> MsgBox "Invite " & colJumbled(1) & " and " & colJumbled(2)

It stops again at “Me.Cls”. (I have printed the rest of the code).

What it’s trying to do is display the new “jumbled” list of names on the form. “Me” normally means the active form, right? And Cls means clear?

Either way, it’s highlighting Me.Cls and saying, once again:

> [@](#):
>
> Compile error:
> 
> Sub or function not defined

Some more assistance here would be very appriciated.

---

<div class="post-metadata">

**Author:** ![Beastal](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@Beastal](https://boards.straightdope.com/u/Beastal)\
**Post date:** [December 13, 2003, 1:37pm UTC](https://boards.straightdope.com/t/visual-basic-help/218772/8 "2003-12-13T13:37:54Z")

</div>

And also appreciated.

---

<div class="post-metadata">

**Author:** ![Armilla](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Armilla](https://boards.straightdope.com/u/Armilla)\
**Post date:** [December 13, 2003, 1:47pm UTC](https://boards.straightdope.com/t/visual-basic-help/218772/9 "2003-12-13T13:47:23Z")

</div>

Is the code actually running in a Form, or is it somewhere else (like a class or module)?

The Me object is only available to code that is defined in a Form, not elsewhere (well…it’s available in classes, which Forms sorta are, but let’s not muddy the issue). “Me.Cls” should be valid for any code running in a Form.
