0

Here is the text file

country1: 93#country2: 355#country3: 213#country4: 376#country5: 244#country6: 54#country7: 374#

For this ASP.NET web services,when i declare string temp ouside "for" loops, Error "Use of unassigned local variable 'temp'"

  [webMethod]
    public string[] getContryCode ()

        {
            string temp;

            string[] data = File.ReadAllLines(@"countryCode.txt");

            string[] country = data[0].Split('#');

            for (int i = 0; i < country.Length; i++ )
            {
                temp = country[i];

            }
                //const string f = "countryCode.txt";

            return temp.Split(':');


        }

If i declare string temp inside a loop,i can't return a value of "temp.Split(':')". Need find a way to solve it

originl file format: #country1:code1#country2:code2# array list 'country': [0] country1 code1 country2 code2 - i can get this work b split temp.split(':') : should get things like this [0]country1 [1] code1 [2] country2 [3] code2

5
  • What is the value of temp when you try to .Split it if country.Length is 0? Commented Aug 30, 2013 at 14:27
  • @Kirk Woll that text file is a country code list, i want get a array from original format #country1:code1#country2:code2# to [0]country1 [1] code1 [2] country 2 [3] code2 Commented Aug 30, 2013 at 14:31
  • I don't think you actually read my question to you. Commented Aug 30, 2013 at 14:31
  • @atom2ueki: Do you have any hashtags accidentally beside each other? As in "##"? Commented Aug 30, 2013 at 14:35
  • 1
    You shouldn't read all of the lines if you only want the first one. That's a lot of wasted effort. Use File.ReadLines("").First() to get the first line without processing more than you need to. Commented Aug 30, 2013 at 14:35

1 Answer 1

5

Your for loop is not guaranteed to iterate once, so you are getting a compiler error that temp may not have a value when you attempt to use it.

Try:

string temp = "";

Even better, though, would be to add proper testing to ensure that all of your inputs are as expected:

if ( !System.IO.File.Exists( @"countryCode.txt" ) )
    throw new ApplicationException( "countryCode.txt is missing" );

string[] data = File.ReadAllLines(@"countryCode.txt");

if ( data.Length == 0 )
    throw new ApplicationException( "countryCode.txt contains no data" );

if ( data[0].Length == 0 )
    throw new ApplicationException( "malformed data in countryCode.txt" );

string[] country = data[0].Split('#');

string temp = "";
for (int i = 0; i < country.Length; i++ )
{
    temp = country[i];
}

return temp.Split(':');

I am not really sure what you are trying to accomplish with the for-loop, though, since you are only going to be returning the last item in the country array, which would be the same as: return country[country.Length - 1];

EDIT:

You may want to remove empty entries in your country array. You can just use the RemoveEmptyEntries option:

string[] country = data[0].Split('#', StringSplitOptions.RemoveEmptyEntries);

/* Using the RemoveEmptyEntries option can cause the function to
   return a 0-length array, so need to check for that afterall: */
if ( country.Length == 0 )
    throw new ApplicationException( "malformed data in countryCode.txt" );
Sign up to request clarification or add additional context in comments.

4 Comments

@Servy - my bad. I was confusing this with VB, which generates a warning (but which many people configure to act as an error)
Regarding your comment on the for loop, I'll bet this is why it's returning an empty string every time. I bet the text file line ends with a "#" character, thus it has a "country" that's just an empty string.
The for loop is still very silly. It overwrites the same variable on each iteration and the end result is to get only the last entry in the array country.
@ChrisSinclair He edited an example line into the question. You are indeed correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.