persistent changes to variables across files
i have a few variables in my main file that im trying to read from/write to in the subforms i have. i can load the variables with the values that were set in the main file just fine across every subform, but writing to them is a different story. you can see what im trying to do here (https://github.com/svfeplvce/svedit). the specific variables im trying to read/write are curPoke, curTrainer, pokemonEdit, pokeDataEdit, plibEdit, and trainerEdit. whats supposed to happen is whenever a user switches to a different subform, the values from the last form stay in the variable. what actually happens is it keeps the changes from the first form when displaying data in the first form, although when the user tries to save, it saves the original values. what am i doing wrong here?
17 Replies
Yikes, well you've got lots of forms writing back to the same static data.
Don't know exactly what you're doing wrong... Too much code to go over since you didn't narrow down, and your explanation is still confusing to me.
Suggest you $debug
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
its not an issue i really CAN debug though, its something i dont know how to do and therefore am asking what exactly in my code i should change to make it work. and yeah im sorry i didn’t narrow it down but that’s just because my entire program does the same thing throughout every subform and every function: it looks at information set on startup/when a user edits a value, and it writes all the changes to a file when a user hits the save button. what SHOULD happen is the changes made should be persistent even if a user switches to a different subform. what ACTUALLY happens is the values are the same as when they were initialised in the “LoadJson()” function in form1.cs when the user saves or switches to a new subform. i hope that cleared it up.
You don't know how to assign value to variables? They're what you're asking about
'What actually happens is the values are the same'
What values?
i mean yes obviously i do but specifically what i am asking is how i can assign a value when the program starts, edit that value throughout different subforms in different files, and keep those changes made at save time.
Edit the same value from different forms?
Regardless, or sounds like what you want is 'temporary state'
Ie you init, you store temp changes (somewhere), then finally you save them to the destination
that sounds like what im trying to do yes
The middle bit is the same as the end bit, you just keep the temp state in a different place
That could be in a different persisted file, a different Dictionary, a different bunch of variables etc
But the pattern when editing will be like 'save change to cache for this data piece'
Then when you save proper, it's 'save cache to proper persisted location '
And you can pick whether to save the whole cache or just bits of it
just to make sure you completely understand here, the values i am trying to edit are all stored in one variable which is passed around across all my subforms and my main file. one form will edit stats while another edits a moveset, for example. what i expect to happen is if a user edits stats and switches to the “moveset” subform, then edits stuff there, the edits they made will remain and will be able to be called upon later as needed. what actually does happen is that on save time/form switch, the edited values dont persist and pretty much just revert back to their original state (to provide a specific example, if a user changed the “hp” stat of a pokemon and then switched to the moveset tab and added a new move to it, id expect the edited hp/new move to be present in the final save, but its not)
but yeah that does sound like what im trying to do
although, if im not mistaken, that is what my code is right now (i have the original state and the edited state stored inside variables initiated in the main form, the edited state is what is passed around and changed and then gets saved to a proper persisted location when the time comes for that)
i think i just may be going about caching the temp state wrong
Ok.
To be clear, the way you are doing this is not great. The data should be better encapsulated for each form, not pulled down/shared from 'main'.
That aside, how do you 'go back' to a form you have previously edited? This will probably shed light onto your particular issue
well the main form acts as a sort of hub(?) for all the other forms. theres a menu on the side that has buttons, and the buttons open up the subforms inside a panel on the main form.
Yes, specifically, in code, how do you go back from one sub form to another
Or generally re open a sub form after you previously closed it
well in the main form, theres a function that runs whenever a user presses a button that closes the current form and opens up the new one. heres the code for that
Where does 'subForm' come from?
Ie is it recently
new
d?yes
That's your problem
Once you ve made changes in a form, you need to store a reference to it and only Hide it, not close it.
Later, you would Show it to recover the form for the user. The old data (in memory) on the subform will still be there
ah i see
thank you, i believe i’ve figured out my solution and if i have any more problems with this i’ll update this
coming back to say that i did not in fact figure out a solution and that there is no good documentation to help me figure out how to implement caching and editing variable temp states that i could find, is there anything you’d recommend i look at