473,400 Members | 2,163 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,400 software developers and data experts.

Global Variable in Class

Hi,

How can I declare a variable in a Class so that it can be accessed from the
page that calls it? Normally I would declare a variable outside a function
so that it can be used globally on the page but I'm not sure how to do it
in a Class. I tried using public but that didn't work. I have the following:

Public Class dataClass
Public Shared Function GetRep(ByVal prodNumber As Integer) As
DataSet
Dim ProdName as String

I tried Public ProdName as String but got an error.

Thanks

--
Message posted via http://www.dotnetmonster.com
Nov 19 '05 #1
3 1407
Shared functions can only only access Shared class variables. If you
need GetRep to be Shared, and you need to use ProdName within GetRep,
you must declare GetRep as Shared.

Keep in mind, when you use Shared class level variables in a
multi-threaded environment (like ASP.NET), you need to be careful that
you only use the variable in a thread-safe way (look at the
documentation for the SyncLock keyword for a start).

Joshua Flanagan
http://flimflan.com/blog
Joe via DotNetMonster.com wrote:
Hi,

How can I declare a variable in a Class so that it can be accessed from the
page that calls it? Normally I would declare a variable outside a function
so that it can be used globally on the page but I'm not sure how to do it
in a Class. I tried using public but that didn't work. I have the following:

Public Class dataClass
Public Shared Function GetRep(ByVal prodNumber As Integer) As
DataSet
Dim ProdName as String

I tried Public ProdName as String but got an error.

Thanks

Nov 19 '05 #2
Thanks for the reply. I have declared the function as shared. How would I
define the variable within the function?

--
Message posted via http://www.dotnetmonster.com
Nov 19 '05 #3
Now I'm confused. Do you want the variable to be in the function (no
data is saved across function calls), or do you want the variable
outside of the function (dad is saved across function calls)?

If you want the variable inside the function, just declare it in the
function:

Public Class dataClass
Public Shared Function GetRep(ByVal prodNumber As Integer) As DataSet
Dim ProdName as String
'do stuff with ProdName - value is lost at end of function
End Function
End Class

If you want the variable outside the function, just declare it outside
of the function (but make sure to add the Shared modifier so it is
available to Shared functions):
Public Class dataClass
Private Shared ProdName as String
Public Shared Function GetRep(ByVal prodNumber As Integer) As DataSet
'do stuff with ProdName
' value is still available after function
End Function
End Class

Joe via DotNetMonster.com wrote:
Thanks for the reply. I have declared the function as shared. How would I
define the variable within the function?

Nov 19 '05 #4

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

Similar topics

2
by: Otto Wyss | last post by:
I have a string variable g_appname which should be global accessable by any class who whishes. I have defined it as wxString *g_appname = NULL; and fills it later on. Now a friend asked why I...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
9
by: Shapper | last post by:
Hello, I am declaring a variable in my aspx.vb code as follows: Public Class catalogue Public productid As String Private Sub Page_Load ... I have an image button where I call the...
8
by: Thomas Coleman | last post by:
Ok, I've obviously discovered that Global.aspx has been completely changed in ..NET 2.0. However, I haven't figured out how to declare a constant that's available to any page in my application...
8
by: Morpheus | last post by:
Hello, Say I have a class with a member... char mId; Whenever an object is created, I want to assign an incrementing character to this member, for instance the first would be A, then B, C,...
4
by: Sheldon | last post by:
Hi, I have a series of classes that are all within the same file. Each is called at different times by the main script. Now I have discovered that I need several variables returned to the main...
15
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the...
10
by: ma | last post by:
Hello, I want to create a global class. To do this I did the followings: 1- Create a class name test. It has a public variable named mystring. public class test { public string mystring =...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.