Questions tagged [unassigned-variable]

93 questions
3
votes
6 answers

Use of unassigned local variable - if statements

I'm doing the following block of code and the compiler is complaining about unassigned local variables and could use some help identifying what's up. while (rsData.Read()) { if (rsData["TYPE"] != DBNull.Value) strType =…
PipBoy
  • 1,127
  • 4
  • 11
  • 16
2
votes
5 answers

C# error CS0165: Use of unassigned local variable - ignoring logic and out reference

After searching around I cant seem to locate why the C# compiler is complaining that the local variable dteDest is unassigned in the line if (dteSrc == dteDest) { The error goes away if I replace the line DateTime dteSrc, dteDest; with DateTime…
Access IT
  • 95
  • 1
  • 2
  • 9
2
votes
1 answer

Local variable is unassigned IF I'm going to change it later in the function

player_health = 10 def add(num): return num + player_health def monster_room(): print(player_health) # Error on this line player_health -= 1 # Doesn't crash without this line, even though the error is thrown on the previous line.…
2
votes
4 answers

Use of unassigned local variable? Can someone explain?

I'm new to writing codes, I understand a few but I can't make some things work. I want this to make the user choose a category first before giving them access to one but I get the error saying "use of the unassigned local variable". Did I do…
Miss Silence
  • 21
  • 1
  • 5
2
votes
1 answer

assigning variables in if statements c#

My problem is this: I am writing a console application for c# that involves user authentication. I have declared a User class and placed all existing users in a list. I have this block to see if the user's input matches an existing username in the…
2
votes
1 answer

Compiler error says I have not assigned a local variable & names do not exist in current context while it does?

I wrote this code, it seems to have some errors. These are the errors I am getting: For loopteller++; I get a error "Use of unassigned local variable loopteller" For all my intpos I get this error "Does not exist in the current context" The goal of…
2
votes
4 answers

Unassigned local variable in one time of several?

I have next code: static void Main(string[] args) { byte currency; decimal amount; if (Byte.TryParse("string1", out currency) && Decimal.TryParse("string2", out amount)) { Check(currency, amount); } …
abatishchev
  • 98,240
  • 88
  • 296
  • 433
2
votes
5 answers

Use of unassigned variable?

I'm getting the error use of unassigned variable "ps" when declaring if paymentstatus is null or has value in the "if" statement. I'm thinking that i allready declared ps but obviously im doing something wrong. Why does the compiler complain about…
WhoAmI
  • 1,188
  • 6
  • 17
  • 47
2
votes
5 answers

Why am I getting this Error in C# Compiler "Unassigned Local Variable"

I am getting the following error. Use of unassigned local variable markduplicate. I don't understand why? This program finds a duplicate in an array. I been trying to figure it out and I feel like im so close. Thanks for the help. using…
2
votes
3 answers

Mathematical function, unassigned variables?

I am looking for a way to add together multiple mathematical functions before assigning the numerical values for the variables in the equations. I am doing it this way because I need to optimize my code, and I want to assign different values to the…
Paul Terwilliger
  • 1,596
  • 1
  • 20
  • 45
1
vote
5 answers

Use of unassigned variable (string)

I have a piece of code that iterates over XML attributes: string groupName; do { switch (/* ... */) { case "NAME": groupName = thisNavigator.Value; break; case "HINT": // use groupName But…
Patryk
  • 22,602
  • 44
  • 128
  • 244
1
vote
6 answers

C# - How to use unassigned variable in try catch block

crmFactory.RegisterDemoAccount throws Exception. In order to use the variable res I need to initialize it. Since AccountRegistrationResponse is not initializable, how can I declare res without getting compilation errors about using unassigned…
SexyMF
  • 10,657
  • 33
  • 102
  • 206
1
vote
1 answer

Error CS0165 Use of unassigned local variable 'json'

I am trying to get value from the HttpContext with a key Correlation-Context, but I am not sure why I am getting the error while trying to use the variable json. internal static CorrelationContext GetCorrelationContext(this IHttpContextAccessor…
1
vote
4 answers

Error 2 Use of unassigned local variable 'Y'

Again i run into an error i don't mean to bug anyone but I'm getting an error on this code: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Input_Program { class Program { private…
unknown
  • 57
  • 3
  • 11
1
vote
2 answers

Use of unassigned local variable - but I know that by the time the program reaches it, it will be assigned

So, I have this section of code: void Readfile() { using (reader = new StreamReader(file)) { string line = ""; DataTable table; // Search for relevant "tables" in the file while ((line = reader.ReadLine()) !=…
Ben
  • 2,433
  • 5
  • 39
  • 69