Solved

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

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

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

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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 discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

13 Experts available now in Live!

Get 1:1 Help Now