Question:
visual basic .net help with substring method?
anonymous
2009-04-12 13:06:42 UTC
for my "who wants to be a millionaire" application I'm trying to get vb to open a file like "millionairequestions.txt" or at least find it, and then read the sentences or words contained in that file. in other words i'm trying to import questions and putting them into a textbox or something (i have vb 2005 express). how do i use the substring method to read the information and stuff?

i tried something like

"
dim question as string
const millionairefile as string = "millionairefile.txt"
my.computer.filesystem.readall(millionairefile)
question = millionairefile.substring(0, 49)
"

(btw question no. 1 is in line 1 of the txt file and spans 49 characters, starting at position 0)
Three answers:
Lost?
2009-04-12 17:01:11 UTC
You'll probably find that opening the file with a StreamReader and using the ReadLine method will be much easier, you will have to add import System.IO first of course.





Dim sr as New StreamReader("C:\millionairefile.txt")

Dim question as String = sr.ReadLine()



then when you want the next question you just do



question = sr.ReadLine()



if you reach the end of the file question will be nothing, so you can do



"If question is not nothing then"



But I would definately recommend parsing your whole text file using a loop and put the values into an array so you can do some randomisation.
?
2016-10-18 12:58:52 UTC
Algorithms are algorithms, however the main significant difference is the reality that VB.internet is an journey pushed 4GL. you would be able to desire to become familiar with the IDE and the form pushed programming variety. image interfaces are worry-free to construct in VB.internet (& C#) yet of direction you nevertheless could desire to income the thank you to pass approximately it. exhibit variants of MS languages are available as loose downloads from the MS web site. Grap VB.internet and proceed for the period of the countless introductory tutes.
Dr. Dan
2009-04-12 13:47:29 UTC
Dim question As String

question = My.Computer.FileSystem.ReadAllText("C:\millionairefile.txt")

MsgBox(question)





That would put your questions in a msg box so basically instead of that you could make a button perform



Dim question As String

question = My.Computer.FileSystem.ReadAllText("C:\millionairefile.txt

Label1.caption(question)







the files that cut off in this paragraph were C:\millionairefile.txt



My.Computer.FileSystem.ReadAllText


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...