mpen
mpen

Reputation: 282825

How to perform a reference assign?

Try not to get too hung up on what I'm doing here, this is just an example:

var @string = new RegexTerm("...");
var literal = new RegexTerm("...");
var key = @string | literal;
var dict = new Term();
var value = key | dict;
var kvp = key | value;
var item = key | kvp;
var set = item + !("," + item);
dict = "{" + set + "}";

I have a circular relationship. value needs dict to be defined, and dict needs value to be defined.

So what I've done is initialized dict to some placeholder Term so that I can use it in the following assignment, and then I give it it's real value later on.

The problem is that now value no longer refers to the same object as defined on the last line. I essentially want to re-assign dict without creating a new object.

Is there any way to do this without adding a setter? This would require me to create a public parameterless constructor on my AndTerm and then add a Set method, which just isn't as pretty, and allows you to construct invalid objects.

Or is there some magical way of reassigning dict to point to a new object and update all existing references? Apparently I'm not allowed to overload the = operator.


Came up with one solution. I added an extra Term called a ProxyTerm:

var dict = new ProxyTerm();
var value = key | dict;
...
dict.Term = "{" + set + "}";

This allows me to keep the base Term class as abstract, and then I don't need to expose any private variables or add any setters.

Upvotes: 0

Views: 86

Answers (1)

sunside
sunside

Reputation: 8249

A solution would be to code against an interface or an abstract base and use a proxy pattern.

class ProxyTerm : ITerm {

     ITerm Reference { get; set; }

     ITerm.SomeMethod() {
          Reference.SomeMethod();
     }

}

This way you'll be able to plug in the real reference afterwards.

Upvotes: 1

Related Questions