Zeeshan Afzal Satti Zeeshan Afzal Satti - 2 months ago 11
ASP.NET (C#) Question

Call static method of inner class with object of outer class

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 give error that you cannot access. how i can access.. Anybody can help

Answer

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
            });
Comments