Solved

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

Posted on 2013-01-21
3
335 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
[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 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 305 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

762 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