C
C#2y ago
nox

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
phaseshift
phaseshift2y ago
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
MODiX
MODiX2y ago
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.
nox
noxOP2y ago
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.
phaseshift
phaseshift2y ago
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?
nox
noxOP2y ago
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.
phaseshift
phaseshift2y ago
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
nox
noxOP2y ago
that sounds like what im trying to do yes
phaseshift
phaseshift2y ago
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
nox
noxOP2y ago
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
phaseshift
phaseshift2y ago
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
nox
noxOP2y ago
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.
phaseshift
phaseshift2y ago
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
nox
noxOP2y ago
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
private Form activeForm = null;
private void openSubForm(Form subForm)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = subForm;
subForm.TopLevel = false;
subForm.FormBorderStyle = FormBorderStyle.None;
subForm.Dock = DockStyle.Fill;
subFormPanel.Controls.Add(subForm);
subFormPanel.Tag = subForm;
subForm.Show();
}
private Form activeForm = null;
private void openSubForm(Form subForm)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = subForm;
subForm.TopLevel = false;
subForm.FormBorderStyle = FormBorderStyle.None;
subForm.Dock = DockStyle.Fill;
subFormPanel.Controls.Add(subForm);
subFormPanel.Tag = subForm;
subForm.Show();
}
phaseshift
phaseshift2y ago
Where does 'subForm' come from? Ie is it recently newd?
nox
noxOP2y ago
yes
phaseshift
phaseshift2y ago
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
nox
noxOP2y ago
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
Want results from more Discord servers?
Add your server