473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Sep 29 '08 #1
5 2928
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

Sep 29 '08 #2
"shapper" <md*****@gmail.comwrote in message
news:9c**********************************@b1g2000h sg.googlegroups.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="defaultValue";
public string ContentType
{
get { return contentType; }
set { contentType = value; }
}

Sep 29 '08 #3
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:

[CategoryAttribute("Progress Bar"),
Browsable(true),
ReadOnly(false),
BindableAttribute(false),
DefaultValueAttribute(typeof(Color),"DarkGreen"),
DesignOnly(false),
DescriptionAttribute("The Progress Bar Color that will be used to
draw the Progress Bar")]
public Color ProgresBarColor {

get {

return this.colorProgressBar;
}

set {

this.colorProgressBar = value;
this.progressBarBrush = new
SolidBrush(this.colorProgressBar);
}

}

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" <md*****@gmail.comschrieb im Newsbeitrag
news:9c**********************************@b1g2000h sg.googlegroups.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
Sep 29 '08 #4
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
Sep 29 '08 #5
On Sep 29, 3:24*pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
"shapper" <mdmo...@gmail.comwrote in message

news:9c**********************************@b1g2000h sg.googlegroups.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="defaultValue";
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
Sep 29 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the...
8
by: stephen | last post by:
Hi, I make use of properties to store values that are relevant to the page but this time I am posting the page to itself and the values are Zero or null (they disappear). is there any other way...
4
by: Avi | last post by:
Hi I am creating web application in which i want to assign by default values to the property which i had created my own. In that one of the property is of type color and i am unable to assign...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
1
by: Olaf Rabbachin | last post by:
Hi folks, I'd like to retrieve default values for properties (of a user control) via code instead of having string-constants. Consider the following (simplified) property: Private _MyColor...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.