Search code examples
c#publicnetmq

the name '' does not exist in the current context


Hi I can't fix the issue in OnLoginSuccess() pubSocket.SendMoreFrame("TopicA").SendFrame("Hello"); variable due to error the name '' does not exist in the current context.

I know that I need to change variable to public but when I add static public PublisherSocket pubSocket = new PublisherSocket(); at the beggining of the class the code doues not work properly. Also, the problem is that I need to set some options (.Options, .Bind) in Main().

using System;
using System.Threading;
using NetMQ;
using NetMQ.Sockets;

namespace Publisher
{
class Program
{

    static public void OnLoginSuccess()
    {
        pubSocket.SendMoreFrame("TopicA").SendFrame("Hello");
    }

    static void Main(string[] args)
    {


        using (var pubSocket = new PublisherSocket())
        {
            pubSocket.Options.SendHighWatermark = 1000;
            pubSocket.Bind("tcp://localhost:12345");             
        }

        OnLoginSuccess();
    }
}
}

Solution

  • As discussed, either create a private static variable in the class

    private static PublisherSocket  pubSocket;
    

    or pass the socket reference into your OnLoginSuccess method.

     static public void OnLoginSuccess(PublisherSocket socket)
     {
           socket.SendMoreFrame("TopicA").SendFrame("Hello");
     }
    

    As Evan pointed out, your ref would have been disposed of due to the using notation. Remove this and dispose once you've finished with it.

     var pubSocket = new PublisherSocket()
     {
           pubSocket.Options.SendHighWatermark = 1000;
           pubSocket.Bind("tcp://localhost:12345");
     };
    
     OnLoginSuccess(pubSocket);
     pubSocket.Dispose();
    

    You might want to put your call in a try catch finally and put the dispose in the finally to ensure it is cleaned up correctly.

    Hope that helps.