?
Solved

should I always declare my variables before assigning

Posted on 2012-03-26
7
Medium Priority
?
267 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
[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
7 Comments
 
LVL 30

Assisted Solution

by:IanTh
IanTh earned 900 total points
ID: 37765086
its always safer to declare the variable before you use it imho
0
 

Author Comment

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

Accepted Solution

by:
Neil Russell earned 900 total points
ID: 37765104
Yes. Good practice is ALWAYS to pre declare ALL variables before use.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Assisted Solution

by:Lalit Chandra
Lalit Chandra earned 200 total points
ID: 37765105
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
ID: 37765106
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
ID: 37765109
Lalit-Chandra I understand scope, I was referring to inside my methods more so....
0
 
LVL 7

Expert Comment

by:Lalit Chandra
ID: 37765123
ok,then you r doing it in a right way.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

741 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