I know I'm late to the party here and you have now solved your problem, but
I think it's worth taking a little time to understand a bit more about
"shared" methods and their place in OO in general. This might help you with
problems you run into in the future.
As you already know, your projects consists of classes. When you add a form
to a project you are really creating a class; dropping controls onto the
form, setting its properties etc all add code to that class.
At runtime, your code usually turns classes into objects, a process known as
instantiation. The methods (the subs and functions that you wrote into the
classes) then become part of the "instance" interface to the object.
Shared methods, functions and properties on the other hand are not instance
based - they are class based. So a method in a class like this
public shared sub MyMethod()
End Sub
Is actually a "class" method called MyMethod. Where normally you would have
to instantiate an object from the class to call "MyMethod", with Shared
methods you dont. You can just say "MyClass.MyMethod", instead of "
Dim MyObject As New MyClass()
MyObject.MyMethod() ' This is an instance method call
Now, the golden rule, and the problem that you ran into, is that instance
members (methods, properties etc called on an object) can see and use class
members (shared methods, properties, variables etc). However, Shared members
can't see instance members; they exist as a part of the class, not as part
of the objectcs created from the class.
Hope that makes a little more sense.
--
Pete Wright
Author of ADO.NET Novice to Pro for Apress
www.petewright.org
"kevin" <ke***@discussions.microsoft.com> wrote in message
news:E4**********************************@microsof t.com...
I have a form and in the form I have a sub that uses a class I instantiate
using
visual basic code: Public oCP As New Rs232 'instantiate the comm
port
I need to share this sub with another form so to declare the sub I use
visual basic code: Public Shared Function IsPortAvailable(ByVal ComPort
As Integer) As Boolean
This gives me the following error
"Cannot refer to an instance member of a class from within a shared method
or shared member initializer without an explicit instance of the class."
To resolve the error, MSDN says
"Provide an explicit instance of the class whose member you wish to
reference."
So how do I do that, and what does it mean to do that?
thanks
kevin