Solved

Class Library Set Property / Field Default or Initial Values?

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

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 74

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

19 Experts available now in Live!

Get 1:1 Help Now