473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Global Routines in a Class

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\ListC lasses.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

Jun 8 '06 #1
4 1093
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.
mosscliffe wrote:
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\ListC lasses.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

Jun 8 '06 #2
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.
Ray Booysen wrote:
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.
mosscliffe wrote:
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\ListC lasses.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


Jun 8 '06 #3
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


"mosscliffe" wrote:
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.
Ray Booysen wrote:
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.
mosscliffe wrote:
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\ListC lasses.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


Jun 8 '06 #4
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 !!.
Peter wrote:
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


Jun 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
10
by: ankisharma | last post by:
Hi all At many places I have seen that programmers pass global variables to functions in c. I am not able to figure out why they do so. need some clues on this. somewhere i heard that this...
25
by: Daniel Bass | last post by:
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having constants in a program which apply to several...
2
by: Matthew Hood | last post by:
I'm at a loss on how to create global functions for my website. Essentially, I have several different functions I use throughout my site and I want to be able to put these in 1 file and call them...
2
by: tshad | last post by:
I am trying to set up some shared functions in my Global.asax file and can't seem to get it to work. In my Global.asax: Public Class myUtils inherits System.Web.HttpApplication public...
1
by: fd123456 | last post by:
Hi tshad, sorry for not coming back to you earlier. When I meant to add a method to Global.asax, I actually meant to add it to the code-behind file. If you click on "Global.asax" in the file...
9
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
11
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global"...
1
by: Web Search Store | last post by:
Hello, Is there a place I can put a class of global variables and have them available in the page_load routine? Here is a class: Public Class globalvar Public ddd As String = "howdy"
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...

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.