Global Routines in a Class

M

mosscliffe

I am trying to create Common Routines in a Class to use within my site,
but as usual I am struggling to understand the fundamentals.

I have the following

Class dbase

Public Shared Function MySub(ByVal tLookup, ByVal Flag) As String
Return MyFuncInSub("Test String")

End Function

Private Function MyFuncInSub(ByVal tLookup) As String
Return "Hello From My privateFuncFunction"
End Function

end class

The idea is I call the Shared part with
dim newstr as string = dbase.MySub

and during the processing of MySub, it calls the private Function
MyFuncInSub

BUT the compiler says

Error 1 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. C:\app_data\webdev\websites\pplists\App_Code\ListClasses.vb 114 16 C:\...\pplists\

If I change the Private Function to Public it gets through the
compiler, but does that not go against the principle of Private Parts
of a CLass.

Please can someone show me the light

Thanks
 
R

Ray Booysen

Hi mosscliffe

You forgot to add shared to the MyFuncInSub method.

Because shared allows code to run the code without declaring an instance
of the class, any code that this shared method calls must also be shared.
 
M

mosscliffe

Thank you very much and for the explanation.

I do have a follow up question. I was trying to be efficient in using
Shared Routines in Classes, but I read somewhere that they can be used
by other users and that they were not exclusive to the current Session.
Does this mean that they could give wrong results as a result of other
user's different values, being passed.

In which case is it better to create local copies. I was thinking
about avoiding the overhead of time and memory in not using this
approach.

Any pointers or comments welcome.
 
G

Guest

If a shared (static) method / class does not store state in shared/ static
fields, (which could be clobbered by another thread attempting to access/
modify it) then you do not have this problem with shared methods.

If you did have stored values you would need to use the lock statement to
prevent other threads from accessing this data until the current thread has
exited from the method call.
Peter


--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
M

mosscliffe

Thank you for that.

If I have understood you correctly. If I only have local variables for
the duration of the routine and return variables by Value at the End
and I never re-enter and use previous values, then I am safe to use
Shared routines.

Would I be safe to set Session(vars), because their storage would be
outside the shared routine ?

Sorry to be so dense about this, but I am an 'Oldie', who is not
completely au-fait with all the terms - yet !!.
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top