Solved

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

Posted on 2007-11-20
7
234 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 142

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 142

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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