954,152 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

The name 'srvr' does not exist in the current context

Hi everyone, I'm trying to write a server application, but I'm having a bit of trouble. I keep getting this error

The name 'srvr' does not exist in the current context

I'm pretty sure I know what the problem is, but I don't know how to fix it. I have three classes so far: Program, notify_icon, and server. Program has the the main method which creates a server object and a notify icon object. Server does the work, notify_icon allows you to stop and start the server so notify_icon has to call server's start and stop methods. I think the problem is notify_icon's methods cannot access server's methods, but I'm not sure what to do I could move the code from notify_icon to Program, but I would like to keep it separate.

Let me know if you need me to try and explain something more.

Here is the main method from program

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Server srvr = new Server();
            Notify_Icon icon = new Notify_Icon();
            Application.Run();
        }


this is the notify icon class

class Notify_Icon
    {
        public Notify_Icon () {
                    NotifyIcon icn = new NotifyIcon();
            icn.Visible = true;
            Stream strm = Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsFormsApplication1.Globe.ico");
            icn.Icon = new Icon(strm);
            ContextMenu menu = new ContextMenu();
            menu.MenuItems.Add(0,
                new MenuItem("Start", new System.EventHandler(srvr.Start)));
            menu.MenuItems.Add(1,
                new MenuItem("Stop", new System.EventHandler(srvr.Stop)));
            icn.ContextMenu = menu;
    }

Can someone please help me with this.

curt22
Junior Poster in Training
57 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

The class Notify_Icon can't acces "srvr" because it is not declared outside the main function, try to add the Server as a parameter in the Notify_Icon constructor like

class Notify_Icon
{
public Notify_Icon (Server srvr)
{
....
}
}

miculnegru
Light Poster
33 posts since Nov 2005
Reputation Points: 15
Solved Threads: 5
 

Thanks miculnegru, but I need both of the objects to call each others methods (so that server can change the icon and change its text from Running to Stopped).

I think I might need to rewrite this, but I'm not sure of any way to do it.

curt22
Junior Poster in Training
57 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Separate out the classes, and create a singleton

public class Server
{
private bool _stopped;
public bool Stopped
{
get
{
return _stopped;
}
set
{
_stopped = value;
}
}
    private static Server _instance = new Server();

public static Server instance
{
return _instance;
}
}



//from your notify icon class
Server.instance.Stopped = true;
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

Ok now the error says

The name 'icon' does not exist in the current context

I changed the code to this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Drawing;

namespace Serveri
{
    class Notify_Icon
    {
        public NotifyIcon icn;
        public Notify_Icon()
        {
             this.icn = new NotifyIcon();
            this.icn.Visible = true;
            Stream strm = Assembly.GetExecutingAssembly().GetManifestResourceStream("Serveri.Globe.ico");
            this.icn.Icon = new Icon(strm);
            ContextMenu menu = new ContextMenu();
            menu.MenuItems.Add(0,
                new MenuItem("Start", new System.EventHandler(Server.instance.Start)));
            menu.MenuItems.Add(1,
                new MenuItem("Stop", new System.EventHandler(Server.instance.Stop)));
            this.icn.ContextMenu = menu;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Serveri
{
    public class Server
    {
        public void Start(object sender, EventArgs e) { }
        public void Start() {
            icon.icn.text = "Running";
        }
        public void Stop(object sender, EventArgs e) { }
        public void Stop() {
            icon.icn.text = "Stopped";
        }
        private static Server _instance = new Server();

        public static Server instance
        {
            get
            {
                return _instance;
            }
        }
    }
}
Server srvr = new Server();
            Notify_Icon icon = new Notify_Icon();
            srvr.Start();
            Application.Run();


I need some way for the two classes to be able to access each others methods.

curt22
Junior Poster in Training
57 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

The server doesn't know about the icon, because its part of a separate class

I forgot to add the private constructor to the server class, plus a few things to do

try this, add this to the server class

private Server()
{
}
private string _status;
public string Status
{
get
{
return _status;
}
}

public void Stop()
{
_status = "Stopped";
}


//in your last piece of code
icon.text = Server.instance.Status;
//might need to be this
icon.icn.text = Server.instance.Status;
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

Then why not pass srvr to the Notify_icon ?

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

If you pass server to it, any other class that uses this notify icon will require to instantiate it with server, when possibly they might not have anything to do with server.

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 
Then why not pass srvr to the Notify_icon ?

Because then Server cannot access Notify_Icon thats my problem I can't get them both to see each other.

curt22
Junior Poster in Training
57 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Only other way I can think of (and its 1am so that doesnt mean much) is to have a kinda parent/child relationship so that the server has a notify_icon option and the notify_icon has a server option.. but thats more messy than you need and the other suggesitons so far are the better way

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

Thanks I think I'm going to try using nested classes see if i can get that working. (Oh and its only 8pm here).

curt22
Junior Poster in Training
57 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

If you're not into the singleton thing, you need to think of the separation between the display and server.

Look at using events,
example when server is started, you catch the event in the notify icon and update the display properly, without having the server ever know about what is displaying in the ui portion.

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 
If you're not into the singleton thing, you need to think of the separation between the display and server.

I dont' mind using a singleton. It just didn't work.
And I'm not sure how to do it using events.

curt22
Junior Poster in Training
57 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 
Look at using events, example when server is started, you catch the event in the notify icon and update the display properly, without having the server ever know about what is displaying in the ui portion.

Could you give me an example of how to do that I think it sounds like the best solution.

curt22
Junior Poster in Training
57 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

Here is one on Microsoft, a little technicalhttp://msdn.microsoft.com/en-us/library/aa645739(VS.71).aspx


Here is a little less technical and probably easier to follow
http://www.csharphelp.com/archives/archive253.html

Once you read them,
create the event in the server class, such OnServerStarted

in the notify class, create a server private variable and subscribe to each event

then on each event that is received, you can update your text as the event is received

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 
If you pass server to it, any other class that uses this notify icon will require to instantiate it with server, when possibly they might not have anything to do with server.

Ok, so why not create overload methods, one for srvr.Start() and one for srvr.Stop() ?

I'm sorry but i still can't see , looking at the code so far, why it will not work :?: . so let's say you have this in main()

Server srvr= new Server();
Notify_Icon ico=new Notify_Icon();
srvr.Start(ico);
// do something..
srvr.Stop(ico);

and in other functions you will have :

srvr.Start();
// do something..
srvr.Stop();

now... if this will not work .... then i'm missing the whole point so sorry :| .

miculnegru
Light Poster
33 posts since Nov 2005
Reputation Points: 15
Solved Threads: 5
 

Ok, so why not create overload methods, one for srvr.Start() and one for srvr.Stop() ?

I'm sorry but i still can't see , looking at the code so far, why it will not work :?: . so let's say you have this in main()

Server srvr= new Server(); Notify_Icon ico=new Notify_Icon(); srvr.Start(ico); // do something.. srvr.Stop(ico);

and in other functions you will have :

srvr.Start(); // do something.. srvr.Stop();

now... if this will not work .... then i'm missing the whole point so sorry :| .


The problem was that I couldn't get the server and icon to both be able to access each others methods, but now that you show me what you ment I think it probably would have worked that way. But I've already rewritten it :( oh well at least it works :) . Thanks anyway.

curt22
Junior Poster in Training
57 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

uh.. a sorry then for the late post.. work keeped me busy.

miculnegru
Light Poster
33 posts since Nov 2005
Reputation Points: 15
Solved Threads: 5
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You