473,388 Members | 1,207 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,388 software developers and data experts.

Default Property Value in C#

I have created a static class which contains properties. I can assign values
to them when the software is running. How can I create default values for
these properties when I am coding them, so that I can retrieve those values
without assigning a new one when the software is running?
Nov 17 '05 #1
5 3080
Belee,

If the values are primitive types, you can just declare the value in the
declaration of the static field.

If you require some more intricate initialization, then you can define a
static constructor, like this:

static()
{
// Do static initialization here.
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Belee" <Be***@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
I have created a static class which contains properties. I can assign
values
to them when the software is running. How can I create default values for
these properties when I am coding them, so that I can retrieve those
values
without assigning a new one when the software is running?

Nov 17 '05 #2
either by assigning default value to variable in declaration
class MyClass {

public static string Variable1 = "Value1";

}

or assigning a value in static constructor

class MyClass{

public static string Variable1;

public static MyClass(){

Variable1 = "Value1";

}

}
Mihir Solanki
http://www.mihirsolanki.com
I have created a static class which contains properties. I can assign
values to them when the software is running. How can I create default
values for these properties when I am coding them, so that I can
retrieve those values without assigning a new one when the software is
running?

Nov 17 '05 #3
Hi Belee,

I see that Nicholas and Mihir gave you already the solution I just want to
clear one point. If you look at the docs you may see an attribute called
DefaultValue. This attribute can be applied to properties, but it doesn't
provide the initial value for the attribute. It is merely used in the
property browser and visual designers, but it deosn't intialize the
property.

Constructor (instance or static) or the *get* accessor for the property is
the plaease where the initial value should be givien.
--

Stoitcho Goutsev (100) [C# MVP]
"Belee" <Be***@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
I have created a static class which contains properties. I can assign
values
to them when the software is running. How can I create default values for
these properties when I am coding them, so that I can retrieve those
values
without assigning a new one when the software is running?

Nov 17 '05 #4
private string _myThing = "default property";
public string MyThing
{
get{ return _myThing;}
set{ _myThing = value;}
}

Cheers.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Belee" wrote:
I have created a static class which contains properties. I can assign values
to them when the software is running. How can I create default values for
these properties when I am coding them, so that I can retrieve those values
without assigning a new one when the software is running?

Nov 17 '05 #5
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
If the values are primitive types, you can just declare the value in the
declaration of the static field.

If you require some more intricate initialization, then you can define a
static constructor, like this:

static()
{
// Do static initialization here.
}


Even if you require more intricate construction, you can call methods:

using System;
using System.Collections;

public class Test
{
static Hashtable map = CreateMap();

static void Main()
{
Console.WriteLine (map["first"]);
}

static Hashtable CreateMap()
{
Hashtable ret = new Hashtable();
ret["first"] = "hi";
ret["second"] = "there";
return ret;
}
}

Static constructors have a small performance penalty for frequent uses
of static methods (due to the lack of a beforefieldinit flag).

Strangely enough though, I tend to use static constructors despite
(just now) finding the above a neater way of doing things. Hmm.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

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

Similar topics

5
by: Chuck Bowling | last post by:
Maybe I'm doing something wrong or just don't understand the concept, but i'm having a problem with default properties. My impression of how a default property should act is this; MyClass c =...
4
by: cashdeskmac | last post by:
I have a string which has been assigned the value of "OneTwoThree". I have created a property because I want to be able to assign a new value to this string. I have: string numbers =...
7
by: steve bull | last post by:
if I have a property something like : public int Width { get {return m_width} set {m_width = value} }
5
by: Kris Rockwell | last post by:
Hello (again), I have gotten the dropdown list functionality to work through a few tricks (probably not the most efficient, but it works) but I am not sure how to set the default selected value....
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...
4
by: Dennis | last post by:
I am trying to set the default design proerties in a control I have derived from the Panel Class. I thought I'd found how to do it from the MSDN but the following line doesn't work: Inherits...
1
by: Bernard Bourée | last post by:
I would like to achieve the following goal: design a class with a default property called dVal where I could store and get a decimal value WITHOUT the need to write the property. I have the...
9
by: Eric | last post by:
Hi Everyone, I'm writing a UserControl that exposes a property of the type System.Drawing.Image, like this: Public Property DefaultImage() As Image Get Return propDefaultImage End Get...
10
by: Brad Baker | last post by:
I have an asp.net/csharp application that requires a particular variable to work properly. This variable is usually passed via a query string in the URL when the application is first run but under...
0
ADezii
by: ADezii | last post by:
The motivation for this Tip was a question asked by one of our Resident Experts, FishVal. The question was: How to Declare Default Method/Property in a Class Module? My response to the question was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.