Error 41 Use Of Unassigned Local Variable C#
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 Us Learn more about Stack Overflow the company Business Learn more about hiring developers use of unassigned local variable c# array or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question use of unassigned local variable c# for loop 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; use of unassigned local variable c# string it only takes a minute: Sign up Why did I get “Use of unassigned local variable” error? up vote -2 down vote favorite I tried to do a simple thing and it gave me an error. The error is: Use use of unassigned local variable c# decimal of unassigned local variable "answer" Where did I go wrong? using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int l; int w; int h; Console.WriteLine("Please Enter th points"); Console.Write("Length: "); l = int.Parse(Console.ReadLine()); Console.Write("Width: "); w = int.Parse(Console.ReadLine()); Console.Write("Height: "); h = int.Parse(Console.ReadLine()); int answer; Console.WriteLine("Enter what you want to Do [S,P,V]"); string cupSize = Console.ReadLine(); switch (cupSize) { case "s": answer = (l * w); break; case "S":
Use Of Unassigned Local Variable C# Struct
answer = (l * w); break; case "p": answer = ((l + w) * 2); break; case "P": answer = ((l + w) * 2); break; case "v": answer = (l * w * h); break; case "V": answer = (l * w * h); break; default: Console.WriteLine("Try agian"); break; } if (answer != 0) { Console.WriteLine("The answer is " + answer ); } } } } c# version-control share|improve this question edited Sep 11 '13 at 21:39 p.s.w.g 97.4k15134184 asked Sep 11 '13 at 21:33 user2770428 6 3 bla bla bla? Change to int answer = 0; –Oliver Sep 11 '13 at 21:35 Unrelated to your question, but you can stack the cases so you don't have to duplicate the code. case "s": case "S": answer = (l*w); break;. Or optionally convert the input to lower case and check that case. –Cemafor Sep 11 '13 at 21:59 You are initializing 'answer' but 'default' in your switch statement does not assign it to anything. –Milne Sep 11 '13 at 22:38 add a comment| 4 Answers 4 active oldest votes up vote 4 down vote You have to set the value of answer in every possible code path, but if your switch-block goes to the default case, it won't be set. Either set the value at the time it's declared: int answer = 0; Or in your default case: default: answer = 0;
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
Use Of Unassigned Local Variable C# Unity
this site About Us Learn more about Stack Overflow the company Business Learn use of unassigned local variable c# list more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question use of unassigned local variable c# try catch 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 http://stackoverflow.com/questions/18751657/why-did-i-get-use-of-unassigned-local-variable-error Use of unassigned local variable error #2 up vote 0 down vote favorite I've looked on google and other stackoverflow Use of unassigned local variable errors and I still cannot find the answer. I think that maybe my error is cause be misusing the scopes of ExtractionCtrl. I tried this code to test the scope and it works. So I don't know where is http://stackoverflow.com/questions/12571735/use-of-unassigned-local-variable-error-2 my mistake now. Testing the scope namespace RandomTesting { internal class Program { private static void Main(string[] args) { int x = 5; switch (x) { case 2: System.Console.WriteLine("Your # is 2"); break; case 5: System.Console.WriteLine("Your # is :{0}", x); x = x + 2; System.Console.WriteLine("Your # is :{0}", x); break; } System.Console.WriteLine("Your # is :{0}", x); Console.ReadLine(); } } } PART OF THE MAIN CODE switch (arg) { case "AR": ExtractionCtrl = new ARExtractionController(); // add new mapping here break; case "ICN": ExtractionCtrl = new IcnExtractionController(); // add new mapping here break; } int ticketID; if (int.TryParse(arg, out ticketID)) { string returnedFilePath = ExtractionController.GetStartupPath(); ExtractionCtrl.Extract(ticketID, returnedFilePath, AR_TEMPLATE_PATH, MAPPING_PATH); } c# .net variables scope share|improve this question edited Dec 12 '12 at 15:29 John Saunders 138k20175321 asked Sep 24 '12 at 19:39 Conrad C 428725 1 was the value of arg actually "AR" or "ICN"? Note the case sensitivity. –CaffGeek Sep 24 '12 at 19:41 1 Where are you getting the error? If it's because ExtractionCtrl isn't assigned, then please post the code for where it is defined and where you assign it a value &ndas
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 http://stackoverflow.com/questions/23230787/use-of-unassigned-local-variable-c-sharp-error 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 http://www.dreamincode.net/forums/topic/286239-use-of-unassigned-local-variable-invalue/ 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 Use of unassigned local variable c# use of 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 System.Linq; using System.Text; using System.Threading.Tasks; namespace MyNotSoVeryFirstApplication { class Program { static void Main(string[] args) { bool stillGoing = use of unassigned 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 in the line: if (moneyBet > Int32.MaxValue) if Convert.ToInt32(moneybetString); throws an exception it will not be assigned. The specification describes the rules for definite assignment in try/finally blocks: 5.3.3.14 Try-finally statements For a try statement stmt of th
of 1 New Topic/Question Reply 6 Replies - 2748 Views - Last Post: 18 July 2012 - 04:38 PM Rate Topic: #1 wolne New D.I.C Head Reputation: 0 Posts: 37 Joined: 28-June 12 Use of unassigned local variable 'inValue' Posted 18 July 2012 - 03:19 PM I cant figure out why Im getting the error "use of unassigned local variable 'inValue,'" when I have it assigned to the file being read in. Any help would be awesome. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.IO; namespace ConsoleApplication5 { class Program { private static StreamReader inFile; //Declares a file stream object static void Main(string[] args) { string inValue; try { inFile = new StreamReader("MessageIn.txt"); //assumes file is in the /bin/Debug folder while ((inValue = inFile.ReadLine()) != null) { Console.WriteLine(inValue); } } catch (System.IO.IOException) { Console.WriteLine("ERROR"); } char[] inValueArray = inValue.ToCharArray(); int[] CountArray = new int[49]; int[] indexArray = new int[26]; int max = 0; for (int i = 0; i < inValue.Length; i++) { CountArray[i] = (char)inValueArray[i]; } for (int i = 0; i < 26; i++) indexArray[i] = 0; int count = 0; int freq = 0; while (count < inValue.Length) { freq = (char)inValueArray[count]; indexArray[freq - 65]++; count++; } for (int i = 0; i < 26; i++) if (indexArray[i] > max) max = indexArray[i]; char[] AArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); int shift = Array.IndexOf(indexArray, 13) - Array.IndexOf(AArray, 'E'); for (int i = 0; i < inValue.Length; i++) { int output = CountArray[i] - shift; if (output < 65) output = 91 - (65 - output); char x = (char)output; Console.Write(x); } } } } The error occurs in line 31. char