Error 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 error use of unassigned local variable c# policies of this site About Us Learn more about Stack Overflow the use of unassigned local variable c# datetime company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users error 2 use of unassigned local variable 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
Use Of Unassigned Local Variable In C#
a minute: Sign up ERROR: use of unassigned local variable (for string array) up vote 4 down vote favorite 1 I am reading connection strings from my App.config file and for that i have following code. try { string[] dbnames; int counter = 0; foreach (ConnectionStringSettings connSettings in ConfigurationManager.ConnectionStrings) { dbnames[counter] = connSettings.Name; counter++; } return dbnames; } catch { throw; use of unassigned local variable c# int } this code giving me error use of unassigned local variable for dbnames. i will have multiple connection strings in my App.config. They can be none,1,2 and so on. Depending on the needs. so i cant statically assign the dbname size. Because there can be a scenario if they exceed the value of assigned size. eg. if i assign it a size of 5, and what if i get 6th connection string. and if i have 1, then remaining 4 will be a memory wastage. If i am wrong then let me know. Thanks. c# .net arrays c#-4.0 unassigned-variable share|improve this question edited Jan 27 '11 at 10:53 Cody Gray 146k23278368 asked Jan 27 '11 at 10:43 booota 66841226 3 Fix it with: string[] dbnames = null; Run the code, then understand why it does not work. –leppie Jan 27 '11 at 10:48 1 @leppie: Clever, i've +1'd. Also, one could say: uninitialized variable has no initializer. \o/ –Romain Jan 27 '11 at 10:49 yeah right, i tried null and it was then jumping to the exception 'cat
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 use of unassigned local variable int posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss
Use Of Unassigned Local Variable 'result'
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
Error 1 Use Of Unassigned Local Variable C#
takes a minute: Sign up C# Error : Use of unassigned local variable up vote 6 down vote favorite The error is caused in the for loop : for (i = 0; i < hand.Length; i++) { Console.WriteLine(hand[i]); } I http://stackoverflow.com/questions/4815311/error-use-of-unassigned-local-variable-for-string-array am trying to store the values to be able to display them at a later time. The writeline is there to help me make sure the code actually works as I intend it to. The rest of the code for reference: *edit: added a line of code enum house //variable type for the card type { Spades, Hearts, Clubs, Diamonds } enum cards //variable type for the cards { Joker, Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, http://stackoverflow.com/questions/22813697/c-sharp-error-use-of-unassigned-local-variable Queen, King } class Program { static void Main(string[] args) { Random rnd; Random rnd2; int i; int random; int random2; String[] hand; house randomhouse; cards randomcard; //all declared variables Console.WriteLine("Your hand is made up of :"); for (i = 0; i <= 6; i++)//does everything in the {} until i is equal to 6 { rnd2 = new Random(); random2 = rnd2.Next(0, 14); randomcard = (cards)random2; //selecting a random card from joker to king if (randomcard > (int)cards.Joker) //if the random card isn't a joker { rnd = new Random(); random = rnd.Next(0, 4); randomhouse = (house)random;//selects a random card type Console.WriteLine(randomcard + " of " + randomhouse); //outputs the name of the card System.Threading.Thread.Sleep(1000);//wait 1 second before getting the new card } else { Console.WriteLine(randomcard);//outputs "Joker" System.Threading.Thread.Sleep(1000);//wait 1 second before getting the new card } hand = new String[i];//making a new array value for every loop hand[i] = randomcard.ToString();//adding randomcard to the array* } Console.Clear(); for (i = 0; i < hand.Length; i++) { Console.WriteLine(hand[i]); } Console.ReadKey(); } } c# arrays loops unassigned-variable share|improve this question edited Apr 2 '14 at 14:03 asked Apr 2 '14 at 13:53 ardox 4314 1 hand[i] = randomcard, perhaps? It's really not clear what should be there. –raina77ow Apr 2 '14 at 13:55 hand = new String[i]; will completely overwrite the array each time you go through the loop, by the way (i.e. it creates an array of length
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 http://stackoverflow.com/questions/19353335/use-of-unassigned-local-variable 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 http://www.codeproject.com/Questions/681282/c-sharp-doubt-clarifying-error 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 Use use of of unassigned local variable up vote 0 down vote favorite 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 use of unassigned am assigning thought the if statements. class Program { static void Main(string[] args) { //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;
Tips/Tricks Top Articles Beginner Articles Technical Blogs Posting/Update Guidelines Article Help Forum Article Competition Submit an article or tip Post your Blog quick answersQ&A Ask a Question View Unanswered Questions View All Questions... C# questions Linux questions ASP.NET questions SQL questions VB.NET questions discussionsforums All Message Boards... Application Lifecycle> Running a Business Sales / Marketing Collaboration / Beta Testing Work Issues Design and Architecture ASP.NET JavaScript C / C++ / MFC> ATL / WTL / STL Managed C++/CLI C# Free Tools Objective-C and Swift Database Hardware & Devices> System Admin Hosting and Servers Java .NET Framework Android iOS Mobile SharePoint Silverlight / WPF Visual Basic Web Development Site Bugs / Suggestions Spam and Abuse Watch features Competitions News The Insider Newsletter The Daily Build Newsletter Newsletter archive Surveys Product Showcase Research Library CodeProject Stuff communitylounge Who's Who Most Valuable Professionals The Lounge The Insider News The Weird & The Wonderful The Soapbox Press Releases Non-English Language > General Indian Topics General Chinese Topics help What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum Site Map Advertise with us About our Advertising Employment Opportunities About Us Ask a Question All Questions All Unanswered FAQ use of unassigned local variable 'dt' Rate this: Please Sign up or sign in to vote. See more: C# compiler DataTable Hi, I am getting the error as "use of unassigned local variable 'dt'" whenever i am compiling my project. My Code is :using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Windows.Forms; using System.Data; namespace DAL { public class dbHelper { SqlConnection SQLConn; string connectionstring; SqlDataAdapter da; public void openconnection() { connectionstring = "Server=GITTWO-PC\\SQL2008R2;Database=emp;User Id=accpac;Password=super1;"; try { SQLConn = new SqlConnection(connectionstring); SQLConn.Open(); } catch(Exception ex) { MessageBox.Show(" Error Connecting to database! - " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public void closeconnection() { SQLConn.Close(); } public DataTable getdt(string query) { openconnection(); da = new SqlDataAdapter(quer