My Class

G

Guest

When I create my own class, to use the functions of that class I have to
create an object of that class and then I can access the functions of that
class,

for example:

dim obj1 as myclass
obj1 = new myclass
obj1.myfunction("parameter")

However, when I use .net class e.g. strings I do not need to create a
object, I can directly use it?

e.g.

x = strings.left("abc",2)

What do I have to do, so that the functions of my class can also be accessed
in the same fashion.. e.g.

myclass.myfunction("parameter")
 
G

Guest

I think you are talking about SHARED members. Triy using the SHARED keyword
when declaring your member. You should be able to access that class member
without instantiating an actual object first.

Hope this helps :blush:)
 
M

Michael D. Ober

Take a look at the "static" and "shared" keywords. One of these two should
do this for you. What you're looking for is a non-instanced class
interface.

Mike Ober.
 
H

Herfried K. Wagner [MVP]

patang said:
When I create my own class, to use the functions of that class I have to
create an object of that class and then I can access the functions of that
class,

for example:

dim obj1 as myclass
obj1 = new myclass
obj1.myfunction("parameter")

However, when I use .net class e.g. strings I do not need to create a
object, I can directly use it?

e.g.

x = strings.left("abc",2)

What do I have to do, so that the functions of my class can also be
accessed
in the same fashion..

There are two ways to archieve what you want to do:

* You can add the 'Shared' modifier to your function when
it's part of a class ('Public Shared Function...').

* Or you can add the function to a module. 'Shared' is not
required when doing this. Functions defined in modules
are imported automatically and thus can be accessed
with their unqualified name (for example, 'Left' instead of
'Strings.Left').
 
G

Guest

Does this mean that LEFT or RIGHT functions (methods) are declared as SHARED
in the STRINGS class ?
 
G

Guest

I have one public function in the class and many private functions. Public
functions uses those private functions. When I declare the public function as
"SHARED" then I can't access the private function from within public
functions, it says: " ....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....."
 
Q

Qwert

"Public Shared Function Left(ByVal str As String, ByVal Length As Integer)
As String
Member of Microsoft.VisualBasic.Strings"
- Microsoft documentation.

But there more classes with a method called 'Left'. And they can all do
other things. Check out the specific class for more info.
 
Q

Qwert

You have to make the 'Private' methods also 'Shared' if you want to use them
inside a 'Public Shared' method, or create an instance of the class inside
the 'Public Shared' instance and use that instance to call the 'Private'
methods.
 
G

Guest

one more question:

In my solution only following are imported:

System
System.Data
System.Drawings
System.Windows.Form
System.XML

System.Data.Oledb

How come the methods of Strings class are accessible even though it has not
been imported in my solution?
 
Q

Qwert

I'm guessing because you have "Microsoft.VisualBasic" in your import
statements of your project.

Right click on your project's name in the solution explorer and click on
properties. Select Common Properties -> Imports. There is a list with
Project Imports.
 
G

Guest

Your are right. "Microsoft.VisualBasic" is included in the list of imports
in my project.
 
H

Herfried K. Wagner [MVP]

patang said:
Does this mean that LEFT or RIGHT functions (methods) are declared as
SHARED
in the STRINGS class ?

These functions are declared in the 'Strings' /module/. Methods declared in
a module are implicitly shared.
 
J

JohnR

I'm not 100% clear about what you mean... I understand completely about
having a public shared function in a class (I use one in a singleton
pattern class to hold some global variables). However, when I access that
function I need to fully qualify it (ie: myClass.myfunction() ). On your
second point, I'm not quite sure what you mean by adding the function to a
module.

I have a custom control library which I've built into a DLL. It contains
various *.VB files and each VB file has 1 or more classes defined. These
classes have public subs which I can access with the fully qualified name
(myClass.mySub). I do an IMPORT of this library dll wherever I need it.

What I would like to do is create a sub in this library that I can reference
without having to fully qualify it. It doesn't particularly matter if this
sub is in a class or not, I'd just like to be able to say
"myFunction(...)" without qualification (assume the name is unique).

Is there a way to do this?

Thanks, John
 
H

Herfried K. Wagner [MVP]

JohnR said:
I'm not 100% clear about what you mean... I understand completely about
having a public shared function in a class (I use one in a singleton
pattern class to hold some global variables). However, when I access that
function I need to fully qualify it (ie: myClass.myfunction() ). On your
second point, I'm not quite sure what you mean by adding the function to a
module.

I have a custom control library which I've built into a DLL. It contains
various *.VB files and each VB file has 1 or more classes defined. These
classes have public subs which I can access with the fully qualified name
(myClass.mySub). I do an IMPORT of this library dll wherever I need it.

What I would like to do is create a sub in this library that I can
reference without having to fully qualify it. It doesn't particularly
matter if this sub is in a class or not, I'd just like to be able to say
"myFunction(...)" without qualification (assume the name is unique).

Is there a way to do this?


Add an additional file to your class library:

\\\
Public Module CommonFunctions
Public Sub DoSomething()
...
End Sub
End Module
///

In the project that uses the DLL import the DLL's root namespace
('ClassLibrary1'). Then you will be able to call 'DoEvents' without
qualification. If you are using a class instead of the module

\\\
Public Class CommonFunctions
Public Shared Sub DoSomething()
...
End Sub
End Class
///

and import the class in the client project that uses the DLL ('Imports
ClassLibrary1.CommonFunctions') you will be able to call 'DoSomething'
without fully qualifying it too.
 
C

Cor Ligthert

Herfried,

I was writing as well an answer on the question when I saw yours.

You can for sure interpret my message wrong, therefore this message.

My message about shared and non shared is *not* based on your message. I had
already written it before that I saw yours.

Cor
 
C

Cor Ligthert

Herfried,

I readed this while busy for a sample about
"Set Controls dinamically? is this for experts?"

and thought that this was your answer on that, so forget it.

Sorry

Cor
 
J

JohnR

Herfried,

Thank you so much for your response. It worked out very well. The
thing I was missing was not realizing that you could do a:

Imports ClassLibrary.CommonFunctions

I thought you could only import the ClassLibrary itself... You really
cleared it up for me...

Thanks again, John
 

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