-2

i would like to know how to resolve the issue "Use of unassigned local variable 'votesArr'. I would like to check if the array is empty first before i do the splitting ',' to store the data inside.

My image is inside the link provided. Please advise. Thanks

Image is shown here

adiga
  • 34,372
  • 9
  • 61
  • 83
Janice
  • 13
  • 3
  • 1
    You need to initialize the variable first before you do a comparison. That said, checking for null after declaring it as null doesn't really make much sense. – Tobias Tengler Feb 12 '19 at 15:07
  • 1
    You have literally just declared that variable. It would never have anything at this point. The error message correctly tells you that. I'm not sure what is there to fix - either don't do that check or assign a value to the variable. – VLAZ Feb 12 '19 at 15:07
  • Please don't upload [images of code](https://meta.stackoverflow.com/a/285557/3082296). They can't be copied to reproduce the issue, aren't searchable for future readers and harder to read than text. Please post the actual code as text to create a [mcve]. – adiga Feb 12 '19 at 15:08
  • Also, is this JavaScript or C#? – VLAZ Feb 12 '19 at 15:08
  • You should move that if condition after `File.ReadAllText()` and then check it's value. – Aria Feb 12 '19 at 15:09
  • 1
    I downvoted because [no MCVE](http://idownvotedbecau.se/nomcve/) and [Images of Code](http://idownvotedbecau.se/imageofcode) – MindSwipe Feb 12 '19 at 15:10
  • Possible duplicate of [C# Use of unassigned local variable c](https://stackoverflow.com/questions/37393671/c-sharp-use-of-unassigned-local-variable-c) – mbj Feb 12 '19 at 15:11

1 Answers1

0

Anyway, you can not use an unassigned variable. here you can just assign a null to the votesArr, I mean:

string[] votesArr = null;
  • 1
    But if you do this, what's the point of putting `if (votesArr == null)` after it? The condition would always be true. – VLAZ Feb 12 '19 at 15:29
  • Yeap, I know. I'v just answered the question, saying "i would like to know how to resolve the issue "Use of unassigned local variable 'votesArr'.". Using an unassigned variable is not possible, If wanna use it elsewhere without assigning, must assign null to it. – Behnam Mirzabeygi Feb 12 '19 at 15:42