[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

HELP ASAP Please..Windows Service Error "The name does not exist in the class or namespace" ..not sure how to fix #

Sorry to be such a novice about this but..... I have a windows service. It contains two separate files. Both have the same namespace name.

I need to pass "bDisplay" into the other file. The get/set and the declaration "private bool m_bIsDisplayed = false;" needs to be in the first file. I have tested it in the file with my function "MsgBxDisplayed" however everytime I received a new message via sockets..it would take the default false value. So as I test I was going to put it into my file where the server.start is. But I keep getting this error and I am not sure how to fix it.

I am getting error CS0103: The name 'm_bIsDisplayed' does not exist in the class or namespace 'Listener.SocketListener' and not sure why or how to solve them does anyone have any help?

How do I access the other file?

ServerSocket.cs *This is where the main server start is so the" private bool m_bIsDisplayed = false;" had to go here so it didn't reset every time.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.IO;
using System.Net;    
using Listener;
using System.Threading;

namespace Listener
{
 private bool m_bIsDisplayed = false;

      public bool GetIsDisplayed()
      {
         return m_bIsDisplayed();
      }

      public void SetIsDisplayed(bool bDisplay)
      {
         m_bIsDisplayed = bDisplay;
      }

}

SocketListner.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Web;
using System.Windows.Forms;

using Listener;

namespace Listener
{

 private void MsgBxDisplayed(Byte [] byteBuffer, int size)
      {
         string sData = Encoding.ASCII.GetString(byteBuffer,0, size);  

         try
         {
            if (m_bIsDisplayed == true)
                 ///Need pass variable here of TRUE or FALSE so it doesn't continue with the default everytime.
            {
             
              System.Diagnostics.EventLog.WriteEntry(this.ToString(),sData);

            }
            else if (m_bIsDisplayed == false)
            {
               
               m_bIsDisplayed = true;
               System.Diagnostics.EventLog.WriteEntry(this.ToString(),sData);

               
           
               DialogResult result = MessageBox.Show("Failure", "WARNING!!", MessageBoxButtons.OK,
                               MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                               MessageBoxOptions.ServiceNotification);

               if(result == DialogResult.OK)
               {
                  // Closes the parent form.
                  //this.Close();
                  m_bIsDisplayed = false;
               }

            }

         }
         catch (Exception e)
         {
            System.Diagnostics.EventLog.WriteEntry(this.ToString(),
            "invalid IsDisplayed" + e.Message);
         }
         }
0
NewMom2Brandon
Asked:
NewMom2Brandon
  • 8
  • 6
1 Solution
 
mglace390Commented:
If I'm understanding your error correctly you just nee to change the accessability level to public or internal
0
 
mglace390Commented:
namespace Listener
{
      private static bool m_bIsDisplayed = false;

      public static bool IsDisplayed
      {
         get { return m_bIsDisplayed; }
         set { m_bIsDisplayed = value;  }
      }
}


if (IsDisplayed == true)
{
     //...
}
else if (IsDisplayed == false)
{
     //....
}
0
 
NewMom2BrandonAuthor Commented:
Which one would I need to change. I changed the MsgBxDisplayed to public and still had the errors.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mglace390Commented:
You need to make the variables static I believe, then you must reference the public variable not the private one:

namespace Listener
{
      private static bool m_bIsDisplayed = false;

      public static bool IsDisplayed
      {
         get { return m_bIsDisplayed; }
         set { m_bIsDisplayed = value;  }
      }
}
0
 
NewMom2BrandonAuthor Commented:
mglace390

It looks like your suggestion is to have the get/set in the same file. I tried this in the same file and it kept defaulting to false everytime I received a new string from the client and poping up messageboxes on top of each other.
0
 
mglace390Commented:
Sorry, maybe I'm not being clear.  Here is what I proposed:

// this is file 1 and the starting point for your service.  The static variables need to be defined within a class like so.

using System;

namespace Listener
{

      class Class1
      {

            private static bool m_IsDisplayed = false;

            public static bool IsDisplayed
            {
                  get { return clsMain.m_IsDisplayed;  }
                  set { clsMain.m_IsDisplayed = value; }
            }
            
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  //
                  // TODO: Add code to start application here
                  //
            }
      }
}

// this is file 2 where you need to reference the variables.  Access them through the public property IsDisplayed

using System;

namespace Listener
{
      /// <summary>
      /// Summary description for Class2.
      /// </summary>
      public class Class2
      {
            public Class2()
            {

                  if (Class1.IsDisplayed)
                  {
                        // IsDisplayed = true;
                  }
                  else
                  {
                        // IsDisplayed = false;
                  }

            }
      }
}

Hope this is a little more clear.
0
 
NewMom2BrandonAuthor Commented:
How should I do the reference back to the other file?
0
 
NewMom2BrandonAuthor Commented:
sorry ignore my last post.
0
 
NewMom2BrandonAuthor Commented:
can't use static becasue it creates and instance....
0
 
mglace390Commented:
It shouldn't, the very purpose of static to to reference a class's members without creating an instance of that class.  Are you getting a specific error you could post for me to take a look at?
0
 
NewMom2BrandonAuthor Commented:
I am sorry...I actually need the instance though
0
 
mglace390Commented:
Ahh, well in that case you can still create the instance of class1 and leave the variables static so that they are global

- OR -

you can do it this way:

using System;

namespace Listener
{

     class Class1
     {

          private bool m_IsDisplayed = false;

          public bool IsDisplayed
          {
               get { return clsMain.m_IsDisplayed;  }
               set { clsMain.m_IsDisplayed = value; }
          }
         
          /// <summary>
          /// The main entry point for the application.
          /// </summary>
          [STAThread]
          static void Main(string[] args)
          {
               //
               // TODO: Add code to start application here
               //
          }
     }
}

// this is file 2 where you need to reference the variables.  Access them through the public property IsDisplayed

using System;

namespace Listener
{
     /// <summary>
     /// Summary description for Class2.
     /// </summary>
     public class Class2
     {
          public Class2()
          {

               Class1 cls = new Class1();

               if (cls.IsDisplayed)
               {
                    // IsDisplayed = true;
               }
               else
               {
                    // IsDisplayed = false;
               }

          }
     }
}
0
 
NewMom2BrandonAuthor Commented:
hmmm thank you. You are putting me on the right track. It is still poping up multiple boxes for me but at least it is getting the value from the other file.
0
 
NewMom2BrandonAuthor Commented:
Just and FYI for future people. When you try to access another class make sure your namespace name is not the same as your class name.

Do this:

namespace NewEventListener
{
   public class EventListener : System.ServiceProcess.ServiceBase
   {

Not:

namespace EventListener
{
   public class EventListener : System.ServiceProcess.ServiceBase
   {

This was the only thing stopping my code from working correctly.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now