Solved

Class Library Set Property / Field Default or Initial Values?

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
defining NULL or 0 10 51
"Emulate" TAB key when press Enter Key 3 74
asp web application 3 43
linq .concat equivalent with expression syntax 4 26
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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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