Error 4 Use Of Unassigned Local Variable
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies error 1 use of unassigned local variable c# of this site About Us Learn more about Stack Overflow the company Business use of unassigned local variable list Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges
Use Of Unassigned Local Variable Unity
Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:
Use Of Unassigned Local Variable C# Array
Sign up Why compile error “Use of unassigned local variable”? up vote 27 down vote favorite 5 My code is the following int tmpCnt; if (name == "Dude") tmpCnt++; Why is there an error Use of unassigned local variable tmpCnt? I know I didn't explicitly initialize it but due to Default Value Table a value type is initialized with 0 anyways. The reference use of unassigned local variable string also reminds me: Remember that using uninitialized variables in C# is not allowed. But why do I have to do it explicitly if it's already done by default? Wouldn't it gain performance if I wouldn't have to do it? Just wondering... c# .net compiler-construction share|improve this question asked Feb 10 '12 at 18:41 theknut 59521231 Can anyone help me with the headline? Couldn't find a fitting for this one :-S –theknut Feb 10 '12 at 18:44 I have a local struct, never initialized, compiles with no errors. Today I created a different struct, treated identically, got "uninitialized local variable error". All its members were set to a value before use, but I could not first set it to null, since it was 'just' a struct. The struct that compiled contained only ints, bools and strings. The one that gave the error also contained DateTimes. "MyStructType myStruct = new MyStructType();" killed the error. Not the first time I've been bitten by missing something a few levels down. –mickeyf Jun 2 at 19:14 add a comment| 9 Answers 9 active oldest votes u
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site
Use Of Unassigned Variable C#
About Us Learn more about Stack Overflow the company Business Learn more about use of unassigned local variable c# datetime hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join how to fix use of unassigned local variable in c# the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Fixing the 'Use of http://stackoverflow.com/questions/9233000/why-compile-error-use-of-unassigned-local-variable unassigned local variable' with a null assignment. Why? up vote 17 down vote favorite 1 With a piece of code like this, the compiler complains on c.MyProperty: MyClass c; try { throw new Exception(); } catch (Exception) { } c.MyProperty = 2; // "Use of unassigned local variable 'c'". Yet it doesn't complain if you assign a null to c in initialization: MyClass c = null; http://stackoverflow.com/questions/6213113/fixing-the-use-of-unassigned-local-variable-with-a-null-assignment-why try { throw new Exception(); } catch (Exception) { } c.MyProperty = 2; // no complains this time. So, why does this work? If c wasn't assigned a null and the compiler hypothetically allowed it, wouldn't the same exception be thrown at c.MyProperty, Object reference not set to an instance of an object? c# exception variable-assignment share|improve this question asked Jun 2 '11 at 10:10 Andreas Grech 51.9k81248326 Hmm, that is interesting, my guess would be though that this code (second version) would still throw an exception at Runtime.. it's likely just a visual studio issue that is not picking up the null assignment - Just a guess ? –Dave Long Jun 2 '11 at 10:13 1 Take a look to stackoverflow.com/questions/4770426/… –Haplo Jun 2 '11 at 10:17 add a comment| 4 Answers 4 active oldest votes up vote 37 down vote accepted When you assign null to the variable you're telling the compiler to back off because you know better than him so he should not complain about this. This is probably due to the fact that assigning null is considered to imply an explicit action by the developer. share|im
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more http://stackoverflow.com/questions/12247698/reason-for-use-of-unassigned-local-variable-error about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Reason for use of unassigned local variable error up vote 0 down vote favorite I'm just wondering why the compiler gives me this error. The try block will use of be executed every time the function is called and that means the variable will get assigned. But still it doesn't let me compile. using System; namespace Checking { class Program { static void Main(string[] args) { int intNum; intNum = readValue("Please enter a number: "); } static int readValue(string strPrompt) { int intRes; Console.WriteLine(strPrompt); try { intRes = Convert.ToInt32(Console.ReadLine()); // Gets assigned here! But still doesnt allow me to compile! } catch (Exception ex) { Console.WriteLine("Please enter use of unassigned a numeric value.\n"); readValue(strPrompt); } return intRes; } } } Putting return intRes inside the try block allows me to get rid of that error, but then an error crops up saying not all code paths return a value. I understand the errors, but I still don't understand why it won't allow me to compile, the try block gets executed every time right? I also know that assigning 0 to intRes will get rid of that error. Regards, c# share|improve this question edited Oct 12 '12 at 20:48 asked Sep 3 '12 at 12:15 Abijeet Patro 1,43912251 add a comment| 5 Answers 5 active oldest votes up vote 3 down vote accepted The compiler is right. The variable is not always assigned. If the conversion fails, the assignment never happens, and the execution continues inside the catch block, where you call the function again, but you have forgotten to assign the return value of that call to the variable: catch (Exception ex) { Console.WriteLine("Please enter a numeric value.\n"); intRes = readValue(strPrompt); } Here's an anternative implementation using a while and TryParse: static int readValue(string strPrompt) { int intRes = 0; bool done = false; while (!done) { Console.WriteLine(strPrompt); if (Int32.TryParse(Console.ReadLine(), out intRes) { done = true; } else { Console.WriteLine("Please enter a numeric value.\n"); } } return intRes; } share|improve this answer edited Sep 3 '12 at 13:00