Solved

Getting a "...does not exist in current context" error: stumped

Posted on 2007-11-20
7
235 Views
Last Modified: 2010-04-15
Hi, this has me going in circles, if someone could explain it I'd be grateful.

Following code works just fine, UNTIL I try to add "logReport" to be the mail message's body; it then barfs up an unsurprising but irritating

Error      1      The name 'logReport' does not exist in the current context      C:\Documents and Settings\dlambe\My Documents\Visual Studio 2005\Projects\SchedulerAlert\SchedulerAlert\Program.cs      82      25      SchedulerAlert

I can't seem to get 'logReport' in the state it needs to be in to be used as a variable in the PlainText method. Please help. :)
using System;
using System.IO;
using System.Net.Mail;
using System.Text.RegularExpressions;
 
namespace FDI
{
    public class SchedMon
    {
        public static void Main()
        {
            try
            {
                if (File.Exists("C:\\Documents and Settings\\dlambe\\Desktop\\SchedLgU.Old.Txt"))
                    File.Delete("C:\\Documents and Settings\\dlambe\\Desktop\\SchedLgU.Old.Txt");
                // copy the SchedLgU.Txt file to a usable copy; it's locked by the
                // Scheduler process, and doesn't allow parsing
                File.Copy("C:\\Documents and Settings\\dlambe\\Desktop\\SchedLgU.Txt", "C:\\Documents and Settings\\dlambe\\Desktop\\SchedLgU.Old.Txt");
                // create an instance of StreamReader to read from the copied file
                // the using statement also closes the StreamReader
                using (StreamReader sr = new StreamReader("C:\\Documents and Settings\\dlambe\\Desktop\\SchedLgU.Old.Txt"))
                {
                    string logLine;
                    string logReport = null;
                    DateTime dtNow = DateTime.Now; //current time
                    DateTime dtHourBefore = DateTime.Now; //one hour before current time
                    dtHourBefore = dtHourBefore.AddHours(-1);
                    // read and display lines from the file until the end of 
                    // the file is reached.
                    while ((logLine = sr.ReadLine()) != null)
                    {
                        Match m = Regex.Match(logLine, @"([0-9]|1[0-2]):([0-5][0-9]):([0-5][0-9])\s([AaPp][Mm])");
                        if (logLine.Contains("ERROR")) //check for error string
                        {
                            if (logLine.Contains(DateTime.Now.ToString("dd/MM/yyyy"))) //check for current date
                            {
                                if (m.Success) //check for regex time match success
                                {
                                    string strM = Convert.ToString(m);
                                    DateTime dtM = DateTime.Parse(strM);
                                    // If the match (dtM) is greater than an hour before it (dtHourBefore), and still
                                    // less than the current time (dtNow), log the line it was found in
                                    if (dtHourBefore < dtM && dtM >= dtNow)
                                    {
                                        logReport = logLine + "\r\n \r\n" + logLine;
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                    if (logReport != null)
                    {
                        PlainText();
                    }
                    Console.WriteLine(logReport); //REMOVE
                    Console.ReadLine(); //REMOVE
                }
            }
            catch (Exception e)
            {
                //barf out error if something goes wrong
                Console.WriteLine("There was a problem:");
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
        public static void PlainText()
        {
            //create the mail message
            MailMessage mail = new MailMessage();
 
            //set the addresses
            mail.From = new MailAddress("scheduler@buh.net");
            mail.To.Add("recipient@buh.net");
 
            //set the content
            mail.Subject = "Scheduler Alert from"; //TODO get WMI box name
            mail.Body = logReport;
 
            //send the message through server
            SmtpClient smtp = new SmtpClient("10.0.0.50");
            smtp.Send(mail);
        }
    }
}

Open in new window

0
Comment
Question by:wysardry
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 15

Expert Comment

by:crisco96
ID: 20320224
Can't you make logReport a parameter to PlainText or a global variable?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 20320237
you 2 options:
either you define logreport as a class-level variable, or you pass it as parameter to the method PlainText.

let me suggest the second syntax:
using System;
using System.IO;
using System.Net.Mail;
using System.Text.RegularExpressions;
 
namespace FDI
{
    public class SchedMon
    {
        public static void Main()
        {
            try
            {
                if (File.Exists("C:\\Documents and Settings\\dlambe\\Desktop\\SchedLgU.Old.Txt"))
                    File.Delete("C:\\Documents and Settings\\dlambe\\Desktop\\SchedLgU.Old.Txt");
                // copy the SchedLgU.Txt file to a usable copy; it's locked by the
                // Scheduler process, and doesn't allow parsing
                File.Copy("C:\\Documents and Settings\\dlambe\\Desktop\\SchedLgU.Txt", "C:\\Documents and Settings\\dlambe\\Desktop\\SchedLgU.Old.Txt");
                // create an instance of StreamReader to read from the copied file
                // the using statement also closes the StreamReader
                using (StreamReader sr = new StreamReader("C:\\Documents and Settings\\dlambe\\Desktop\\SchedLgU.Old.Txt"))
                {
                    string logLine;
                    string logReport = null;
                    DateTime dtNow = DateTime.Now; //current time
                    DateTime dtHourBefore = DateTime.Now; //one hour before current time
                    dtHourBefore = dtHourBefore.AddHours(-1);
                    // read and display lines from the file until the end of 
                    // the file is reached.
                    while ((logLine = sr.ReadLine()) != null)
                    {
                        Match m = Regex.Match(logLine, @"([0-9]|1[0-2]):([0-5][0-9]):([0-5][0-9])\s([AaPp][Mm])");
                        if (logLine.Contains("ERROR")) //check for error string
                        {
                            if (logLine.Contains(DateTime.Now.ToString("dd/MM/yyyy"))) //check for current date
                            {
                                if (m.Success) //check for regex time match success
                                {
                                    string strM = Convert.ToString(m);
                                    DateTime dtM = DateTime.Parse(strM);
                                    // If the match (dtM) is greater than an hour before it (dtHourBefore), and still
                                    // less than the current time (dtNow), log the line it was found in
                                    if (dtHourBefore < dtM && dtM >= dtNow)
                                    {
                                        logReport = logLine + "\r\n \r\n" + logLine;
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                    if (logReport != null)
                    {
                        PlainText(logReport);
                    }
                    Console.WriteLine(logReport); //REMOVE
                    Console.ReadLine(); //REMOVE
                }
            }
            catch (Exception e)
            {
                //barf out error if something goes wrong
                Console.WriteLine("There was a problem:");
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
        public static void PlainText(string logReport)
        {
            //create the mail message
            MailMessage mail = new MailMessage();
 
            //set the addresses
            mail.From = new MailAddress("scheduler@buh.net");
            mail.To.Add("recipient@buh.net");
 
            //set the content
            mail.Subject = "Scheduler Alert from"; //TODO get WMI box name
            mail.Body = logReport;
 
            //send the message through server
            SmtpClient smtp = new SmtpClient("10.0.0.50");
            smtp.Send(mail);
        }
    }
}

Open in new window

0
 
LVL 12

Assisted Solution

by:topdog770
topdog770 earned 250 total points
ID: 20320248
problems that I see:

1) string LogReport is defined within the main function so it won't be visible to your PlainText method.

These changes to your code should make it work

public class SchedMon
    {
static string LogReport = "";

 public static void Main()
        {
  // ... code
 
 // string logReport = null;


0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:wysardry
ID: 20320463
crisco96 - I didn't know how to get the right syntax :)

angelIII - I can't see any changes other than the "string logReport" added to the PlainText method; if that's all that was changed and I'm not blind (always argueable ;), I get a

Error      1      No overload for method 'PlainText' takes '0' arguments      C:\Documents and Settings\dlambe\My Documents\Visual Studio 2005\Projects\SchedulerAlert\SchedulerAlert\Program.cs      58      25      SchedulerAlert

topdog770 - worked great! i tried a bunch of different ways and locations, but I never tried - and still don't quite get the reasoning - behind the "static" designation making it work. Could I bother you for a quick one-liner explanation?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20320508
there was a second change:
   if (logReport != null)
                    {
                        PlainText(logReport);  <<< added logReport as parameter here
                    }
0
 
LVL 12

Expert Comment

by:topdog770
ID: 20320540
Because your two functions are static, the variable has to be static as well.
0
 

Author Comment

by:wysardry
ID: 20320553
Awesome, thank you both!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question