1

So I'm trying to be smart about this but it looks like this idea is quite dumb on account of I'm getting a stack overflow exception right off the bat.

Ideally, I would like for CheckInternetStatus to run whenever I use the IsConnected flag.

How can I accomplish this correctly? Or is this just not a good idea at all?

public static class Internet
{
    public static bool IsConnected
    {
        get
        {
            CheckInternetStatus();
            return IsConnected;
        }
        set
        {
            IsConnected = value;
        }
    }
}

Additional Information: CheckInternetStatus updates IsConnected

6 Answers 6

5

There are two things wrong with your code:

1) No backing variable, you are recursively calling property IsConnected:

private bool _isConnected;

public static bool IsConnected
{
    get
    {
        _isConnected = CheckInternetStatus();
        return _isConnected;
    }
    set
    {
        _isConnected = value;
    }
}

2) You should not do 'lots of work' in a property. Use a method instead. This better conveys intent to someone reading your code

Actually 3 things:

3) If you stick with using a property (I wouldn't), the setter should probably be private. You don't want connected to be set if you are not actually connected.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answer. I voted for yours as it is the most concise and in-depth.
4

think you want

private static bool _isConnected;
public static bool IsConnected
    {
        get
        {
            CheckInternetStatus();
            return _isConnected;
        }
        set
        {
            _isConnected= value;
        }
    }

because you are recursively calling into your property

Comments

1

This is a case where a backing field can be extremely useful.

private static bool _isConnected;

public static bool IsConnected {
    get {
        CheckInternetStatus();
        return _isConnected;
    }
    set {
        _isConnected= value;
    }
}

Alternatives

Additional Information: CheckInternetStatus updates IsConnected

CheckInternetStatus should manipulate the private field. Futhermore, the set should be private (or possibly not exist). But IsConnected seems to have a somewhat unexpected side-effect (the act of connecting). Instead, what about:

public static bool IsConnected {
    get;
    private set;
}

public static void Connect(){
    if( IsConnected ){
        // exit, or fail if this is considered an exceptional scenario
    }

    // do work
    IsConnected = true;
}

// call
if( !Foo.IsConnected ){
    Foo.Connect();
}

This seems cleaner and more obvious. Properties (as a general rule) shouldn't perform large amounts of work.

1 Comment

Just remember that CheckInternetStatus() should be setting _isConnected rather than IsConnected, too.
0

You're infinitely recursing.

return IsConnected;

calls your getter, which calls your getter again and again, infinitely, until you crash.

Comments

0

You are doing this wrong. You need some instance variable. You are calling yourself. Correction looks like:

public static class Internet
{
private static bool _isConnected;

public static bool IsConnected
{
    get
    {
        CheckInternetStatus();
        return _isConnected;
    }
    set
    {
        _isConnected = value;
    }
}

}

Comments

0

I think you can also use like this,

       private static bool _isConnected;

        public static bool isConnected
        {
            get { CheckInternetStatus(); return _isConnected; }
            set { isConnected = value; }
        }

3 Comments

How is this any different than the half-dozen answers already stating the same thing?
oops im sorry, i forgot to refresh the page, so I didn't notice that there are already answers, sorry ,
No problem. You should see a bar across the page saying "new answers to this question", which you can click to see new answers and save answering something that has already been answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.