C Sharp Error 1 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 of this site About
Use Of Unassigned Local Variable C# Array
Us Learn more about Stack Overflow the company Business Learn more about hiring use of unassigned local variable c# for loop developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the
Use Of Unassigned Local Variable C# String
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 C# error: Use of unassigned local use of unassigned local variable c# decimal variable up vote 22 down vote favorite 1 I'm not sure why I'm getting this error, but shouldn't this code compile, since I'm already checking to see if queue is getting initialized? public static void Main(String[] args) { Byte maxSize; Queue queue; if(args.Length != 0) { if(Byte.TryParse(args[0], out maxSize)) queue = new Queue(){MaxSize = maxSize}; else Environment.Exit(0); } else { Environment.Exit(0); } for(Byte j = 0; use of unassigned local variable c# struct j < queue.MaxSize; j++) queue.Insert(j); for(Byte j = 0; j < queue.MaxSize; j++) Console.WriteLine(queue.Remove()); } So if queue is not initialized, then the for loops aren't reachable right? Since the program already terminates with Environment.Exit(0)? Hope ya'll can give me some pointers :) Thanks. c# initialization share|improve this question edited Nov 2 '08 at 0:17 Blair Conrad 98.3k17106100 asked Nov 1 '08 at 20:33 jkidv 1,11131212 6 I can't give you any pointers, but I hope you can get a handle on this one. –wprl Nov 1 '08 at 20:40 add a comment| 5 Answers 5 active oldest votes up vote 65 down vote accepted The compiler doesn't know that the Environment.Exit() is going to terminate the program; it just sees you executing a static method on a class. Just initialize queue to null when you declare it. Queue queue = null; share|improve this answer edited Mar 9 '13 at 1:14 Zaid Masud 7,20634369 answered Nov 1 '08 at 20:35 tvanfosson 350k65568700 add a comment| up vote 9 down vote The compiler doesn't know that Environment.Exit() does not return. Why not just "return" from Main()? share|improve this answer answered Nov 1 '08 at 20:35 Brian 95.2k11187
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
Use Of Unassigned Local Variable C# Unity
more about Stack Overflow the company Business Learn more about hiring developers or posting use of unassigned local variable c# list ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack
Use Of Unassigned Local Variable C# Try Catch
Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Use of unassigned local variable up vote 0 down vote favorite http://stackoverflow.com/questions/256073/c-sharp-error-use-of-unassigned-local-variable i have to build a program that takes the diameter of a pizza and finds how many slices it can hold and the area of each slice, the pizza, it defines the pizza size with constants, but a getting an error with "int numberOfSlice" it is saying use of unassigned local variable, even thought am assigning thought the if statements. class Program { static void Main(string[] args) { http://stackoverflow.com/questions/19353335/use-of-unassigned-local-variable //declarations of Constans const int SMALL_MIN = 12; const int SMALL_MED = 16; const int MED_LARGE = 24; const int LARGE_XLARGE = 30; const int XL_MAX = 36; const int SMALL_SLICE = 8; const int MED_SLICE = 12; const int LARGE_SLICE = 16; const int XL_SLICES = 24; //declarations of varable double pizzaDiameter; int numberOfSlices = 0; double sliceArea; double radius; string userInput = " "; Console.WriteLine("Please enter the diameter of your pizza:"); // tell user to input diameter userInput = Console.ReadLine(); // gets userinput double.TryParse(userInput, out pizzaDiameter); // see if userinput is vaild if (pizzaDiameter >= SMALL_MIN && pizzaDiameter <= XL_MAX) // if in range will continue { // all the ranges for the pizzas if (pizzaDiameter >= SMALL_MIN && pizzaDiameter < SMALL_MED) { numberOfSlices = (SMALL_SLICE); } else if (pizzaDiameter >= SMALL_MED && pizzaDiameter < MED_LARGE) { numberOfSlices = (MED_SLICE); } else if (pizzaDiameter >= MED_SLICE && pizzaDiameter < LARGE_XLARGE) { numberOfSlices = (LARGE_SLICE); } else if (pizzaDiameter >= LARGE_XLARGE && pizzaDiameter <= XL_MAX) { numberOfSlices = (XL_SLICES); } radius = pizzaDiameter / 2; // divides pizzaDiameter to get radius sliceArea = Math.PI * Math.Pow(radius, 2) / numberOfSlices; // gets slice area sliceArea = Math.Round(sliceArea, 2); // rounds to 2 places
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings http://stackoverflow.com/questions/23230787/use-of-unassigned-local-variable-c-sharp-error and policies of this site About Us Learn more about Stack Overflow http://stackoverflow.com/questions/6213113/fixing-the-use-of-unassigned-local-variable-with-a-null-assignment-why the company Business Learn more 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 use of only takes a minute: Sign up Use of unassigned local variable c# error up vote 0 down vote favorite As follows, when i debug it gives me the error : Error 1 Use of unassigned local variable 'moneyBet' I'm not sure what is wrong with the following code. I've never gotten something like that before. using System; using System.Collections.Generic; using use of unassigned System.Linq; using System.Text; using System.Threading.Tasks; namespace MyNotSoVeryFirstApplication { class Program { static void Main(string[] args) { bool stillGoing = true; int moneyBet; int moneyInBank = 0; //change 0 to amount held in actuality while (stillGoing == true) { Console.WriteLine("Money in bank : {0}", moneyInBank); Console.WriteLine("----------------------------------------------------"); Console.Write("Enter amount you would like to bet: "); string moneybetString = Console.ReadLine(); try { moneyBet = Convert.ToInt32(moneybetString); } catch (FormatException e) { Console.WriteLine(e.Message); } catch (OverflowException e) { Console.WriteLine(e.Message); } finally { if (moneyBet > Int32.MaxValue) Console.WriteLine("You are about to bet {0}. Are you sure you want to bet this amount?", moneyBet); } } Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } } c# variables share|improve this question asked Apr 22 '14 at 21:44 Daniel Lukish 3016 What happens if there's an exception during the Convert.ToInt32? –John Saunders Apr 22 '14 at 21:45 2 int moneyBet is never assigned –karthikr Apr 22 '14 at 21:46 add a comment| 5 Answers 5 active oldest votes up vote 1 down vote accepted You need to definitely assign moneyBet before you read it
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 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 Fixing the 'Use of 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; 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|improve this answer answered Jun 2 '11 at 10:12 Joao Angelo 30.9k57597 1 Although that made me laugh, I'm still