Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# automatic properties - is it possible to have custom getter with default setter?

Tags:

c#

It's possible I shouldn't even be attempting this in the first place, but here's what I have so far:

public List<int> AuthorIDs
{
    get
    {
        var l = new List<int>();
        using (var context = new GarbageEntities())
        {
            foreach (var author in context.Authors.Where(a => a.Books.Any(b => b.BookID == this.BookID)).ToList())
            {
                l.Add(author.AuthorID);
            }
        }
        return l;
    }
    set; //compiler error
}

How would I leave the above setter without any sort of custom logic? In the olden days I think you would just use:

set { authorIDs = value; }

which doesn't work now.

Is this whole idea just terrible to begin with?

Edit:

To answer some people's questions: I'm attempting to combine MVC with Data Annotation validation, with default binding, with Entity Framework 4.0...and failing quite fantastically, I believe.

like image 295
asfsadf Avatar asked Sep 23 '10 00:09

asfsadf


1 Answers

No, it's not possible. Either everything is explicit, or the whole property is automatic. Anyway, in that case the setter doesn't seem to make any sense... there should be no setter at all.

Also, I think you should make it a method. It would make it clearer to the caller that it performs a possibly lenghty calculation. It's also against guidelines to perform complex processing in a property.

like image 144
Thomas Levesque Avatar answered Sep 21 '22 05:09

Thomas Levesque