Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use a class method or function without instantiating

Status
Not open for further replies.

netangel

Programmer
Feb 7, 2002
124
PT
I'm having a problem using a class method or function without instantiating the class. I need to be able to use the functions and methods of a class avoiding to instantiate it.
Now I'm using something like:
(New ClassName).MyMethod()

But as I write two or three chained functions, it becomes impossible to understand:
(New ClassName).MyMethod1((New ClassName).MyMethod2())

I'd like to able to use something like:
ClassName.MyMethod(ClassName.MyMethod())

Does anyone knows a way of writing classes that allow this?
I know JAVA allows it.
NetAngel
 
May I ask why you are trying to avoid instantiating it. I am not sure but I believe you may be instantiating anyway or at least for the duration of the function call.
I may be completely off here so help me out if I am guys.
Doing calls this way would mean alot of instantiations of that class.

That's my two cents anyway. That'l do donkey, that'l do
[bravo]
 
To use a class, you need to instantiate it or it will be done for you. You could encapsulate your functions with interfaces that would instantiate those classes and then run those functions. This way you would not have to chain your functions. Example:

ClassA
Public Function MyFunc2More()
{
Set Obj = New ClassB
MyFunc1(Obj.Operation1)
Set Obj = Nothing
}
Public Function MyFunc1()
{
' My code...
}

ClassB
Public Function Operation1
{
' My Code...
}


So, your call is minimized to ...

MyObject.MyFunc2More

Optionally, you could export those functions to a code library module and call your functions using similar techniques mentioned above.

The code above was obviously not actual code, it was just to demo the concept.

Gary
gwinn7
A+, Network+
 
In C#, to call methods without instantiating the class, you'd declare them as static. I don't know the equivalent keyword in VB7, but I'm sure it has one.

The limitation in using static methods is that you can't access any instance data - none of the private data in the class is available. It's really only good for "pure code" methods.

Chip H.
 
chiph is correct. In VB.NET the keyword is shared

For example - when the form loads, it will call a function without instantiating the class.

Public Class Class1
Public Shared Function Hello() As String
Return "Hi"
End Function
End Class

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show(Class1.Hello)
End Sub
End Class
 
Thanks, Custom24.

Keyword in VB7 is "Shared".

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top