| | |
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:
Solved Threads: 0
Hi everyone, I'm trying to write a server application, but I'm having a bit of trouble. I keep getting this error
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
this is the notify icon class
Can someone please help me with this.
C# Syntax (Toggle Plain Text)
The name 'srvr' does not exist in the current context
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)
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
C# Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
Separate out the classes, and create a singleton
C# Syntax (Toggle Plain Text)
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;
Last edited by dickersonka; Aug 27th, 2008 at 6:46 pm.
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Sep 2007
Posts: 56
Reputation:
Solved Threads: 0
Ok now the error says
I changed the code to this.
I need some way for the two classes to be able to access each others methods.
C# Syntax (Toggle Plain Text)
The name 'icon' does not exist in the current context
C# Syntax (Toggle Plain Text)
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; } } }
C# Syntax (Toggle Plain Text)
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; } } } }
C# Syntax (Toggle Plain Text)
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.
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
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
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)
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;
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
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
www.houseshark.net
•
•
Join Date: Sep 2007
Posts: 56
Reputation:
Solved Threads: 0
C# Syntax (Toggle Plain Text)
Then why not pass srvr to the Notify_icon ?
![]() |
Other Threads in the C# Forum
- Previous Thread: How to know the number of columns in the table
- Next Thread: how to insert data...
| Thread Tools | Search this Thread |
.net 2007 access algorithm array barchart bitmap box broadcast c# camera check checkbox client combobox control conversion cs4 csharp custom customactions database datagrid datagridview dataset date datetime degrees development draganddrop drawing encryption enum event eventcloseformc# excel file form format forms function gdi+ handler httpwebrequest image index input install java keypress label list listbox listener listview load mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting resolved. richtextbox search security server sleep socket sql statistics stream string table text textbox thread time timer update usercontrol validation view visual visualstudio webbrowser windows winforms wordautomation wpf xml






