Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 5th, 2004, 03:02 AM
Registered User
 
Join Date: Jun 2004
Location: , , Iran.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Global Variables in .NET

Hi evryone
Tis is my first message in wrox P2P:)
In WINDOWS APPLICATIONS for example in Classes when I declare a global variable(like int a=0;)
and use this variable in different procedures for example I change the value in one procedure(procedure 1)(like a=1;) and when in another procedure(procedure 2) I want to obtain the value it is correct(I mean a is 1 not 0 or null)...but....
in ASP.NET APPLICATIONS if I do this method just like above the value is not correct
(in asp.net application in procedure 2 a is 0 not 1 )
if I use another type of variables like string or even classes the results are the same.
I can not get why...
Could someone explain it...
With thanks and regards.

Reply With Quote
  #2 (permalink)  
Old July 5th, 2004, 08:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

If you are trying to share the same value among forms, modules, classes, etc., you need to declare the global variable as static in C#. That will make the value the same among all procedures.

Brian
Reply With Quote
  #3 (permalink)  
Old July 5th, 2004, 09:49 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Can you describe what you are intending to do with these global variables? This may provide us more information to determine what is "not working". Working with globals can be tricky depending on the situation. You should try to avoid them as much as possible. It's possible that what you are tryig to achieve can not be done the way you think it can and that there is a better way of doing it in ASP.NET.
Reply With Quote
  #4 (permalink)  
Old July 6th, 2004, 12:39 AM
Registered User
 
Join Date: Jun 2004
Location: , , Iran.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

>Can you describe what you are intending to do with these global variables?
I have a DataAdapter object for working on my database. you know in evry procedure the adapter should be recreated .I don't want to do that(I mean recreating adapter in evry procedure)
finaly by declaring my adapter as a static global varable my problem has been solved.but because of security reasons It will not be a good manner
I think recarating it in evry procedure is a better way
but for other ordinary global variables by declaring them as static they behave really globally in ASP.NET applications
any idea?
is sthere any better ways?
Thanks to bmains and planoie.


Reply With Quote
  #5 (permalink)  
Old July 6th, 2004, 02:03 AM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 69
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can declare variables truly Global by declaring them statically in the global.asax.cs file.

www.CoderForRent.com
Get A Computer Job!
Reply With Quote
  #6 (permalink)  
Old July 6th, 2004, 02:27 AM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

http://p2p.wrox.com/topic.asp?TOPIC_ID=12408
HTH

Always:),
Hovik Melkomian.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
global variables Komila ASP.NET 3.5 Basics 2 October 31st, 2008 01:14 PM
Global Variables in C# su C# 2 December 11th, 2006 06:18 PM
Global variables zarol Beginning PHP 3 May 10th, 2005 06:17 AM
Global Variables in ASP.NET mehdi62b ASP.NET 1.0 and 1.1 Professional 11 August 8th, 2004 03:15 AM
Global variables ps124 ASP.NET 1.0 and 1.1 Basics 6 April 6th, 2004 02:48 PM



All times are GMT -4. The time now is 07:23 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.