Error Use Of Possibly 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 Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads error use of unassigned local variable c# with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the error 1 use of unassigned local variable 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: error cs0165 use of unassigned local variable 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 use of unassigned local variable unity know I didn't explicitly initialize it but due to Default Value Table a value type is initialized with 0 anyways. The reference 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
Use Of Unassigned Local Variable String
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 up vote 55 down vote accepted Local variables aren't initialized. You have to manually initialize them. Members are initialized, for example: public class X { private int _tmpCnt; // This WILL initialize to zero ... } But local variables are not: public static void SomeMethod() { int tmpCnt; // This is not initialized and must be assigned before used. ... } So your code must be: int tmpCnt = 0; if (name == "Dude") tmpCnt++; So the long and the short of it is, members are initialized, locals are not. That is why you get the compiler error. share|improve this answer answered Feb 10 '12 at 18:45 James Michael Hare 25.2k55168 3 Thanks! Didn't know about the difference on members and loca
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
Use Of Unassigned Local Variable Array
Learn more about Stack Overflow the company Business Learn more about hiring developers use of unassigned local variable struct or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack use of unassigned local variable datetime 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 What does “Use of unassigned local variable” http://stackoverflow.com/questions/9233000/why-compile-error-use-of-unassigned-local-variable mean? up vote 17 down vote favorite 3 I keep getting this error for annualRate, monthlyCharge, and lateFee..And for the death of me I cannot figure out why. Any help would be greatly appriciated! using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lab_5___Danny_Curro { class Program { static void Main(string[] args) { string firstName; string lastName; int accNumber; string creditPlan; double balance; string status; Boolean late http://stackoverflow.com/questions/5710485/what-does-use-of-unassigned-local-variable-mean = false; double lateFee; double monthlyCharge; double annualRate; double netBalance; Console.Write("Enter First Name: "); firstName = Console.ReadLine(); Console.Write("Enter Last Name: "); lastName = Console.ReadLine(); Console.Write("Enter Account Number: "); accNumber = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter Credit Card Plan Number[Blank Will Enter Plan 0]: "); creditPlan = Console.ReadLine(); Console.Write("Enter Balance: "); balance = Convert.ToDouble(Console.ReadLine()); Console.Write("Is This Account Late?: "); status = Console.ReadLine().Trim().ToLower(); if (creditPlan == "0") { annualRate = 0.35; //35% lateFee = 0.0; monthlyCharge = balance * (annualRate * (1 / 12)); return; } if (creditPlan == "1") { annualRate = 0.30; //30% if (status == "y") { late = true; } else if (status == "n") { late = false; } if (late == true) { lateFee = 25.00; } monthlyCharge = balance * (annualRate * (1 / 12)); return; } if (creditPlan == "2") { annualRate = 0.20; //20% if (status == "y") { late = true; } else if (status == "n") { late = false; } if (late == true) { lateFee = 35.00; } if (balance > 100) { monthlyCharge = balance * (annualRate * (1 / 12)); } else { monthlyCharge = 0; } return; } if (creditPlan == "3") { annualRate = 0.15; //15% l
help? Post your question and get tips & solutions from a community of 418,547 IT Pros & Developers. It's quick & easy. "Use of possibly unassigned field" compile error P: n/a Chris LaJoie Our company has https://bytes.com/topic/c-sharp/answers/227853-use-possibly-unassigned-field-compile-error been developing a program in C# for some time now, and we haven't had any problems with it, but just last night something cropped up that has me, and everyone else, stumped. I have a struct that contains several different types of data. This struct is used throuout the program. Now, when I compile, I get 6 errors, all of them "Use of possibly unassigned field 'awayTime'" or "Use of possibly unassigned field 'intlTime'". It is correct, use of it is possible that these fields aren't assigned, but I don't care. It's inside of a struct, and everything inside of a struct gets assigned its default value anyway. This is difficult to explain, but if anyone else has had this problem and fixed it, i'd love to know how. Thanks. Chris LaJoie Nov 15 '05 #1 Post Reply Share this Question 10 Replies P: n/a Jon Skeet Chris LaJoie