Results 1 to 6 of 6

Thread: Setting up StreamReader to use 3 different text files

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2013
    Location
    Grants Pass, OR
    Posts
    3

    Setting up StreamReader to use 3 different text files

    I need advice setting up StreamReader.
    This is for a game. 3 radio buttons are supposed to control access to 3 text files (difficulty level).
    I can get it to work for one. I declare (Dim) a new StreamReader and then open the file.
    But if I try to Dim inside an "If, Them" statement to make a choice based on the radio buttons, I get an error because 'SR System' is 'Friend.

    Could anyone suggest a basic organization for this set-up?

    I have a loop for loading the text from the file into an array and choosing a random word. I just want to be able to specify any one of the three text files to run in the loop (one variable).

    Thanks!

  2. #2
    Member
    Join Date
    Nov 2013
    Posts
    50

    Re: Setting up StreamReader to use 3 different text files

    Can you post some of your code?

    Thanks,

    Paul
    Please mark your threads Resolved if your thread has been answered..And don't forget to rate the post if the answer has helped you.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Setting up StreamReader to use 3 different text files

    LEt's say you have two cars... a VW and a Subaru... Let's say you're sitting in the VW, when you go to make a call on your cell... only, you left your cell in the Subaru... it can't be in the Subaru and let you make a call from the VW at the same time.

    Same thing here... when you dim a variable inside the If statement, it's only good inside that block (the Subaru)... you can't use variables declared in the If statement outside of the block (while sitting in the VW)... the scope of any variable is defined by the block in which it is defined... be it class, module, sub, loop, conditional, what ever....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Setting up StreamReader to use 3 different text files

    As tg suggests, you can't declare the variable inside the If block because then it doesn't exist outside the If block. The alternative is outside the If block. You have two choices:

    1. Declare the StreamReader variable first but do not create a StreamReader object. Use an If...ElseIf statement to determine which RadioButton is checked and then create a StreamReader object inside each block using the appropriate file name and assign them to the variable. You can then use the StreamReader after the End If line.

    2. Declare a String variable first and assign the appropriate file name to it inside the If...ElseIf statement. After the End If line, declare and create the StreamReader and pass it the String containing the file name.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2013
    Location
    Grants Pass, OR
    Posts
    3

    Re: Setting up StreamReader to use 3 different text files

    Thanks jmcilhinney and tg,

    Here is some code (as screenshots):

    one shows declaring the StreamReader with no StreamReader Object (which I had)

    one shows if elseif block (which I had) - I'm not sure about the object on these lines

    one shows the radio button code from the other (non-main) form I know I didn't have to use variables, but I was trying different things

    With this set-up, I can play the game, but it doesn't always use the text file I choose. Is there something I need to clear out between games?

    Thank you for all your help,

    Gordon




    Name:  Screen Shot 2013-12-11 at 6.45.17 PM.png
Views: 371
Size:  7.8 KBName:  Screen Shot 2013-12-11 at 6.45.43 PM.png
Views: 307
Size:  26.7 KBName:  Screen Shot 2013-12-11 at 6.46.19 PM.png
Views: 372
Size:  21.2 KB

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Setting up StreamReader to use 3 different text files

    You should debug your code. Don't just read the code; watch it in action. Place a breakpoint at the top of a section of code using F9 and then, when execution breaks, you can step through the code line by line to see exactly where it goes and you can evaluate all your variables and whatever else you like at each step. That way you don;t have to guess what's happening; you can see it happen.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width