Two general questions

Hi,

I have two general questions, i.e. about the following code (VS 2005):

[code]
Imports System
Imports System.Text
Imports System.Collections.Generic

Public Class Example
Public Shared Sub Main()
Console.WriteLine("This is a message.")
End Sub
End Class

Module Module1
Sub Main()
Dim C As Example = New Example
C.Main()
End Sub
End Module
[/code]
Visual Studio underlines "C.Main()" saying: "Access of shared member through an instance; qualifying expression will not be evaluated".
VS suggests: Replace 'C' with 'Example'.

So my first question is: Why is VS suggesting this?

My second question is: If I change "Dim C As Example = New Example" into "Dim C As Example", C.Main() still works. Why is that? I thought I could only use instances of classes?!

Thanks in advance for any help!

Randy.

Comments

  • : Hi,
    :
    : I have two general questions, i.e. about the following code (VS
    : 2005):
    :
    : [code]:
    : Imports System
    : Imports System.Text
    : Imports System.Collections.Generic
    :
    : Public Class Example
    : Public Shared Sub Main()
    : Console.WriteLine("This is a message.")
    : End Sub
    : End Class
    :
    : Module Module1
    : Sub Main()
    : Dim C As Example = New Example
    : C.Main()
    : End Sub
    : End Module
    : [/code]:
    : Visual Studio underlines "C.Main()" saying: "Access of shared member
    : through an instance; qualifying expression will not be evaluated".
    : VS suggests: Replace 'C' with 'Example'.
    :
    : So my first question is: Why is VS suggesting this?
    :
    : My second question is: If I change "Dim C As Example = New Example"
    : into "Dim C As Example", C.Main() still works. Why is that? I
    : thought I could only use instances of classes?!
    :
    : Thanks in advance for any help!
    :
    : Randy.
    :

    Both are related to the same fact: the function is Shared between instances, and thus is not bound to any instance.
    VS suggests you call it as such:
    [code]
    Example.Main()
    [/code]
    This is the proper way to call it. It's prefered over calling the Shared function through an instance. In both cases (calling like above, or calling through an instance) the resulting code will be Example.Main(). So that's why it also works without the New, because the instance doesn't apply.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion