I have an application with the following:
- A data layer class containing the data handling logic and the data itself.
public class DataLayer_JSON { CacheData chd = new CacheData(); //The chd object contains the actual data used in the DataLayer_JSON class public DataLayer_JSON(string relPath) }
- A main menu form (essentially it could be just any class)
public partial class MainMenuForm : Form { DataLayer_JSON data = new DataLayer_JSON("questions.txt"); ... private void btnEditon_Click(object sender, EventArgs e) { ... using (var EF = new EditorForm(data.GetCathegories(), data.GetDifficulties(), data.GetQuestions())) { var result = EF.ShowDialog(); if(result == DialogResult.OK) { data.ClearQuestions(); //Clear all cached questions //Since we are still in scope of the EditorForm (EF) within the using clause, we can access it's members data.AddQuestionRange(EF.AllQuestions); //Replace cache with the edited list } } ... //Here we save the data permanently to a text file when closing the program. //This could also be done from the EditorForm if we wanted to private void MainMenuForm_FormClosing(object sender, FormClosingEventArgs e) { data.Save("questions.txt"); }
- An editor form (started from #2)
public EditorForm(IEnumerable<INameObject> Cathegories, IEnumerable<INameObject> Difficulties, IEnumerable<Question> oldQuestions) { ... }
In #2, I create and initialize the data layer instance (#1). I want to modify the data contained in #1 from within #3, but so far, I have only been able to pass by value the contents from #1 to #3, via #2. The results are then returned from #3 to #2 and handled there.
I've read about passing by reference in C#, but my conclusion is that you cant assign a variable to the reference, and then have that variable modify the original data.
I read about the references in C# in the following places this time, as well as reading extensively about this topic many times before: C# reference assignment operator? How do I assign by "reference" to a class field in c#?
So the question is: How can I go about changing the content of an instance of #1 directly in #3?