create a random aray for displying a mesage in a label

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to display a differnt message evry time a button is clicked. so I set
up a aray of ms(12) so that I can asign 12 differnt strings to a labelbut I
get a error . First of all can I asign a string value to a label? Im new to
vb . I 'm still trying to shake the happits of basica or gwbasic trobo basic.
You get the Idea. So can someone get me on the right path here.
 
I want to display a differnt message evry time a button is clicked. so I set
up a aray of ms(12) so that I can asign 12 differnt strings to a labelbut I
get a error . First of all can I asign a string value to a label? Im new to
vb . I 'm still trying to shake the happits of basica or gwbasic trobo basic.
You get the Idea. So can someone get me on the right path here.

Actually, in VB.NET (and VB6 too, unless you had option base 1 set) that
would have 13 items :) But anyway...

Dim ms() as string = {"first", "second", "third", "fourth", "fifth"}
dim rand As new Random()
dim i as integer = rand.next (0, ms.getupperbound(0))

messagebox.show (ms(i))
 
Additionally to Tom's comments, you say you cannot assign to a label?, the
answer is yes

myLabel.Text = "YourString"

or

myLabel.Text = yourArray(index)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
Actually, in VB.NET (and VB6 too, unless you had option base 1 set) that
would have 13 items :) But anyway...

Dim ms() as string = {"first", "second", "third", "fourth", "fifth"}
dim rand As new Random()
dim i as integer = rand.next (0, ms.getupperbound(0))

messagebox.show (ms(i))

Just to point out one thing. The rand.next function that you show will get
a random number from 0 to 3. It does not include the upper bound. You
need to specify ms.getupperbound(0,ms.getupperbound(0) + 1).

From the docs:

Return Value
A 32-bit signed integer greater than or equal to minValue and less than
maxValue; that is, the range of return values includes minValue but not
MaxValue. If minValue equals maxValue, minValue is returned.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Just to point out one thing. The rand.next function that you show will get
a random number from 0 to 3. It does not include the upper bound. You
need to specify ms.getupperbound(0,ms.getupperbound(0) + 1).

From the docs:

Return Value
A 32-bit signed integer greater than or equal to minValue and less than
maxValue; that is, the range of return values includes minValue but not
MaxValue. If minValue equals maxValue, minValue is returned.

Yes, your correct... I always forget that. Actually, an easier
solution, would just to use ms.getupperbound(0, ms.length).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top