CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2011
    Posts
    4

    Variable Declaration Question

    I have a method that gets called on a Timer event (so every .5 seconds or so). In the method, I declare a variable and assign a value to it. The variable is not used anywhere else, which is why I defined it in the method.
    However, I was wondering if it makes more sense to just declare the variable globally and re-assign to it in the method. Wouldn't that save on resources needed to re-create the variable every .5 seconds?

    I'm guessing this type of question has been asked (and answered) many times but couldn't find anything with a quick google search. If you have a link to an article explaining this sort of thing, I'd appreciate it.

    Thanks.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Variable Declaration Question

    No, this is the wrong way to write software.

    The decision to declare a variable at a given scope is one of function, not performance. When a variable can be local to a function, it should be. Global variables are a common source of problems as you need to account for this "hidden" state. Your functions now become less deterministic as their output may change even when given the same input.

    Prefer local variables. Prefer immutable data structures. There are always exceptions to every rule (even that one I suppose...), but 99.9% of the time this is not a performance consideration, it is one of reliability and clean design.

    You will almost certainly never notice the nanoseconds required to create a local variable. It is certainly not a consideration when you are assigning a new value to this variable in the method anyway. All you are doing is making your program more error prone and confusing, you are not making the slightest bit of difference performance wise. Please read: http://c2.com/cgi/wiki?PrematureOptimization
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  3. #3
    Join Date
    Sep 2011
    Posts
    4

    Re: Variable Declaration Question

    Thanks. I feel like I already knew this...just needed someone else to yell it at me.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Variable Declaration Question

    No problem, happy to.... yell?
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  5. #5
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Talking Re: Variable Declaration Question

    A variable can be compared to a storage room, and is essential for the programmer. In C#, a variable is declared like this:

    <data type> <name>;

    An example could look like this:

    string name;

    That's the most basic version. Usually, you wish to assign a visibility to the variable, and perhaps assign a value to it at the same time. It can be done like this:

    <visibility> <data type> <name> = <value>;

    And with an example:

    Code:
    private string name = "John Doe";
    The visibility part is explained elsewhere in this tutorial, so let's concentrate on the variable part. We will jump straight to an example of actually using a couple of them:


    Code:
    using System;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string firstName = "John";
                string lastName = "Doe";
    
                Console.WriteLine("Name: " + firstName + " " + lastName);
    
                Console.WriteLine("Please enter a new first name:");
                firstName = Console.ReadLine();
    
                Console.WriteLine("New name: " + firstName + " " + lastName);
    
                Console.ReadLine();
            }
        }
    }
    Okay, a lot of this has already been explained, so we will jump directly to the interesting part. First of all, we declare a couple of variables of the string type. A string simply contains text, as you can see, since we give them a value straight away. Next, we output a line of text to the console, where we use the two variables. The string is made up by using the + characters to "collect" the different parts.

    Next, we urge the user to enter a new first name, and then we use the ReadLine() method to read the user input from the console and into the firstName variable. Once the user presses the Enter key, the new first name is assigned to the variable, and in the next line we output the name presentation again, to show the change. We have just used our first variable and the single most important feature of a variable: The ability to change its value at runtime.

    Another interesting example is doing math. Here is one, based on a lot of the same code we have just used:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured