We help IT Professionals succeed at work.

Creating a Singleton class

allanmark
allanmark asked
on
411 Views
Last Modified: 2013-12-17
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
Comment
Watch Question

CERTIFIED EXPERT

Commented:
attatchment ?

Author

Commented:
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

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
'PingService.SinPingService' to acess it
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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;
       }
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
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

Author

Commented:
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
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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
CERTIFIED EXPERT

Commented:
ServiceHost<PingService> host = ServiceHost<PingService>();
 
and singleton patter u shud use the process class which actually does the process.
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Many thanks for the excellent support!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.