1

I have retrieved a static table from a database in a list. If someone calls the constructor of that class with an ID, the constructor finds the object in the list and copies all values.

public class MyClass
{
    public int Id { get; set; }
    public string Text { get; set; }

    public MyClass(int instanzId)
    {
        MyClass myClass = CachedList().Find(T => T.Id == instanzId);
        Id = myClass.Id;
        Text = myClass.Text;
    }
}

This will always create a new instance of that object. Is it possible to return the object from the list directly? Like this:

public class MyClass
{
    public int Id { get; set; }
    public string Text { get; set; }

    public MyClass(int instanzId)
    {
        this = CachedList().Find(T => T.Id == instanzId);
    }
}

I know it's easy in a static method, but how can that be done in the constructor?

9
  • You can't return any values from a constructor, and you can't assign values to 'this'. Commented Jul 14, 2015 at 12:11
  • 1
    This will always create a new instance of that object. - and you are expecting what, when calling new MyClass()? Commented Jul 14, 2015 at 12:11
  • 3
    @DanielA.White: At least, not in a class constructor. In a struct constructor you can assign to this... Commented Jul 14, 2015 at 12:11
  • 2
    I would write whatever crap I could think of to have @JonSkeet correct me. :) Commented Jul 14, 2015 at 12:13
  • 1
    @MichaelK, you previously said (implied) that all the values were cached. How does using a factory method reveal whether it's cached? Commented Jul 14, 2015 at 12:30

2 Answers 2

5

Well no, not via constructor. You can do it via a method. For example a factory:

public MyClass FindOrCreate(int instanceId)
{
    MyClass obj = CachedList().Find(T => T.Id == instanzId);
    //create obj when it does not exist

    return obj;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@MichaelK, it's your only option if you don't want to create a new instance.
Unfortunately. I thought there was a better way than copying the properties.
2

Wrap cache into just another lightweight class:

  public class MyWrapClass {
    private MyClass m_Cache;

    public MyWrapClass() {
      m_Cache = CachedList().Find(T => T.Id == instanzId);
      ...
    }
    ...

    // implicit cast to MyClass if you want it 
    public static implicit operator MyClass(MyWrapClass value) {
      if (null == value)
        return null;
      else
        return value.m_Cache;
    }
  }

  ...
  // Or MyWrapClass test = new MyWrapClass();
  MyClass test = new MyWrapClass();

1 Comment

except the clear wtf? when reading MyClass test = new MyWrapClass(); months later I really like the cleverness of this attempt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.