StreamReader help

ARC

Well-known member
Joined
Sep 9, 2006
Messages
63
Location
Minnesota
Programming Experience
Beginner
In VS, i added an item (bankroll.txt) and there's only 1 line of text on it:

2000


And I'm trying to assing that number (the only line in the text document) to a Public TotalMoney
But when I run it, TotalMoney stays at 0


VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
CurrentBet = 0
 
Dim ammount As String
Dim bankrollStreamReader As IO.StreamReader
 
If IO.File.Exists("bankroll.txt") Then
     bankrollStreamReader = IO.File.OpenText("bankroll.txt")
     ammount = bankrollStreamReader.ReadLine()
     TotalMoney = CStr(ammount)
     bankrollStreamReader.Close()
End If
 
Me.moneylabel.Text = "Total Money: $" + CStr(TotalMoney)
 
End Sub

::EDIT:: I noticed I dont have a catch for if it cant find the text document... so I took out the IfThen's and just left the instructions in... so it wasnt checking to see if the file existed first... and it gave me the following error at runtime:
VB.NET:
Could not find file 'D:\Data\Visual Studio 2005\Projects\SingleGambit\SingleGambit\bin\Release\bankroll.txt'.
 
Last edited:
first off i see that you're declaring amount as a string, reading a value from the file as a string, then converting amount to a string (which it already was one) and setting TotalMoney to that amount, why not just do this:
TotalMoney= bankrollStreamRead.ReadLine()

also th reason it's not finding the file is because when you declare the StreamReader you're only providing a filename and extension, not the full path to where the file actually is, so it looks in the folder the exe file is running in which is: 'D:\Data\Visual Studio 2005\Projects\SingleGambit\SingleGambit\bin\Release\' so unless you move the txt file to that folder or you specify the folder path when you declare the StreamReader, it wont work
 
Hmm

ok so then after I compile it, will the txt file be in the appropriate place when the streamreader tries to open it? or should I use ApendText first to create one the very first time it's run?

I just need to store that one number, so the user can return with the same TotalMoney that they quit with in the previous session. right now, I have that set as:

VB.NET:
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitProgram.Click
Dim bankrollStreamWriter As IO.StreamWriter
bankrollStreamWriter = IO.File.CreateText("bankroll.txt")
bankrollStreamWriter.WriteLine(TotalMoney)
bankrollStreamWriter.Close()
Me.Close()
End Sub

Where button5 is the Exit button on the form. Is there an easier way to save that number than the sequential access files?
 
Last edited:
i would say that you're using the easiest way, if you didnt want to save it as a text file you could save it as an XML file, but that might be a little overkill in this case
 
Its quite a common thing.. Do this:

On the project menu
Go to Proeprties
CLick the Settings tab

Put some values in here. Make sure that "Save My.Settings on shutdown" is ticked elsewhere in the properties

Now you can say My.Settings.SomePref = "this is my saved pref"
Quit the app, restart it and My.Settings.SOmePref is still that saved value..

i assumed thiis is why you want to save a value in a text file...
 
It's a simple card game app. the totalmoney variable is defult at 2000 when it's first ran. Onclose, the current totalmoney variable should be saved somehow so the next time it's ran, that defult variable of 2000 has been changed to whatever it was = to the last time it was closed.
 
That sounds like the perfect thing for a user setting, you can even make it an Integer.
 
Project menu
<project name> Properties
Settings tab
Add setting: TotalMoney, User scope, Integer type, value = 2000
Application tab
Save my settings on shutdown

Now, just use My.Settings.TotalMoney like any other variable. Its value will be remembered between program restarts. I've done a movie somewhere here of how you can even attach the setting to a control so that the control (it was a checkbox but textbox would work too) is always the same value when restarting that it was when you shut down..
 
And is it a good idea to still keep another variable for the TotalMoney to keep calling? or is it safe to call and change the My.Settings.TotalMoney a lot?

::EDIT::

Ok, that was kind of a dumb question, lemme rephrase it... Is it ok to do:

VB.NET:
WinBet()
...
...
TotalMoney += CurrentBet
My.Settings.TotalMoneySetting = TotalMoney
 
End Sub
 
Last edited:
And is it a good idea to still keep another variable for the TotalMoney to keep calling? or is it safe to call and change the My.Settings.TotalMoney a lot?

There is no significant performance gain to using a cached local var in an app where the most amount of time is spent waiting for user input.

So long as you dont change TotalMoney millions of times in a loop, just use it stright out of the settings.
 
Back
Top