Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with simple C# error

Tags:

c#

.net

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!!!!

like image 609
Ryan Rodemoyer Avatar asked May 20 '26 14:05

Ryan Rodemoyer


1 Answers

You aren't initializing the StringBuilder and List.

StringBuilder Output = new StringBuilder();
List<string> Emails = new List<String>();
like image 196
Dave Markle Avatar answered May 23 '26 02:05

Dave Markle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!