Solved

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

Posted on 2007-11-20
7
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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: 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.

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

615 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