473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 11403
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() is defined as static. 2. newA as a member method...
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 to let the user type in the class name and...
3
by: Gergely Varadi | last post by:
public class A : IComparable { int IComparable.CompareTo(object obj) { return 1; } } /// <summary> /// <see cref="A.System.IComparable.CompareTo"/>
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 could say that I have object of some class or...
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 myDictionary of type Dict{} When I write print...
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 called when a new instance is created, as below. ...
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 a new class: class MyClass(object): pass a =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.