472,353 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 11243
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: seesaw | last post by:
class A { public: static A* newA() { return new A; } .... }; In the code, two things not very clear and natural to me: 1. the method newA()...
1
by: Action | last post by:
Let's say class parent class B : parent class C : parent ....etc. (may add later......so I don't know how many classes will there...) I wanna...
3
by: Gergely Varadi | last post by:
public class A : IComparable { int IComparable.CompareTo(object obj) { return 1; } } /// <summary> /// <see...
4
by: Piotr Perak | last post by:
Can someone explain the "object instance" term that is used i many of C# books? I have C++ background. In C++ instance and object meant the same. I...
15
by: Cruella DeVille | last post by:
I'm trying to implement a bookmark-url program, which accepts user input and puts the strings in a dictionary. Somehow I'm not able to iterate...
2
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is...
14
by: =?GB2312?B?zPC5zw==?= | last post by:
Howdy, I wonder why below does not work. a = object() a.b = 1 # dynamic bind attribute failed... To make it correct, we have to create...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.