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

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 56
Reputation: curt22 is an unknown quantity at this point 
Solved Threads: 0
curt22 curt22 is offline Offline
Junior Poster in Training

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

 
0
  #1
Aug 27th, 2008
Hi everyone, I'm trying to write a server application, but I'm having a bit of trouble. I keep getting this error
  1. 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
  1. static void Main()
  2. {
  3. Application.EnableVisualStyles();
  4. Application.SetCompatibleTextRenderingDefault(false);
  5. Server srvr = new Server();
  6. Notify_Icon icon = new Notify_Icon();
  7. Application.Run();
  8. }

this is the notify icon class
  1. class Notify_Icon
  2. {
  3. public Notify_Icon () {
  4. NotifyIcon icn = new NotifyIcon();
  5. icn.Visible = true;
  6. Stream strm = Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsFormsApplication1.Globe.ico");
  7. icn.Icon = new Icon(strm);
  8. ContextMenu menu = new ContextMenu();
  9. menu.MenuItems.Add(0,
  10. new MenuItem("Start", new System.EventHandler(srvr.Start)));
  11. menu.MenuItems.Add(1,
  12. new MenuItem("Stop", new System.EventHandler(srvr.Stop)));
  13. icn.ContextMenu = menu;
  14. }
Can someone please help me with this.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 33
Reputation: miculnegru is an unknown quantity at this point 
Solved Threads: 5
miculnegru miculnegru is offline Offline
Light Poster

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

 
0
  #2
Aug 27th, 2008
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)
{
....
}
}
Last edited by miculnegru; Aug 27th, 2008 at 5:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 56
Reputation: curt22 is an unknown quantity at this point 
Solved Threads: 0
curt22 curt22 is offline Offline
Junior Poster in Training

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

 
0
  #3
Aug 27th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

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

 
0
  #4
Aug 27th, 2008
Separate out the classes, and create a singleton

  1. public class Server
  2. {
  3. private bool _stopped;
  4. public bool Stopped
  5. {
  6. get
  7. {
  8. return _stopped;
  9. }
  10. set
  11. {
  12. _stopped = value;
  13. }
  14. }
  15. private static Server _instance = new Server();
  16.  
  17. public static Server instance
  18. {
  19. return _instance;
  20. }
  21. }
  22.  
  23.  
  24.  
  25. //from your notify icon class
  26. Server.instance.Stopped = true;
Last edited by dickersonka; Aug 27th, 2008 at 6:46 pm.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 56
Reputation: curt22 is an unknown quantity at this point 
Solved Threads: 0
curt22 curt22 is offline Offline
Junior Poster in Training

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

 
0
  #5
Aug 27th, 2008
Ok now the error says
  1. The name 'icon' does not exist in the current context
I changed the code to this.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.IO;
  7. using System.Reflection;
  8. using System.Drawing;
  9.  
  10. namespace Serveri
  11. {
  12. class Notify_Icon
  13. {
  14. public NotifyIcon icn;
  15. public Notify_Icon()
  16. {
  17. this.icn = new NotifyIcon();
  18. this.icn.Visible = true;
  19. Stream strm = Assembly.GetExecutingAssembly().GetManifestResourceStream("Serveri.Globe.ico");
  20. this.icn.Icon = new Icon(strm);
  21. ContextMenu menu = new ContextMenu();
  22. menu.MenuItems.Add(0,
  23. new MenuItem("Start", new System.EventHandler(Server.instance.Start)));
  24. menu.MenuItems.Add(1,
  25. new MenuItem("Stop", new System.EventHandler(Server.instance.Stop)));
  26. this.icn.ContextMenu = menu;
  27. }
  28. }
  29. }

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Serveri
  7. {
  8. public class Server
  9. {
  10. public void Start(object sender, EventArgs e) { }
  11. public void Start() {
  12. icon.icn.text = "Running";
  13. }
  14. public void Stop(object sender, EventArgs e) { }
  15. public void Stop() {
  16. icon.icn.text = "Stopped";
  17. }
  18. private static Server _instance = new Server();
  19.  
  20. public static Server instance
  21. {
  22. get
  23. {
  24. return _instance;
  25. }
  26. }
  27. }
  28. }


  1.  
  2. Server srvr = new Server();
  3. Notify_Icon icon = new Notify_Icon();
  4. srvr.Start();
  5. Application.Run();

I need some way for the two classes to be able to access each others methods.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

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

 
0
  #6
Aug 27th, 2008
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

  1.  
  2. private Server()
  3. {
  4. }
  5. private string _status;
  6. public string Status
  7. {
  8. get
  9. {
  10. return _status;
  11. }
  12. }
  13.  
  14. public void Stop()
  15. {
  16. _status = "Stopped";
  17. }
  18.  
  19.  
  20. //in your last piece of code
  21. icon.text = Server.instance.Status;
  22. //might need to be this
  23. icon.icn.text = Server.instance.Status;
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

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

 
0
  #7
Aug 27th, 2008
Then why not pass srvr to the Notify_icon ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

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

 
0
  #8
Aug 27th, 2008
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.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 56
Reputation: curt22 is an unknown quantity at this point 
Solved Threads: 0
curt22 curt22 is offline Offline
Junior Poster in Training

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

 
0
  #9
Aug 27th, 2008
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

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

 
0
  #10
Aug 27th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC