-2

I haven't used in years and when I did it was . I'm trying to set up a simple proof of concept in C# and I'm really struggling. I've created a new forms application (I want to be able to control the feed with buttons, start/stop/etc). I then open program.cs which gives me the following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace feedtest
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }

        }
    }

I've installed using package manager and I've added the using reference, but that's where things go sour. The first step in the documentation says to "initialize the API" using the following line:

Pubnub pubnub = new Pubnub("demo", "demo");

I finally figured out I put that in Static Void Main() but now it's like I'm going down the rabbit hole. After modifying a few things from the documentation and ignoring others, I'm at the following point:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using PubNubMessaging.Core;

namespace feedtest
{
    static class Program
    {
        private static string result;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            Pubnub pubnub = new Pubnub("demo", "demo");

            pubnub.Subscribe<string>(
               "my_channel",
                DisplaySubscribeReturnMessage,
                DisplaySubscribeConnectStatusMessage,
                DisplayErrorMessage
            );


        }

        static void DisplayErrorMessage(PubnubClientError obj)
        {
            throw new NotImplementedException();
        }

        static void DisplaySubscribeConnectStatusMessage(string obj)
        {
            throw new NotImplementedException();
        }

        static void DisplaySubscribeReturnMessage(string obj)
        {
            Console.WriteLine("SUBSCRIBE REGULAR CALLBACK:");
            Console.WriteLine(result);
            if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
            {
                List<object> deserializedMessage = pubnub.JsonPluggableLibrary.DeserializeToListOfObject(result);
                if (deserializedMessage != null && deserializedMessage.Count > 0)
                {
                    object subscribedObject = (object)deserializedMessage[0];
                    if (subscribedObject != null)
                    {
                        //IF CUSTOM OBJECT IS EXCEPTED, YOU CAN CAST THIS OBJECT TO YOUR CUSTOM CLASS TYPE
                        string resultActualMessage = pubnub.JsonPluggableLibrary.SerializeToJsonString(subscribedObject);
                    }
                }
            }
        }
    }

}

The current error is

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

enter image description here

How can I fix this error? Should I? Am I even close? Did I pick the wrong starting point? Does anyone know of any good, simple, pubnub tutorials?

sm_
  • 2,572
  • 2
  • 17
  • 34
  • Sorry, but that's really not a great title question. – Brian Rasmussen Aug 25 '16 at 22:04
  • 1
    The problem here is not a pubnub problem, but c#. You might get some mileage out of learning c# fundamentals before also learning pubnub at the same time. – recursive Aug 25 '16 at 22:10
  • 1
    Please [edit] your question title to something that describes the actual problem you're experiencing or question you're asking. You've been here long enough to know that the title as written is totally uninformative - what possible value would that have to a future reader here who finds it in a search result while trying to find a problem solution? And you're also experienced enough here to know that questions asking us to find tutorials are off-topic as well. (I'd suggest you Google a C# tutorial first before worrying about Pubnub.) – Ken White Aug 25 '16 at 22:15
  • I completely agree and have voted to close (I would delete but there's already an answer). I was just frustrated because following docs like this is how I typically learn. I start with a basic sample and modify it from there. In this case the basic sample just doesn't work as is and I guess that's what I was hoping to get help with. –  Aug 26 '16 at 13:16

1 Answers1

2

pubnub is out of scope where you're trying to use it. Using it as a local variable, as you are, you need to declare it in the same method where you're using it.

If you want to use the same pubnub in both methods, then you need to put it somewhere else or pass it. Probably declare it as a static field in your program.

private static Pubnub pubnub = new Pubnub("demo", "demo");
recursive
  • 83,943
  • 34
  • 151
  • 241