Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# - Class-level variables (fields) with instantiation seem weird. Are they?

Posted on 2013-01-21
3
Medium Priority
?
341 Views
Last Modified: 2013-01-22
Take a look at the following code snippet that one of my subs created. He created some class-level variables (or fields) and then went ahead and instantiated them. My inclination would have been to declare the variables at the class level and then instantiate them in the class constructor or an appropriate method. I have to admit, the calls to the variables do work. Maybe his construct is a good one, and I just don't get it.

   class FlatFileParser
   {
      private ADOBoulder adoBoulder = new ADOBoulder();
      private FieldMapping fldMapp = new FieldMapping();
      private int intLineNo = 0;
      private bool isErrorFile = false;
      ...

Open in new window

0
Comment
Question by:jdana
3 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38802748
You can declare and instantiate variables in one go. Its a matter of personal preference though I would prefer to instantiate reference type variables in a code block where I can log errors.
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 1220 total points
ID: 38803989
The approach you described here is valid one. There is nothing wrong to declare and instantiate variables on the class level (say, in "declarations section"). Also, as you can see, the compiler allows that, so it means there is nothing wrong !

See, for example, we have the following code:
    public class MyTest
    {
        private int someNumber=77;
        private string someString;
        
        private double someNumber2;
        private string someString2 = "b";

        public MyTest()
        {
            someNumber2 = 33;
            someString = "a";
        }
    }

Open in new window


Lines 'private int someNumber=77;' and 'private string someString2 = "b";' are executed first, even before calling the constructor. Then the constructor is called. The someString and someNumber2 variables are NOT allocated before entering the constructor!

You may consider the initialization of  'private int someNumber=77;' and 'private string someString2 = "b";'  variables as a part of the class constructor.

This is a matter of preferences and a personal taste. Or "religious" matter, as out architect tells :). Personally myself, I like to keep initialization code in a constructor or in some other method. I believe it makes code more readable and maintainable.
I'd say the demonstrated approach was very popular among VB developers :).
0
 

Author Closing Comment

by:jdana
ID: 38805794
anarki_jimbel,

Thanks for the thoughtful response. Like you, I prefer to put initialization of the variables in the constructor. Keep it clean, and the code behaves as expected.

J
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

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!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 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