Solved

should I always declare my variables before assigning

Posted on 2012-03-26
7
262 Views
Last Modified: 2012-08-13
I've always been in the habit of declaring my variables at the top of the page as null and then assigning them, normally inside my try{} or main block of code, something along the lines of the below:
protected void Page_Load(object sender, EventArgs e)
    {
        List<Job> jobs = null;

        if (!Page.IsPostBack)
        {
            jobs = new List<Job>;

            //and so on....
        }
    }

Open in new window


Is that good/bad practice or would I be better off just creating a new list inside my main if.

Is it the case I should only do this if I need the list in scope for my finally block or some code outside of the main if....?
0
Comment
Question by:scm0sml
7 Comments
 
LVL 30

Assisted Solution

by:IanTh
IanTh earned 225 total points
Comment Utility
its always safer to declare the variable before you use it imho
0
 

Author Comment

by:scm0sml
Comment Utility
so you are saying as I'm doing it in your opinion is correct?
0
 
LVL 37

Accepted Solution

by:
Neil Russell earned 225 total points
Comment Utility
Yes. Good practice is ALWAYS to pre declare ALL variables before use.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Assisted Solution

by:Lalit Chandra
Lalit Chandra earned 50 total points
Comment Utility
Every declared variable has it scope. the variable that you define outside oof methods, it become global to the class,means its get  of memory every time the class is initiated. and as it is global, there is no any control of their access,and hence any wrong access (updation) is done  by some other method while it being used by any other method.

So,By Standards, its a good habit to limit a variable to a limited scope (method scope) until an unless there is a serious need of declaring Global variables.
0
 

Author Comment

by:scm0sml
Comment Utility
It's what I've always done but have never been clear whether it was correct or not.

Is there any reason for this as such?
0
 

Author Comment

by:scm0sml
Comment Utility
Lalit-Chandra I understand scope, I was referring to inside my methods more so....
0
 
LVL 7

Expert Comment

by:Lalit Chandra
Comment Utility
ok,then you r doing it in a right way.
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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

16 Experts available now in Live!

Get 1:1 Help Now