Solved

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

Posted on 2013-01-21
3
336 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
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.
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…

732 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