0

I have an application with the following:

  1. 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) }
  1. 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");
    }
  1. 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?

Community
  • 1
  • 1
Spaxys
  • 9
  • 2
  • Make #1 static, thus exposing it to #2 and #3. Keep it read-only to avoid blaspheming the C# gods. – Shannon Holsinger Aug 31 '16 at 23:39
  • 1
    We need to see example code here. By default, if you pass an instance of a class somewhere, you are passing a reference. – Blorgbeard Aug 31 '16 at 23:48
  • Agree with @Blorgbeard ... I have something in my head (like read-only concurrent collections that are threadsafe as static) but your code may not be anything like that, thereby rendering my earlier advice moot or even bad. – Shannon Holsinger Aug 31 '16 at 23:54

1 Answers1

0

How can I go about changing the content of an instance of #1 directly in #3?

Well, you'd start by passing the instance of #1 to #3. Currently you are not doing that.

So for example:

public EditorForm(DataLayer_Json data)
{
    ...
}

and

using (var EF = new EditorForm(data))

Then inside the EditorForm, you can do whatever you want to data (except for reassigning the actual reference) and those changes will be reflected outside of EditorForm as well.

data.ClearQuestions();          // Works fine, mutating the object.
data = new DataLayer_JSON();    // Doesn't affect the `data` variable that was passed in.
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272