2

I'm writing this small program to extract any number of email address from a text file. I am getting two errors, "Use of unassigned local variable." and I'm not sure why.

static void Main(string[] args)
{
string InputPath = @"C:\Temp\excel.txt";
string OutputPath = @"C:\Temp\emails.txt";
string EmailRegex = @"^(?:[a-zA-Z0-9_'^&/+-])+(?:\.(?:[a-zA-Z0-9_'^&/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$";
string Text = String.Empty;
StringBuilder Output;
List<string> Emails;

using (TextReader tr = new StreamReader(InputPath))
{
    Text = tr.ReadToEnd();
}

MatchCollection Matches = Regex.Matches(Text,EmailRegex);

foreach (Match m in Matches)
{
    Emails.Add(m.ToString().Trim()); // one error is here
}

foreach (String s in Emails)
{
    Output.Append(s + ","); // the other error is here
}

using (TextWriter tw = new StreamWriter(OutputPath))
{
    tw.Write(Output.ToString());
}
} 

Sorry for the formatting ... I'm kind of pressed for time!

edit: WOW. I'm an idiot - must be because I'm pressed for time!!!!

0

6 Answers 6

21

You aren't initializing the StringBuilder and List.

StringBuilder Output = new StringBuilder();
List<string> Emails = new List<String>();
Sign up to request clarification or add additional context in comments.

Comments

9

The problem is here:

StringBuilder Output;
List<string> Emails;

You haven't initialized Emails and Output. Try:

StringBuilder Output = new StringBuilder();
List<string> Emails = new List<string>();

Comments

8

Your "Output" and "List" variables are not assigned with object instances. Change:

StringBuilder Output;
List<string> Emails;

To

StringBuilder Output = new StringBuilder();
List<string> Emails = new List<string>();

Comments

8

you are not creating a Stringbuilder or Email List:

StringBuilder Output = new StringBuilder();
List<string> Emails = new List<string>(); 

Comments

4

You have to initialize the Emails and Output objects by using "new". Basically have:

StringBuilder Output = new StringBuilder();
List<string> Emails = new List<string>();

Comments

1

As so many have said, the errors come from the object that you didn't create.

But, why are you storing the email addresses in a temporary list, then putting them in a temporary string, then write it to a file? Just write them to the file directly:

static void Main(string[] args) {

   string InputPath = @"C:\Temp\excel.txt";
   string OutputPath = @"C:\Temp\emails.txt";
   string EmailRegex = @"^(?:[a-zA-Z0-9_'^&amp;/+-])+(?:\.(?:[a-zA-Z0-9_'^&amp;/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$";

   using (TextWriter tw = new StreamWriter(OutputPath))
      foreach (Match m in Regex.Matches(File.ReadAllText(InputPath), EmailRegex)) {
         tw.Write(m.Value);
         tw.Write(',');
      }
   }
}

Note: The regular expression seems to have been corrupted somewhere along the line. There are some &amp; in it that probably should just be & characters.

Comments

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.