I'm trying to develop my application extension and part of it has a variable that I want to tell if the user has an internet connection or not. I've set it up to where it's read only, but I'm trying something new and it threw a StackOverflowException. Hopefully someone can explain why and how to fix it?

[DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int desc, int reserved);

        public bool InternetConnection
        {
            get
            {
                return InternetConnection;
            }

            private set
            {
                InternetConnection = false;
                int desc = 0;

                if (InternetGetConnectedState(out desc, 0))
                {
                    InternetConnection = true;
                }
            }
        }

I have no knowledge of Stack Overflow Exceptions, I don't know what causes them, how to fix them, or anything else of that nature.

The problem is that the get method returns itself. You should change it to return the InternetConnectionState:

[DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int desc, int reserved);
        public bool InternetConnection
        {
            get
            {
                int desc; // By default is initialized to 0.
                return InternetGetConnectedState(out desc, 0);
            }

        }

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.