Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-20
7
Medium Priority
?
240 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 1000 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 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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