Data Files

  • Thread starter Thread starter kronecker
  • Start date Start date
K

kronecker

I have a long list of jokes and I wish to select them randomly from a
big file. What is teh best way to select them? Ordinary Text or XML?
Also how do I index them? I know howto read text files so I suppose I
could use a separator of some sort bewteen jokes but I am thinking
that they may all need to be read into an array first?

Thanks

K.
 
I have a long list of jokes and I wish to select them randomly from a
big file. What is teh best way to select them? Ordinary Text or XML?
Also how do I index them? I know howto read text files so I suppose I
could use a separator of some sort bewteen jokes but I am thinking
that they may all need to be read into an array first?

Thanks

K.

If you can list your jokes line by line in a text file:

You can read your text content using StreamReader,
and assign the content to a multi-line textbox:

'------Code Begins-------
Imports System.IO

Using reader As New StreamReader("c:\jokes.txt")
' I recommend setting "Wordwrap" to False for
' textbox not to force long lines to be misaligned.

TextBox1.Text = reader.ReadToEnd

End Using
'----Code Ends--------

After loading all the jokes into a textbox(even you can hide the
textbox by making its visible property to False), and now you can
select them randomly as you can use textbox's "lines" property with
Random class:

'------Code Begins-------
' Like that:
Dim rand As New Random
Dim selectedRand As String
selectedRand = TextBox1.Lines _
(rand.Next(0, TextBox1.Lines.Length))
' eg: Display random joke in MsgBox
MsgBox(selectedRand)
'-----Code Ends------

Plus, you can make the textbox read-only not to be modified by user,
and make it invisible not to be seen by user depending on your wish.

Hope this helps,

Onur Güzel
 
kimiraikkonen said:
If you can list your jokes line by line in a text file:

You can read your text content using StreamReader,
and assign the content to a multi-line textbox:

'------Code Begins-------
Imports System.IO

Using reader As New StreamReader("c:\jokes.txt")
' I recommend setting "Wordwrap" to False for
' textbox not to force long lines to be misaligned.

TextBox1.Text = reader.ReadToEnd

End Using
'----Code Ends--------

After loading all the jokes into a textbox(even you can hide the
textbox by making its visible property to False), and now you can
select them randomly as you can use textbox's "lines" property with
Random class:

'------Code Begins-------
' Like that:
Dim rand As New Random
Dim selectedRand As String
selectedRand = TextBox1.Lines _
(rand.Next(0, TextBox1.Lines.Length))
' eg: Display random joke in MsgBox
MsgBox(selectedRand)
'-----Code Ends------

Plus, you can make the textbox read-only not to be modified by user,
and make it invisible not to be seen by user depending on your wish.

Hope this helps,

Onur G�zel

Thanks, do you mean each joke per line? Otehrwise how would I know
where one joke started and the otehr ended? So just put each jok in a
very long line in the text file?

regards

K.
 
Thanks, do you mean each joke per line? Otehrwise how would I know
where one joke started and the otehr ended? So just put each jok in a
very long line in the text file?

regards

K.- Hide quoted text -

- Show quoted text -

Yes, the way i mentioned was assuming each joke was seperated line by
line in a text file. I don't know how you store your jokes so that was
just a way of doing it. Also you can use string's split method
depending how your jokes (strings) are stored and will be delimited.
http://msdn.microsoft.com/en-us/library/system.string.split.aspx

Thanks,

Onur Güzel
 
Back
Top