Solved

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

Posted on 2007-11-20
7
232 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:wysardry
Comment Utility
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]
Comment Utility
there was a second change:
   if (logReport != null)
                    {
                        PlainText(logReport);  <<< added logReport as parameter here
                    }
0
 
LVL 12

Expert Comment

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

Author Comment

by:wysardry
Comment Utility
Awesome, thank you both!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now