Property Default Values in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Property Default Values in C#

    Hello,

    I have a class with a few properties:

    public class PageViewData {
    public string ContentType { get; set; }
    public string Keywords { get; set; }
    public string Title { get; set; }
    }

    What is the correct way to define a property default value in case the
    property is not defined?

    Thank You,
    Miguel
  • Jon Skeet [C# MVP]

    #2
    Re: Property Default Values in C#

    On Sep 29, 3:12 pm, shapper <mdmo...@gmail. comwrote:
    Hello,
    >
    I have a class with a few properties:
    >
      public class PageViewData {
        public string ContentType { get; set; }
        public string Keywords { get; set; }
        public string Title { get; set; }
      }
    >
    What is the correct way to define a property default value in case the
    property is not defined?
    You can't do it directly using automatically implemented properties -
    other than by setting the properties in the constructor(s) of course.
    There are a few things like this where automatic properties could be
    made more pleasant fairly easily. It would be nice to see them tweaked
    for C# 4, but I don't expect that to happen. Maybe for C# 5 -
    particularly as part of a push towards making immutability easier.

    Jon

    Comment

    • Alberto Poblacion

      #3
      Re: Property Default Values in C#

      "shapper" <mdmoura@gmail. comwrote in message
      news:9c6bfab0-28bf-4691-838e-cf0b420791b7@b1 g2000hsg.google groups.com...
      I have a class with a few properties:
      >
      public class PageViewData {
      public string ContentType { get; set; }
      public string Keywords { get; set; }
      public string Title { get; set; }
      }
      >
      What is the correct way to define a property default value in case the
      property is not defined?
      Use the "old" syntax, and assign the default value to the private
      variable that stores the property:

      private string contentType="de faultValue";
      public string ContentType
      {
      get { return contentType; }
      set { contentType = value; }
      }

      Comment

      • =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?=

        #4
        Re: Property Default Values in C#

        Hi Miguel,

        there are two ways: Either initalize all values
        to some default stuff in the Class Constructor,
        or use attributes on the classes like e.g. this:

        [CategoryAttribu te("Progress Bar"),
        Browsable(true) ,
        ReadOnly(false) ,
        BindableAttribu te(false),
        DefaultValueAtt ribute(typeof(C olor),"DarkGree n"),
        DesignOnly(fals e),
        DescriptionAttr ibute("The Progress Bar Color that will be used to
        draw the Progress Bar")]
        public Color ProgresBarColor {

        get {

        return this.colorProgr essBar;
        }

        set {

        this.colorProgr essBar = value;
        this.progressBa rBrush = new
        SolidBrush(this .colorProgressB ar);
        }

        }

        its a piece of code that i use in my extended
        progress bar class. See the .NET Documentation
        for more information about attributes and the
        information about the classes in this example,...

        Regards

        Kerem

        --
        -----------------------
        Beste Grüsse / Best regards / Votre bien devoue
        Kerem Gümrükcü
        Latest Project: http://www.codeplex.com/restarts
        Latest Open-Source Projects: http://entwicklung.junetz.de
        -----------------------
        "This reply is provided as is, without warranty express or implied."
        "shapper" <mdmoura@gmail. comschrieb im Newsbeitrag
        news:9c6bfab0-28bf-4691-838e-cf0b420791b7@b1 g2000hsg.google groups.com...
        Hello,
        >
        I have a class with a few properties:
        >
        public class PageViewData {
        public string ContentType { get; set; }
        public string Keywords { get; set; }
        public string Title { get; set; }
        }
        >
        What is the correct way to define a property default value in case the
        property is not defined?
        >
        Thank You,
        Miguel

        Comment

        • Marc Gravell

          #5
          Re: Property Default Values in C#

          Either initalize all values
          to some default stuff in the Class Constructor,
          or use attributes on the classes
          Attributes won't change the value, though - they are used mainly to
          determine whether a value should be saved, and to reset the value
          (akin to ShouldSerialize {name} and Reset{name}). Generally you need
          code in the ctor, or a field initializer (which you can't do with auto-
          implemented properties).

          Marc

          Comment

          • shapper

            #6
            Re: Property Default Values in C#

            On Sep 29, 3:24 pm, "Alberto Poblacion" <earthling-
            quitaestoparaco ntes...@poblaci on.orgwrote:
            "shapper" <mdmo...@gmail. comwrote in message
            >
            news:9c6bfab0-28bf-4691-838e-cf0b420791b7@b1 g2000hsg.google groups.com...
            >
            I have a class with a few properties:
            >
             public class PageViewData {
               public string ContentType { get; set; }
               public string Keywords { get; set; }
               public string Title { get; set; }
             }
            >
            What is the correct way to define a property default value in case the
            property is not defined?
            >
               Use the "old" syntax, and assign the default value to the private
            variable that stores the property:
            >
            private string contentType="de faultValue";
            public string ContentType
            {
               get { return contentType; }
               set { contentType = value; }
            >
            }
            I see, so it is exactly the same as in VB.NET. I though, since C# has
            automatic properties, that this would be different.

            Thanks,
            Miguel

            Comment

            Working...