Results 1 to 8 of 8

Thread: [RESOLVED] Don't quite understand this warning; Access of shared member....

  1. #1

    Thread Starter
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Resolved [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:
    1. Dim id As InternetDetector = New InternetDetector(2500, My.Settings.CheckInternetHostName, Me, _uiLanguage)
    2. 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:
    1. Dim id As InternetDetector = New InternetDetector(2500, My.Settings.CheckInternetHostName, Me, _uiLanguage)
    2. InternetDetector.Start()

    Both of them work, but how is it that InternetDetector.Start knows the parameters of id?

    InternetDetector class:
    vb.net Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Imports System.Drawing
    5. Imports System.Windows.Forms
    6.  
    7. Public Class InternetDetector
    8.     Private Shared _host As String = Nothing
    9.     Private Shared _interval As Integer = 1000
    10.     Private Shared _form As Object = Nothing
    11.     Private Shared _uiLanguage As String = Nothing
    12.  
    13.     Public Sub New(ByVal interval As Integer, ByVal host As String, ByVal sender As Object, ByVal uiLanguage As String)
    14.         _host = host
    15.         _interval = interval
    16.         _form = sender
    17.         _uiLanguage = uiLanguage
    18.     End Sub
    19.  
    20.     Public Shared WithEvents checkTimer As New System.Timers.Timer(_interval)
    21.  
    22.     Public Shared Sub Start()
    23.         checkTimer.Start()
    24.  
    25.     End Sub
    26.  
    27.     Public Shared Sub [Stop]()
    28.         checkTimer.Stop()
    29.  
    30.     End Sub
    31.  
    32.     Public Shared Sub m_Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles checkTimer.Elapsed
    33.  
    34.         InternetDetector_Result(IsInternetIsAvailable(_host))
    35.  
    36.     End Sub
    37.  
    38.     Private Delegate Sub InternetDetector_ResultCallback(ByVal result As Boolean)
    39.     Private Shared Sub InternetDetector_Result(ByVal result As Boolean)
    40.         Dim f As Form = DirectCast(_form, Form)
    41.  
    42.         If f.InvokeRequired Then
    43.             f.Invoke(New InternetDetector_ResultCallback(AddressOf InternetDetector_Result), result)
    44.  
    45.  
    46.         Else
    47.             'Some crazy code to add a label to a form
    48.  
    49.         End If
    50.     End Sub
    51.  
    52.     Private Shared Function IsInternetIsAvailable(ByVal host As String) As Boolean
    53.         Try
    54.             Dim a As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(host)
    55.             Return True
    56.  
    57.         Catch ex As System.Net.Sockets.SocketException
    58.             Return False
    59.  
    60.         End Try
    61.  
    62.     End Function
    63.  
    64. End Class
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  3. #3

    Thread Starter
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    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
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    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.

    Name:  vbforums_statusbar.png
Views: 126
Size:  1.5 KB

    If this is not what you meant, please let me know
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Don't quite understand this warning; Access of shared member....

    Quote Originally Posted by _powerade_ View Post
    ... 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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    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
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Don't quite understand this warning; Access of shared member....

    Quote Originally Posted by _powerade_ View Post
    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?


    Quote Originally Posted by _powerade_ View Post
    ...But nonetheless, I think it's a nice gimmick to show it in the statusbar
    Gimmick is correct.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width