943,769 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 17273
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 27th, 2008
0

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

Expand Post »
Hi everyone, I'm trying to write a server application, but I'm having a bit of trouble. I keep getting this error
C# Syntax (Toggle Plain Text)
  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
C# Syntax (Toggle Plain Text)
  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
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
curt22 is offline Offline
56 posts
since Sep 2007
Aug 27th, 2008
0

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

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.
Reputation Points: 15
Solved Threads: 5
Light Poster
miculnegru is offline Offline
33 posts
since Nov 2005
Aug 27th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
curt22 is offline Offline
56 posts
since Sep 2007
Aug 27th, 2008
0

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

Separate out the classes, and create a singleton

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Aug 27th, 2008
0

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

Ok now the error says
C# Syntax (Toggle Plain Text)
  1. The name 'icon' does not exist in the current context
I changed the code to this.

C# Syntax (Toggle Plain Text)
  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. }

C# Syntax (Toggle Plain Text)
  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. }


C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
curt22 is offline Offline
56 posts
since Sep 2007
Aug 27th, 2008
0

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

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

C# Syntax (Toggle Plain Text)
  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;
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Aug 27th, 2008
0

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

Then why not pass srvr to the Notify_icon ?
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Aug 27th, 2008
0

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

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.
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Aug 27th, 2008
0

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

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
curt22 is offline Offline
56 posts
since Sep 2007
Aug 27th, 2008
0

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

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
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How to know the number of columns in the table
Next Thread in C# Forum Timeline: how to insert data...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC