Solved

Class Library Set Property / Field Default or Initial Values?

Posted on 2012-12-27
5
288 Views
Last Modified: 2012-12-27
Q. How do I set the Property / Field Default or Initial values in a Class Library (.dll)?

I've read about Attributes and it doesn't seem the best approach. The example below is a simple illustration of properties/fields which need to be used, but don't neccessarilly need user setup. Note: it'a a static class.

   
public static class FtpClient
    {
         private static bool _useBinary = false;

         public static bool UseBinary
         {
              set { _useBinary = value; }
          }
     }

Open in new window

0
Comment
Question by:pointeman
  • 3
  • 2
5 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 38723766
That would work (should work for a non-static class [and members] as well). Standard OO practices typically have you initialize a class within its constructor. .NET has something called "object initialization", which can substitute for a equivalent constructor or the approach above. Object initialization does not work with static code, however. Object initialization looks like this:

class Foo
{
    public string Message { get; set; }
}

class Program
{
    static void Main()
    {
        Foo f = new Foo()
        {
            Message = "Hello World!";
        };
    }
}

Open in new window


The braces and the assignment of "Hello World!" to the Message property denote object initialization. Any non-static property can be initialized in this fashion.

I'm not sure how Attributes would help you here. There is a DefaultValueAttribute class, but it is more for code generators than for actual runtime code. This attribute will not set a default value for your objects at runtime.
0
 

Author Comment

by:pointeman
ID: 38723861
I don't use class constructors in my static Class Library.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38723976
Well as I mentioned, what you have shown is a viable approach. You could also define a static constructor (if you so desired), but its net effect would be the same as what you are currently doing.

e.g.

...

    static FtpClient()
    {
        _useBinary = false;
    }

...

Open in new window


Note:  A static constructor isn't called directly. It will be invoked automatically the moment you refer to any of the class' static members or the first time you create an instance of class (if that class is non-static). The initialization you have shown above is also executed in this manner.
0
 

Author Comment

by:pointeman
ID: 38724036
Okay, that makes sense. I found many other articles defining the terms 'Default' or 'Initial' property / field value.
0
 

Author Closing Comment

by:pointeman
ID: 38724257
Thanks again...
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# 2015 Downloading text file and preserving the line breaks 5 37
designing in object programming 12 78
Windows Media Player 2 26
VB: Convert 2 dates to specific format 24 48
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question