generic function in a class to be used by reference only

G

Guest

From asp.net (vb) or another winforms vb.net application I would like to us
generic functions compiled in .dl
Of course a reference is made to the .dl

All the examples show a component to be compiled as a creatable objec
I would like to use the functions more like a librar

for instanc

public Class MyComponen
public function GetText() as strin
return "This is text
end functio
end clas

New project will use it like

dim mycomp as mycomponen
dim s as strin

s = mycomp.GetTex

In vb6 I could compile a .dll to use the function global multiuse withou
the need to instantiate an object firs
like thi

- make a reference to mycomponent.dl

dim s=GetText (directly without an object

This must be possible in .net because all the FrameWor
components work like tha
just import system.data.sqlclient and I can use all the functions from sqlclien

Can anyone give a hint because all solutions upto now do it by instantiating a

thank you elka
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?ZWxrYQ==?= said:
From asp.net (vb) or another winforms vb.net application I would like to use
generic functions compiled in .dll
Of course a reference is made to the .dll

All the examples show a component to be compiled as a creatable object
I would like to use the functions more like a library

for instance

public Class MyComponent
public function GetText() as string
return "This is text"
end function
end class

New project will use it like :

dim mycomp as mycomponent
dim s as string

s = mycomp.GetText


In vb6 I could compile a .dll to use the function global multiuse without
the need to instantiate an object first
like this

- make a reference to mycomponent.dll

dim s=GetText (directly without an object)

This must be possible in .net because all the FrameWork
components work like that
just import system.data.sqlclient and I can use all the functions from sqlclient

Can anyone give a hint because all solutions upto now do it by instantiating a

'Public Shared Function GetText(...) As String' will allow you to call
the function using 'MyComponent.GetText(...)' (without instantiating
it).
 
J

Jay B. Harlow [MVP - Outlook]

Elka,
In addition to use Shared within a class as Herfried showed. (which is what
I prefer as its better encapsulated).

You can define GetText within a public module:
public Class MyComponent
public Shared function GetText() as string
return "This is text"
end function
end class
public Module MyComponent
public function GetText() as string
return "This is text"
end function
end Module

The first requires you to use:

s = MyComponent.GetText()

While the second allows:

s = GetText()

I prefer the first as its obvious that the GetText function is coming from
the MyComponent type.

Hope this helps
Jay
 
H

Herfried K. Wagner [MVP]

Jay,

* "Jay B. Harlow said:
The first requires you to use:

s = MyComponent.GetText()

While the second allows:

s = GetText()

I prefer the first as its obvious that the GetText function is coming from
the MyComponent type.

In addition to what you are saying:

You can import the class so you can use the shared method without
qualifying it. But I think that's not a good idea...
 
J

Jay B. Harlow [MVP - Outlook]

Herfried,
D'oh!
You can import the class so you can use the shared method without
qualifying it. But I think that's not a good idea...

I meant to add the import the class to make it a module trick ;-)

I rarely use the import class trick, however it is handy for truly global
functions, such as System.Math

Jay
 

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