Search code examples
c#reference-type

C# reference a value type


I have the main component of my program which runs in the main thread. The program has the ability to save the user's work to a file, however extra meta-information is saved along with the user's work.

Example:

[Serializable]
public Class UserWork
{
  // Lots of things in here. Lots of work user has done, etc...
}

In the program, I store a couple things like settings, etc. Let's assume these are bool flag1 and bool flag2. I have a structure like this:

[Serializable]
public class SavedFile
{
  UserWork userWork;
  bool flag1;
  bool flag2;
}

When I save:

saveData(new SavedFile(userWork, flag1, flag2));

When I load:

SavedFile savedFile = SavedFile.Load(path);
UserWork userWork = savedFile.userWork;
bool flag1 = savedFile.flag1;
bool flag2 = savedFile.flag2;

So basically, flag1 and flag2 need to be saved with the work, but they don't necessarily belong to with the work. They actually represent how far through the workflow the user is at the time of save.

Now, I have built an AutoSave feature which monitors events which are fired whenever the user makes any modifications. The UserWork class fires events, so this was easy to do. The issue I'm having is that I'm not sure how to send and store the references to the flags in the AutoSave thread.

I have:

public class AutoSave // (This code runs in a separate thread)
{
  bool flag1;
  bool flag2;

  UserWork userWork;

  public AutoSave(UserWork userWork, ref bool flag1, ref bool flag2)
  {
    this.flag1 = flag1;
    this.flag2 = flag2;

    this.userWork = userWork;

    userWork.workUpdated += new WorkUpdatedHandler(save);
  }

  void save(object sender, WorkUpdateArgs e)
  {
    saveDate(new SavedFile(userWork, flag1, flag2));
  }
}

When I run my code (which is not the above code, but similar code), the flags are both always false regardless of what their values are in my main thread.

Edit for clarification: The issue is not that I cannot get this to work with other workarounds. For example, I can make the two flags public in the AutoSave class and update the main flags and the autosave flags simultaneously. i.e. flag1 = autoSave.Flag1 = true. Rather, the issue is that this is cumbersome. It means I must remember to do this each and every time any of the flags get updated. Also, it doesn't make sense to be updating several variables with the same data all the time when I can simply have one copy in memory and reference it in several places.


Solution

  • You can't use ref like that - the code you've shown won't compile. You'd have to take the ref modifier off your instance variables. Whatever was passed into the AutoSave constructor is entirely independent from the instance variables as soon as they've been assigned.

    You could use a wrapper class:

    public class Wrapper<T>
    {
        public T Value { get; set; }
    }
    

    and pass in two Wrapper<bool> references - then the caller could do

    flag.Value = true;
    

    and that change would be visible in AutoSave. It's pretty nasty though... I'd try to avoid that pattern if you can.

    Can whatever's changing the flags not raise an event too, so you could capture that in AutoSave?