0

I have two classes. One is this:

namespace DataStructures_Algorithms
{
    public class BackendService
    {

        public class LocationServiceQueue
        {
            private static string deviceId;
            private static POI currentPOI;
            private static DateTime timeStamp;
            private static ConcurrentQueue<DeviceMessage> concurrentQueue = new ConcurrentQueue<DeviceMessage>();

            public static void Enqueue(DeviceMessage deviceMessage)
            {
                try
                {
                    concurrentQueue.Enqueue(deviceMessage);

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception Occured in LocationServiceQueue Class " + ex.ToString());
                }
            }

        }
}

and now i want to call enqueue method of LocationServiceQueue class.

                backendService.LocationServiceQueue.Enqueue(new DeviceMessage
                {
                    DeviceId = deviceID,
                    CurrentPOI = currentPOI,
                    Timestamp = DateTime.Now
                });

it gives an error that you cannot access. How can I access?

vitr
  • 6,766
  • 8
  • 30
  • 50
  • Could you show more on how you're calling it? For example, did you instantiate the class properly first? – Keyur PATEL Sep 12 '16 at 01:33
  • i first instantiate the backedService object. `BackendService backendService = new BackendService();`. but now i am stuck how to call that static method of inner class of that backendService object – Zeeshan Afzal Satti Sep 12 '16 at 01:35
  • If the answer below doesn't work for you (it does seem correct though), I saw one of the answers online, they suggest passing your outer class as a parameter to the inner one. Maybe this can help: [Can i access outer class objects in inner class](http://stackoverflow.com/questions/2957900/can-i-access-outer-class-objects-in-inner-class) – Keyur PATEL Sep 12 '16 at 01:44

2 Answers2

1

Enqueue is a class method so you have to access method using class name (not using instance variable).

Since inner LocationServiceQueue class a public class, you could do this.

BackendService.LocationServiceQueue.Enqueue(new DeviceMessage
            {
                DeviceId = deviceID,
                CurrentPOI = currentPOI,
                Timestamp = DateTime.Now
            });
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
1

I posted a link in the comments, but an answer would be easier to help you with. Modify your code to this:

namespace DataStructures_Algorithms
{
    public class BackendService
    {
        public class LocationServiceQueue
        {
            private BackendService _backendservice;

            private static string deviceId;
            private static POI currentPOI;
            private static DateTime timeStamp;
            private static ConcurrentQueue<DeviceMessage> concurrentQueue = new ConcurrentQueue<DeviceMessage>();

            public LocationServiceQueue(BackendService outer)
            {
                _backendservice = outer;
            }


            public static void Enqueue(DeviceMessage deviceMessage)
            {
                try
                {
                    concurrentQueue.Enqueue(deviceMessage);

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception Occured in LocationServiceQueue Class " + ex.ToString());
                }
            }

        }
}

And later:

BackendService.LocationServiceQueue l = new BackendService.LocationServiceQueue(new BackendService());

Although this only works if you don't really care about your BackendService class, but only about the LocationServiceQueue methods.

Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41