Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, September 13, 2013 6:57 PM
I have a Foreach loop grabbing specific errors from a log. My goal is to put the gathered data in a variable to use outside the scope of the Foreach. The following is what I have but doesn't quite work. The variable theEntireLine is only the last match and outside the scope of the foreach it's not recognized. Can you help?
MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Multiline);
* foreach (Match match in matches)*
* {*
* string theEntireLine = match.Groups[0].Value;*
* Console.WriteLine(theEntireLine);*
* }*
* {*
* System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); *
* message.To.Add("[email protected]");*
* message.Subject = "Error Detected in your stuff";*
* message.From = new System.Net.Mail.MailAddress("[email protected]");*
* message.Body = " Errors captured at " + DateTime.Now + " : The following lines are from the log: " + theEntireLine;*
* System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("outlookclient.sjmc.org");*
* smtp.Send(message);*
* }*
Please mark my post as helpful or the answer or better yet.... both! :) Thanks!
All replies (7)
Friday, September 13, 2013 7:04 PM ✅Answered | 1 vote
string theEntireLine = "";
foreach (Match match in matches)
{
theEntireLine = theEntireLine + match.Groups[0].Value;
}
Saturday, September 14, 2013 7:39 AM ✅Answered | 1 vote
To give you another choice you can also use a StringBuilder which is more efficient than a string in loops
StringBuilder theEntireLine = new StringBuilder();
foreach (Match match in matches)
{
string lastLine = match.Groups[0].Value;
theEntireLine.AppendLine(lastLine);
Console.WriteLine(lastLine);
}
And when you want to write it you can do
message.Body = " Errors captured at " + DateTime.Now + " : The following lines are from the log: " + theEntireLine.ToString();
Friday, September 13, 2013 7:13 PM | 1 vote
The problem is that this line is inside the loop:
string theEntireLine = match.Groups[0].Value;
What you are doing is declaring the variable again for every cycle of the loop. Instead, the variable should be declared outside of the loop and then the desired string should be appended to the string, like this:
theEntireLine = theEntireLine + match.Groups[0].Value;
Another solution would be to use a string builder, instead of just appending a string each time. The string builder is specifically designed to be more efficient in scenarios like this one.
Friday, September 13, 2013 8:29 PM
Hi zperryz;
As the other post have pointed out that you are declaring that variable theEntireLine inside the loop and therefore only seeing the last value. You need to declare the variable outside the loop but you should declare it as a collection so that you can capture all results as the code snippet shows below.
List<string> theEntireLine = new List<string>();
foreach (Match match in matches)
{
theEntireLine.Add(match.Groups[0].Value);
Console.WriteLine(theEntireLine.Last());
}
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.
Saturday, September 14, 2013 2:24 PM
Declaring the variable outside the loop fixed the scope issue and the "theEntireLine = theEntireLine + match.Groups[0].Value;" created the string I was looking for thanks!
Please mark my post as helpful or the answer or better yet.... both! :) Thanks!
Saturday, September 14, 2013 2:25 PM
Very helpful. I will try out the string builder. Thanks!
Please mark my post as helpful or the answer or better yet.... both! :) Thanks!
Saturday, September 14, 2013 2:26 PM
This is a better solution! StringBuilder rocks. Thanks for your help.
Please mark my post as helpful or the answer or better yet.... both! :) Thanks!