473,692 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11440
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***@discussi ons.microsoft.c om> wrote in message
news:E4******** *************** ***********@mic rosoft.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(fir stValue 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.AddNum bers(1,2)"

Forgive me if I misread your situation.

Robert Smith
Kirkland, WA
www.smithvoice.com

"kevin" <ke***@discussi ons.microsoft.c om> wrote in message
news:E4******** *************** ***********@mic rosoft.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.IsPortAva ilable(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.IsPortAva ilable 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***@discussi ons.microsoft.c om> wrote in message
news:E4******** *************** ***********@mic rosoft.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(fir stValue 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.AddNum bers(1,2)"

Forgive me if I misread your situation.

Robert Smith
Kirkland, WA
www.smithvoice.com

"kevin" <ke***@discussi ons.microsoft.c om> wrote in message
news:E4******** *************** ***********@mic rosoft.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.YourPu blicFunctionInF orm1()
End Sub
End Sub

When you create Form2 in Form1 do this.

Dim F2 as New Form2(Me)

Hope it helps
Chris

"kevin" <ke***@discussi ons.microsoft.c om> wrote in message
news:94******** *************** ***********@mic rosoft.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.IsPortAva ilable(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.IsPortAva ilable 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***@discussi ons.microsoft.c om> wrote in message
news:E4******** *************** ***********@mic rosoft.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.YourPu blicFunctionInF orm1()
End Sub
End Sub

When you create Form2 in Form1 do this.

Dim F2 as New Form2(Me)

Hope it helps
Chris

"kevin" <ke***@discussi ons.microsoft.c om> wrote in message
news:94******** *************** ***********@mic rosoft.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.IsPortAva ilable(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.IsPortAva ilable 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***@discussi ons.microsoft.c om> wrote in message
news:E4******** *************** ***********@mic rosoft.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***@discussi ons.microsoft.c om> wrote in message
news:E4******** *************** ***********@mic rosoft.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.MyMeth od", instead of "

Dim MyObject As New MyClass()
MyObject.MyMeth od() ' 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***@discussi ons.microsoft.c om> wrote in message
news:E4******** *************** ***********@mic rosoft.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
4326
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 of class A IS returning a "new" instance of A
1
1585
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 instaniate a new instance e.g. string userinput = "classB"
3
3815
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
5519
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 instance of some class. There was no object instance used. How can I understand object instance? (I don't mean framweork class named object) -- Piotr (Peri, 3P) Perak
15
5276
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 type(myDictionary) I get that the type is "instance", which makes no sense to me. What does that mean? Thanks
2
2001
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. class AnyObject: pass class Container: links = def add(self,other):
14
2514
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 = MyClass() a.b = 1 # OK
0
8600
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9079
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8954
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8796
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6458
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5817
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4555
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.