0

I know the question isn't very clear, the thing I don't even know what words might exst to describe what I'm trying to do but this example should make it clearer.

Say this bit of code

public string Underscores;
public string Spaces 
{ 
    get => Underscores.Replace('_', ' '); 
    set => Underscores = value.Replace(' ', '_'); 
}

In this example, the value of Underscores is stored in memory but the value of Spaces is not and is instead generated on the fly when refered to. If the value of Spaces is changed, the value of Underscored changes accordingly. What I want to do is to extend this concept to structs and classes:

struct StringStruct
{
    public string Value { get; set; }
}

...

public string Underscores;
public StringStruct Spaces
{
    get => new StringStruct { Value = Underscores.Replace('_', ' ') }; 
    set => UnderScores = value.Value.Replace(' ', '_') };
}

It works fine if I overwrite Spaces with a new StringStruct, but trying to change the value of Space's Value property results in a compiler error since it's not a variable. The same scenario but where StringStruct is a class causes no error but the change will be ignored and Underscores will remain the same.

The examples were made up for the sake of explanation and not what I'm actually trying to do.

TheBoxyBear
  • 371
  • 2
  • 15

1 Answers1

0

The value and Underscores in your code are not strings but instances of StrigStruct. So you can't call the string.Replace() method on them.

This will fix your problem.

public StringStruct Underscores;
public StringStruct Spaces
{
    get => new StringStruct { Value = Underscores.Value.Replace('_', ' ') };
    set => Underscores = new StringStruct { Value = value.Value.Replace(' ', '_') };
}

Edit

After OP edited the post with new information.

The reason why you get the Cannot modify the return value of 'expression' because it is not a variable error is because you're using a struct for your StringStruct instead of a class since struct is not a reference type unlike a class. Please read more here.

Sach
  • 10,091
  • 8
  • 47
  • 84
  • Yes and no. I noticed the issue and updated the struct. The problem is about how to allow the modification of a "derived" struct's property and reflect the change in the variable the "derived" one is based on and potentially do the same with classes. – TheBoxyBear Jul 10 '20 at 22:57
  • The example on its own works but the problem is when trying to do something like `Spaces.Value = "abc";` I know the way I structured it it's not possible and I'm looking for a new approach that would make it possible without having to store Underscore and Spaces. – TheBoxyBear Jul 10 '20 at 23:03
  • OK, that's because you're using `struct` instead of `class` (read here: https://stackoverflow.com/a/1747702/302248) – Sach Jul 10 '20 at 23:10
  • Right, using a struct gives an error, but replacing it with a class still deosn't solve it. Although there is no error, the change is ignored and Underscores stays the same. Ideally it should call the setter where value is a modified version of what returned by the getter but it looks like it's doesn't behave like this. – TheBoxyBear Jul 10 '20 at 23:12
  • Yes but that's expected behavior, isn't it? You're simply overwriting the value of `Value` property in `StringStruct`. Your solution is that you should make `Value` a getter only, and use `Replace` to set it. I'll update my answer. – Sach Jul 10 '20 at 23:14
  • Essentially your problem is that you're using a property to do the job of a method. – Sach Jul 10 '20 at 23:18
  • The Replace method was only added to go around a mistake when pasting code from the first example. I'm working on a library so the end goal if to allow the end user to work with the objects/struct intuitively in the format they are more comfortable with without having to use extra memory to store each value in every format. – TheBoxyBear Jul 10 '20 at 23:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217623/discussion-between-sach-and-joujoubox). – Sach Jul 10 '20 at 23:19