Link to home
Start Free TrialLog in
Avatar of allanmark
allanmark

asked on

Creating a Singleton class

Greetings all

I am trying to create a SIngleton class (see attached) and get complaints: "Error      3      A get or set accessor expected      ".

What have I missed?


in advance, thanks!!!

    allanmark
Avatar of Gautham Janardhan
Gautham Janardhan

attatchment ?
Avatar of allanmark

ASKER

Sorry .....

BTW, once the cllass is correctly defined, how do I call / insntiate in my form?
using System;
using System.Collections.Generic;
using System.Text;
 
namespace BranchPingService
{
    public class PingService: IPingService
    {
 
    #region Singleton
 
    private static volatile PingService sinPingService;
 
    private PingService()
    {
 
    }
 
    public static PingService SinPingService
    {
        get
        {
            if (sinPingService == null)
            {
                sinPingService = new PingService();
            }
        }
        return sinPingService;   // HERE IS WHERE THE ERROR IS HAPPENING?
    }
 
    #endregion
 
    public bool PingBranch()
    {
        List<BranchDetail> branches;
 
        try
        {
            BranchPingLogic logic = new BranchPingLogic();
            //List<BranchDetail> branches = logic.GetOpenBranches();
            branches = logic.GetOpenBranches();
            
            branches.ForEach(delegate(BranchDetail branch)
                {
                    PingReply pingRep = logic.PingBranch(branch.IpAddress);
 
                    if (pingRep != null)
                    {
                        bool isBranchCurrentlyUp = pingRep.Status == IPStatus.Success;
                        if ((isBranchCurrentlyUp && !branch.IsUp) ||
                            (!isBranchCurrentlyUp && branch.IsUp))
                        {
                            logic.WriteBranchHistory(branch.ID, isBranchCurrentlyUp);
                            if (!isBranchCurrentlyUp)
                            {
                                logic.SendBranchDownNotifications(branch);
                            }
                        }
                    }
                });
            return ("Ping completed at: " + DateTime.Now.TimeOfDay.ToString());
        }
        catch (Exception ex)
        {
            IRPLogger logger = RPLoggerFactory.CreateLogger();
            logger.Log(ex, "Could not ping the branches");
            return "Ping failed!";
        }
    }
 
}

Open in new window

SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
'PingService.SinPingService' to acess it
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fantatstic!! ThankYou everyone!!!

Can I be really cheeky and ask for a comment on this code, forwarded to me by a colleague - I'm curious as to what the locking is about

private static object _syncRoot = new object();

  get
        {
           // Check that the instance is null
           if (_instance == null)
           {    
                // Lock the object
                lock (_syncRoot)
                {
                    // Check to make sure its null
                    if (_instance == null)
                    {
                        _instance = new PublicationDataAccess();
                    }
                }
           }

           // Return the non-null instance of Singleton
           return _instance;
       }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Some syntax error in my code
public class PingService: IPingService
{
    ...
    private static volatile PingService sinPingService = new PingService();
 
    private PingService()
    {
 
    }
 
    public static PingService SinPingService
    {
        get
        {
            return sinPingService;
        }
    }
    ...
}

Open in new window

Thankyou both!!

philipjonathan:-  thanks for the simplified code -- unfortrunately taht want me to do the lockign thingy!

I am happy that my questions has been fully (more than so, in fact) > I now have another issue which I fully appreciate is a separate question. Howeve, for continuity, I'd like to keep this together and increase the points. If you'd prefer a separate question, I will do such ... your help has been great and I'd liek to keep the flow going.

I have now added teh following code to my Console/ Hosting application (the Pingservice) forms part of a WCF service)  - see attachgement and tougth all was well.

However, when I compile dteh solution I get this

ON THE PINGSERVICE:  "Error      3      Program 'D:\Allan\\BranchPing\BranchPingService\obj\Release\BranchPingService.exe' does not contain a static 'Main' method suitable for an entry point      BranchPingService"  

ON THE HOST:
Error      4      Metadata file 'D:\Allan\\BranchPing\BranchPingService\bin\Release\BranchPingService.exe' could not be found      BranchPingHost

Thoughts?

HOSTING CODE
 ServiceHost<BranchPingService.PingService> host = ServiceHost<BranchPingService.PingService>();
            if (host != null)
            {
                host.Open();
 
                Console.WriteLine("PingService is listening. Press enter to exit...");
                Console.ReadLine();
 
                host.Close();
            }
            else
            {
                Console.WriteLine("Unable to instantiate service. Press enter to exit...");
                Console.ReadLine();
            }
 
 
PINGSERVICE:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
using System.Configuration;
using RPLogger;
 
namespace BranchPingService
{
    public sealed class PingService : IPingService
    {
 
        #region Singleton
 
        private static volatile PingService _instance;
 
        // Static synchronization root object, for locking
        private static object _syncRoot = new object();
 
 
        private PingService()
        {
 
        }
 
        public static PingService Instance
        {
            get
            {
                // Check that the instance is null
                if (_instance == null)
                {
                    // Lock the object
                    lock (_syncRoot)
                    {
                        // Check to make sure its null
                        if (_instance == null)
                        {
                            _instance = new PingService();
                        }
                    }   
                }
 
                // Return the non-null instance of Singleton
                return _instance;
            }
        }
 
        #endregion
 
        public bool PingBranch()
        {
            List<BranchDetail> branches;
 
            try
            {
                BranchPingLogic logic = new BranchPingLogic();
                //List<BranchDetail> branches = logic.GetOpenBranches();
                branches = logic.GetOpenBranches();
 
                branches.ForEach(delegate(BranchDetail branch)
                    {
                        PingReply pingRep = logic.PingBranch(branch.IpAddress);
 
                        if (pingRep != null)
                        {
                            bool isBranchCurrentlyUp = pingRep.Status == IPStatus.Success;
                            if ((isBranchCurrentlyUp && !branch.IsUp) ||
                                (!isBranchCurrentlyUp && branch.IsUp))
                            {
                                logic.WriteBranchHistory(branch.ID, isBranchCurrentlyUp);
                                if (!isBranchCurrentlyUp)
                                {
                                    logic.SendBranchDownNotifications(branch);
                                }
                            }
                        }
                    });
                return true;
            }
            catch (Exception ex)
            {
                IRPLogger logger = RPLoggerFactory.CreateLogger();
                logger.Log(ex, "Could not ping the branches");
                return false;
            }
        }
 
    }   // public sealed class PingService : IPingService
}

Open in new window

Well, this time the question is not related to the main question. And since you are a premium service member, it will not harm you to open another question. And for the benefit of clarity to others who would read this thread in the future, I'd suggest you do so. It's up to other experts if they still want to reply through this thread though
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did that. Got:

Error      3      'ServiceModelEx.ServiceHost<BranchPingService.PingService>' is a 'type' but is used like a 'variable'      D:\Allan\BranchPing\BranchPingService\PingService.cs      94      63      BranchPingService
ServiceHost<PingService> host = ServiceHost<PingService>();
 
and singleton patter u shud use the process class which actually does the process.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many thanks for the excellent support!!