Visual Basic Importing and reading a random line from a csv file

AIMIWA95

Estimable
Apr 24, 2014
1
0
4,510
I hope I posted this in the correct category, I chose the best one for my problem in my opinion.

I am writing a program in visual basic which is rather simplistic for the most part, what I need it to do is to read from a csv file and then choose a line from that csv file at random. there are 40 lines and from that 40, all I need to do is randomise them and select one and output it by showing it as a label in a form.

I have imported the csv but cannot get it to randomise and output the result to a label,
any help would be greatly appreciated and I will answer any questions if I can if there's anything else you need to know.
thanks.
 

randomizer

Distinguished
Is this VB 6 or VB .NET? I'm not that familiar with VB 6 but if it's .NET you could read it line by line into a List(Of String) and then access the list by random index to retrieve the line you're after. Then just set the label's Text property to that string. You can reuse the same list if you need to retrieve another line. If you don't need to repeatedly retrieve lines then you could just iterate through each line of the file and stop when you hit the random number you've specified, avoiding the need to store the whole file in memory. With only 40 lines you could probably skip the list even with repeat access without the user noticing the relatively "significant" slowdown, but it's up to you.