Sign in to follow this  

[.net] Private Property?

This topic is 3293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I didn't know you can make the set and get variables private. Why would you do this?
[Browsable(false),
         Description("The file which is being posted")]
        public HttpPostedFileAJAX PostedFile
        {
            get
            {
                if ((_PostedFile == null) && (IsPosting))
                {
                    HttpPostedFile file = Request.Files[0];
                    _PostedFile = new HttpPostedFileAJAX(file.FileName, file.ContentType, file.ContentLength);
                }

                return _PostedFile;
            }
            private set { _PostedFile = value; } // what's the point? how does this work?
        }

Share this post


Link to post
Share on other sites
Makes it somewhat easier to modify later if you need to perform a check or do some other operation before/after assigning the value to the variable. I usually just assign directly to the underlying variable inside the class.

Share this post


Link to post
Share on other sites
If setting a property requires validation of the value, or performs some other side effect, or maybe doesn't correspond to a backing variable at all, having a setter is a must. If users outside of the class should not be able to change the value of the property, but must instead influence it via some other methods on the class, then the setter should be made private, internal, or protected as appropriate.

Share this post


Link to post
Share on other sites
I use it very frequently for stuff I may want to make more complex later, or is already more complex than a very basic value. For example, if I have a read-only inventory, I may make a public indexer with a private setter, and use only that private setter to set items around in the inventory. That way, if I want to change later to a different storage system, like an index-based array, dictionary, list, etc, it requires very little effort.

One thing that pisses me off is that you can't do this:


abstract class BaseClass
{
protected abstract object MyProperty { get; }
}

class MyClass : BaseClass
{
object myObj;

protected override object MyProperty
{
get { return myObj; }
private set { myObj = value; } // error!
}
}



I would love to be able to use that for that exact same reason - not adding a virtual setter, but just a regular setter. Often times I have to resort to a separate method SetMyProperty(), which is just ugly.

[Edited by - Spodi on December 3, 2008 4:57:44 PM]

Share this post


Link to post
Share on other sites
This is also really handy in 3.5 as you don't have to declare the variables behind properties at all if you don't want to and you may want to make the setting of the variable private, protected or internal:


//C# 2
class Foo {
private int bar;
public int Bar {get{return bar;}}
}

//C# 3
class Foo {
public int Bar {get;private set;}
}


[Edited by - turnpast on December 7, 2008 3:18:58 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
I didn't know you can make the set and get variables private. Why would you do this?
To restrict the user's interaction with your property's value.
In that example you allow the user read-only access, whilst the internals of your class have read-write access. In another language, such as Java, it would be like providing a public getPostedFile method but no public setPostedFile method.

Share this post


Link to post
Share on other sites
It might help to think about it if you consider making a setter that's not private but protected:

class Base
{
int propval;
public int SomeClassProperty
{
get { return propval; }
protected set { propval = value; }
}
public Base() { SomeClassProperty = 1; }
}
class Derived : Base
{
public Derived() { SomeClassProperty = 2; }
}

Share this post


Link to post
Share on other sites
Quote:
Original post by turnpast
This is also really handy in 3.5 as you don't have to declare the variables behind properties at all if you don't want to and you may want to make the setting of the variable private, protected or internal:


//.NET 1.1/2.0
class Foo {
private int bar;
public int Bar {get{return bar;}}
}

//.NET 3.0+
class Foo {
public int Bar {get;private set;}
}

Actually that's a feature of C# 3.0 and not related to the version of .NET you are targeting.

Share this post


Link to post
Share on other sites

This topic is 3293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this