what is an "explicit instance of a class"

G

Guest

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
 
C

Chris, Master of All Things Insignificant

I think this is what is going on....

You can't do this. A shared function can not access variables of the class
because you can use the function without creating an instance of the class.
Public Class BadClass
Dim I as Integer
Public Shared Sub BadShare()
I += 1 '<-- Bad
End Sub
End Class

I don't think the "shared" keyword is the solution to your problem. What do
you mean when you say you need to "share this sub with another form". Can
you give a simple example of what you would like to do. I think you mean
Form2 calls method from Form1. What is the relationship between the two
forms now? Does one create and show the other?

Chris
 
S

smith

" I need to share this sub with another form so to declare the sub I use"
If I read you correctly you're using the Shared keyword so that Form1's sub
can be used from another Form? If that's what you meant then using the
Shared keyword isn't what you want, just declare Form1's available sub as
Public or Friend and cut the "Shared".

"Shared" lets you use a class without creating an instance, such as having a
class named "MyWidget" that has a method "AddNumbers(firstValue as int32,
secondValue as int32) as Integer" this method doesn't neccessarily require
you to create a full MyWdiget Object ( "Dim o as new Widget") just to use
this function (" o.AddNumbers") and you can instead declare te method as
Shared so that it can be used without an object being explicitly created by
using the syntax "iResult = MyWidget.AddNumbers(1,2)"

Forgive me if I misread your situation.

Robert Smith
Kirkland, WA
www.smithvoice.com
 
G

Guest

Thanks for the response

What you say makes good sense. The class in question controls a serial
comm port, and the sub (it is actually a function) Form1.IsPortAvailable(By
Val CommPortNumber as integer) as Boolean uses the class to determine if a
specific commport is available. Form2 is an options form that needs to
generate a list of all available commports so I would like to simply use
Form1.IsPortAvailable rather than have the same functionin multiple places.
Form2 is always created and shown by Form1, and Form1 will alway exist when
Form2 exists.
kevin
 
G

Guest

Thanks Robert,
First let me state, the sub in form1 is a function, but that shouldn't
matter should it? Anyway....
Yes you read it correctly, I simply want to use a function in Form1 from
Form2. When I use just the Public keyword as you suggest however,, I get an
error saying " Reference to a non-shared member requires an object
reference." on the line in Form2 that does the calling
kevin
 
C

Chris, Master of All Things Insignificant

Ok, this should be pretty easy then.

First, Take off the Shared and make it just public.

Change Form2 to look like this

Public Class Form2
Dim RefForm1 as Form1
Sub New(ByRef Value as Form1)
MyBase.New()
RefForm1 = Value
End Sub

Sub UseForm1()
RefFrom1.YourPublicFunctionInForm1()
End Sub
End Sub

When you create Form2 in Form1 do this.

Dim F2 as New Form2(Me)

Hope it helps
Chris
 
G

Guest

Thanks Chris,
It worked.
have a great day
kevin

Chris said:
Ok, this should be pretty easy then.

First, Take off the Shared and make it just public.

Change Form2 to look like this

Public Class Form2
Dim RefForm1 as Form1
Sub New(ByRef Value as Form1)
MyBase.New()
RefForm1 = Value
End Sub

Sub UseForm1()
RefFrom1.YourPublicFunctionInForm1()
End Sub
End Sub

When you create Form2 in Form1 do this.

Dim F2 as New Form2(Me)

Hope it helps
Chris
 
P

Phill. W

.. . .
I need to share this sub with another form

"share" <> "Shared"

In VB.Net, a Shared method is one that can be called without an
instance of the class that contains it, e.g. [String].Join().

A method (on a form) that can be used elsewhere in the project
usually needs to be made Public, as in

Public Function IsPortAvailable( _
ByVal ComPort As Integer _
) As Boolean

HTH,
Phill W.
 
P

Pete Wright

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top