|
-
May 11th, 2013, 11:12 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Don't quite understand this warning; Access of shared member....
Hi,
I have a class called InternetDetector that is checking to see if internet is available, and if available, add a Label to a form.
I declare the class like this:
vb.net Code:
Dim id As InternetDetector = New InternetDetector(2500, My.Settings.CheckInternetHostName, Me, _uiLanguage)
id.Start()
That will give me a warning, saying that Access of shared member, constant member, enum member or nested type through an instance; qualifing expression will not be evaluated, and it suggest me to change my code to:
vb.net Code:
Dim id As InternetDetector = New InternetDetector(2500, My.Settings.CheckInternetHostName, Me, _uiLanguage)
InternetDetector.Start()
Both of them work, but how is it that InternetDetector.Start knows the parameters of id? 
InternetDetector class:
vb.net Code:
Option Strict On
Option Explicit On
Imports System.Drawing
Imports System.Windows.Forms
Public Class InternetDetector
Private Shared _host As String = Nothing
Private Shared _interval As Integer = 1000
Private Shared _form As Object = Nothing
Private Shared _uiLanguage As String = Nothing
Public Sub New(ByVal interval As Integer, ByVal host As String, ByVal sender As Object, ByVal uiLanguage As String)
_host = host
_interval = interval
_form = sender
_uiLanguage = uiLanguage
End Sub
Public Shared WithEvents checkTimer As New System.Timers.Timer(_interval)
Public Shared Sub Start()
checkTimer.Start()
End Sub
Public Shared Sub [Stop]()
checkTimer.Stop()
End Sub
Public Shared Sub m_Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles checkTimer.Elapsed
InternetDetector_Result(IsInternetIsAvailable(_host))
End Sub
Private Delegate Sub InternetDetector_ResultCallback(ByVal result As Boolean)
Private Shared Sub InternetDetector_Result(ByVal result As Boolean)
Dim f As Form = DirectCast(_form, Form)
If f.InvokeRequired Then
f.Invoke(New InternetDetector_ResultCallback(AddressOf InternetDetector_Result), result)
Else
'Some crazy code to add a label to a form
End If
End Sub
Private Shared Function IsInternetIsAvailable(ByVal host As String) As Boolean
Try
Dim a As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(host)
Return True
Catch ex As System.Net.Sockets.SocketException
Return False
End Try
End Function
End Class
-
May 11th, 2013, 11:17 AM
#2
Re: Don't quite understand this warning; Access of shared member....
That's because everything in your class is shared which in fact makes it a module rather than a class. If you would create a second object you would also change the first since nothing in your class belongs to the object instance.
-
May 11th, 2013, 11:26 AM
#3
Thread Starter
Fanatic Member
Re: Don't quite understand this warning; Access of shared member....
Ahh, thank you Joachim. I will go over the code later tonight, need to go out now and bbq before it starts to rain
-
May 11th, 2013, 11:32 AM
#4
Re: [RESOLVED] Don't quite understand this warning; Access of shared member....
Why do you think that it is necessary to check if the internet is available? A better question is what is internet availability? It looks like you have defined it to mean that a hostname can be resolved, regardless of who provides the response.
-
May 11th, 2013, 12:16 PM
#5
Thread Starter
Fanatic Member
Re: [RESOLVED] Don't quite understand this warning; Access of shared member....
Some of the features in the application is depending on internet, and they will get disabled if the host can not be reached. The label I am adding through that class is just to show the user if internet is available or not.

If this is not what you meant, please let me know
-
May 11th, 2013, 01:10 PM
#6
Re: [RESOLVED] Don't quite understand this warning; Access of shared member....
 Originally Posted by _powerade_
... if internet is available or not.
What do you mean by the above statement? Your code checks to see if a name can be resolved. What if it is resolved by the DNS Resolver running on the local PC?
You are also assuming that nothing happens between the time you check if the internet is available and using it. What if it is not up when you check, and the next instant it comes back up? What if you check and it is up, and the next instant it goes down?
It has been my experience that these kinds of checks do not accomplish anything when examined closely.
-
May 11th, 2013, 01:30 PM
#7
Thread Starter
Fanatic Member
Re: [RESOLVED] Don't quite understand this warning; Access of shared member....
I have very little experience on this topic, so I guess everything you write here is correct. So what do you suggest me to do? Would it be better if I use the IP-address instead, or just simply go for a Ping? From what I have read on MSDN, using the Ping method isn't that reliable either...
I plan to check within a 5 to 10 second interval, and if it gets unavailable between the checks, then I must handle that in code.
But nonetheless, I think it's a nice gimmick to show it in the statusbar
-
May 11th, 2013, 02:21 PM
#8
Re: [RESOLVED] Don't quite understand this warning; Access of shared member....
 Originally Posted by _powerade_
I have very little experience on this topic, so I guess everything you write here is correct. So what do you suggest me to do? Would it be better if I use the IP-address instead, or just simply go for a Ping? From what I have read on MSDN, using the Ping method isn't that reliable either...
I'd suggest not bothering with it at all. Whatever your code is really doing has to be wrapped in a Try-Catch right?
 Originally Posted by _powerade_
...But nonetheless, I think it's a nice gimmick to show it in the statusbar 
Gimmick is correct.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|