Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 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