1 Attachment(s)
Opening a saved file to then put values into specific labels
Hello, I am creating a game for my final project and my teacher requires us to have a save and open dialog in the program. Well, I have the save done. Here is the code to probably help with the issue:
Code:
File.WriteAllText(SaveFileDialog.FileName, PlayerName & vbNewLine & PlayerHealth & vbNewLine & PlayerCash & vbNewLine & PlayerInfantry & vbNewLine & PlayerTanks & vbNewLine & PlayerJet & vbNewLine & PlayerMissile & vbNewLine & PlayerNuke & vbNewLine & PlayerRepairsLeft & vbNewLine & AiName & vbNewLine & AiCash & vbNewLine & AiInfantry & vbNewLine & AiTanks & vbNewLine & AiJet & vbNewLine & AiMissile & vbNewLine & AiNuke & vbNewLine & AiRepairsLeft)
That is the order. Each one represents a value. Here is an image of where those values need to go:
Attachment 143161
I do not know how to do this as I have never done it like this before. Hopefully I explained everything well. Thanks!
Re: Opening a saved file to then put values into specific labels
It would be more appropriate to put all those values into an array and then call File.WriteAllLines. Similarly, you can then call File.ReadAllLines to read the data back into an array. You can then do whatever you like with each array element.
Re: Opening a saved file to then put values into specific labels
Quote:
Originally Posted by
jmcilhinney
It would be more appropriate to put all those values into an array and then call File.WriteAllLines. Similarly, you can then call File.ReadAllLines to read the data back into an array. You can then do whatever you like with each array element.
Then I could put after the readalllines this:
lblInfantry.Text = values(1)
And it would change the current value of that label into the new one it is reading?
Re: Opening a saved file to then put values into specific labels
Why not try it and let us all know.
Re: Opening a saved file to then put values into specific labels
Excuse me, sorry for asking but it says "Can not convert from String to Encoding"
Code:
File.WriteAllLines(SaveFileDialog.FileName, ValuesString(0) & vbNewLine & values(0) & vbNewLine & values(1) & vbNewLine & values(2) & vbNewLine & values(3) & vbNewLine & values(4) & vbNewLine & values(5) & vbNewLine & values(6) & vbNewLine & values(7) & vbNewLine & ValuesString(1) & vbNewLine & values(8) & vbNewLine & values(9) & vbNewLine & values(10) & vbNewLine & values(11) & vbNewLine & values(12) & vbNewLine & values(13) & vbNewLine & values(14) & vbNewLine & values(15))
Re: Opening a saved file to then put values into specific labels
It should be more like
Code:
File.WriteAllLines(SaveFileDialog.FileName, values)
You are trying to create a big long string from your array rather than just passing the array.
1 Attachment(s)
Re: Opening a saved file to then put values into specific labels
Alright you have helped me figure out that issue. I saved it instead of .xml to .csv and will do ReadallLines for opening. BUT I new issue has occurred for only this line of code. Now, this is a label storing an Integer value in it but being converted to String like the other labels with integers. Everything else gets converted to String but this 1 line:
Code:
values(0) = frmMainGame.lblPlayerCash.Text
Now that is just another integer but here is the result I get: Attachment 143277 I have tried methods like .ToString(), Integer.Parse, Convert.ToString(code here) but none of those methods work. I do not understand why. I have researched it but nothing useful came up. This should not be happening. I am not very experienced which is why I come to the forums. I am trying to learn these issues and fix them myself but I sometimes just have no idea.
Re: Opening a saved file to then put values into specific labels
Those values exceed 2 billion I believe, and technically don't fit in an Integer.
You probably want to use a Long, aka Int64 to hold those values.
Perhaps use a Long for everything so you have a common type and handle your two worse (maximum value) cases.
Re: Opening a saved file to then put values into specific labels
So I try that method and it does not save any of the information below it. Reason if you know because I have tried fixing it multiple times.
Code:
values(0) = Int64.Parse(frmMainGame.lblPlayerCash.Text)
Re: Opening a saved file to then put values into specific labels
I guess I'm confused.
What is the values() array's type?
It seems if you are trying to save strings (writing to a file) from strings (.Text properties), then you shouldn't need any conversions at all.
The values() array should be an array of strings.
Perhaps your excel sheet capture you posted is showing Scientific (or engineering) notation for the number because your column is too narrow, or because of the format that you have specified for the cells formats large numbers that way.
It may just be a display issue, not a value type issue, i.e. it is not an error, just a different representation of the number.
Is the values array an array of String?