Thread: Use of unassigned local variable 'i'

  1. #16
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by C_ntua View Post
    I believe the only reasonable argument here, as shown in my example, is when the code forces the program to die with a function. The language can understand if you have a return statement, but has no idea what a function does.

    But think that it would be kind of troublesome to "fix" the above. In my example, maybe Application.Exit() can fail and not terminate the program.

    The real question here is this "Should it be a warning or an error?"

    99% of the times, using unassigned variables in unacceptable. So what would you have done? Make it a simple warning?

    I get the feeling of the question, been there myself, but always ask "what else could have been done?".
    Actually, in your code above, the condition will still be evaluated. Application.Exit() does NOT immediately and unconditionally kill the program. Rather, it puts a message in the message pump that requests a shutdown. The remainder of the function will continue to execute (assuming that it's executing on the same thread as the message pump) since the message can't be processed until the currently executing function returns. Only when the message pump next checks for messages will the program shut down.

    Example: The following DOES write the line of text to the log:

    Code:
                StreamWriter loggingFile = new StreamWriter(@"outputLog.txt");
                
                /* ...  */
    
                Application.Exit();
                loggingFile.WriteLine("Exit has been called");
                loggingFile.Close();
    You probably could do a scorched-earth method like get a handle to the current process and kill it, but that's CERTAINLY not good practice.
    Last edited by Cat; 04-23-2010 at 03:14 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should "static" variable always be local to a file?
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2008, 11:51 PM
  2. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  3. Printing part of a local variable
    By Phan in forum C Programming
    Replies: 10
    Last Post: 09-26-2005, 05:51 PM
  4. return local variable
    By sangi in forum C Programming
    Replies: 17
    Last Post: 10-22-2004, 03:40 AM
  5. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM