Solved

Class Library Set Property / Field Default or Initial Values?

Posted on 2012-12-27
5
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 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