469,306 Members | 1,916 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

what is an "explicit instance of a class"

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
Nov 21 '05 #1
8 10932
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

"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

Nov 21 '05 #2
" 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

"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

Nov 21 '05 #3
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

"Chris, Master of All Things Insignifican" wrote:
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

"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


Nov 21 '05 #4
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
"smith" wrote:
" 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

"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


Nov 21 '05 #5
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

"kevin" <ke***@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
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

"Chris, Master of All Things Insignifican" wrote:
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

"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


Nov 21 '05 #6
Thanks Chris,
It worked.
have a great day
kevin

"Chris, Master of All Things Insignifican" wrote:
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

"kevin" <ke***@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
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

"Chris, Master of All Things Insignifican" wrote:
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

"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


Nov 21 '05 #7
"kevin" <ke***@discussions.microsoft.com> wrote in message
news:E4**********************************@microsof t.com...
.. . .
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.
Nov 21 '05 #8
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


Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by seesaw | last post: by
4 posts views Thread by Piotr Perak | last post: by
15 posts views Thread by Cruella DeVille | last post: by
2 posts views Thread by Daniel Lipovetsky | last post: by
14 posts views Thread by =?GB2312?B?zPC5zw==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.