Solved

Class Library Set Property / Field Default or Initial Values?

Posted on 2012-12-27
5
287 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now