Use of unassigned local variable 'i'

This is a discussion on Use of unassigned local variable 'i' within the C# Programming forums, part of the General Programming Boards category; Code: int i; try { i = 0; } catch { } if (i == 1) { } This is ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Denmark
    Posts
    49

    Use of unassigned local variable 'i'

    Code:
                int i;
    
                try
                {
                    i = 0;
                }
                catch
                {
                }
    
                if (i == 1)
                {
                }
    This is a simplification, but shows what I mean. If you try to compile this, it says...

    Use of unassigned local variable 'i'

    ... with the i in the if statement underlined. Can someone explain why this is reasonable? Bear in mind what I said before, this is a gross over simplification of what I was actually trying to do.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    In theory, if the first line of your try block throws an exception it will reach your if condition without i being set to any specific value. So you need to set i in a way that it will always have a value even if exceptions are thrown.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Denmark
    Posts
    49
    As I said Niels, the example I posted above is a gross over simplification, but the example above, as it stands, equally fails with the unassigned error. That is the ONLY line in the try block.
    Last edited by Fossaw; 04-21-2010 at 06:49 AM.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    If the only line in your try block is a line that you know for certain will never throw an exception, then why have a try block at all?

    Seems like if you have something in a try block, one way the compiler might interpret this is, "Hey, the programmer is saying that this code might throw an exception at any point. I shouldn't depend on anything in the try block happening successfully." If you ask me, this is totally reasonable.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Maybe I'm missing the point here, but why not put the
    Code:
    if (i == 1) { }
    within the try brackets also? Afterall, if the try code fails, then i remains unassigned. The other choice is to give i an initial value like 0 or -1.

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    If you don't feel like initializing i to a value then you can always add a "finally" block after your catch block and set i to a value there. This way you can check the value of i later on for any of those values i might be assigned (whether in the try block, or finally block) and it will not produce an error.
    The keyboard is the standard device used to cause computer errors!

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Simply put, the compiler assumes that nothing inside a try..catch is guaranteed to have been executed; it's the same as if you set i inside an if statement. If you're really, really sure that i will always have a proper value set, then you can just declare it as int i = 0; or some other appropriate default, and ignore it.

    In more realistic code, this is a warning to you that (in theory) not all code paths properly set the value. You may want to reconsider the design such that this can't happen. Granted, in your over-simplified code, there's no way for an assignment to throw and thus the hypothetical code-path that results in uninitialized code can't ever actually occur.
    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.

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    A lot of people find this annoying. But the compiler has to make sure that unassigned variables are not used. And that is the price you pay.

    A better example would be
    Code:
    int i;
    try { 
      i = 0; 
    } catch { 
       Application.Exit(); 
    }
    if (i == 0) 
       i++;
    In any case, you can just initialize it and get it over with. The initialization doesn't seem "ugly", it is done in the same line, it is written on the end of the line, so it doesn't really take space. Thus, the initialization is a good solution.

  9. #9
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Quote Originally Posted by Fossaw View Post
    As I said Niels, the example I posted above is a gross over simplification, but the example above, as it stands, equally fails with the unassigned error. That is the ONLY line in the try block.
    Yes. And if the line throws an exception, you will catch it and continue reading i, which is not initialized because the line with the initialization was not properly executed.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Location
    Denmark
    Posts
    49
    Thanks for the input, well, to those that understood what oversimplification meant anyway.

    It is irritating that C# insists on "doing things for you" but in crude "general case" ways that don't match the real world.

    My dislike of C# has increased again.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Fossaw
    Thanks for the input, well, to those that understood what oversimplification meant anyway.
    Yes, the oversimplification means that your example does not reflect the "real world".

    Quote Originally Posted by Fossaw
    It is irritating that C# insists on "doing things for you" but in crude "general case" ways that don't match the real world.
    In the real world, you would make good use of the fact that the statement could be placed before the try block, in the finally block, or perhaps try/catch/finally is not necessary at all.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2010
    Location
    Denmark
    Posts
    49
    My post was to demonstrate the issue. An oversimplification, which still showed the point, in a forum such as this, is more likely to yield useful comment then a cut and paste of the real world application I have the misfortune to be working with.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    C# insists on "doing things for you"
    If that were true then "i" would not remain unassigned. Maybe you mean C# didn't do things for you?

  14. #14
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Fossaw View Post
    It is irritating that C# insists on "doing things for you" but in crude "general case" ways that don't match the real world.
    Actually, ANY instance of this happening will come down to one of two scenarios, both of which are problems with how it was coded:

    A) A code path exists where this value isn't set before it's used.
    B) An unnecessary flow-control structure is being used.


    In the case of a "real-world" try/catch, either the assignment line or any preceding line could throw, or those lines cannot. If one of the lines throws, then A) is true -- you have a code path where the value is unset. If none of those lines can ever throw, then B) is true -- you can pull this line and all preceding lines out of the try block, because they cannot raise exceptions and thus don't need to be inside the block.
    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.

  15. #15
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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?".

Page 1 of 2 12 LastLast
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, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21