Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 4th, 2004, 08:46 PM
Registered User
 
Join Date: Mar 2004
Location: london, Middlesex, United Kingdom.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Global variables

can anyone tell me how to assign a value in a sub routine into a global variable? an example of my code is shown below...

Dim Number1 as Integer
Dim RAnswer as Integer

Sub Start_Click

'other coding goes here..................

Number1 = objDataReader("QNo")
RAnswer = objDataReader("QAns")

End sub

i want to assign the two numbers retreived from the datareader into the global variables Number1 and RAnswer. can anyone also tell me if i have declared the global variables correctly. Thanx


  #2 (permalink)  
Old April 4th, 2004, 11:11 PM
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=2064
u can find ur answer there as I did!!!

Always:),
Hovik Melkomian.
  #3 (permalink)  
Old April 5th, 2004, 06:35 AM
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

That looks perfectly fine to me.

One bit of advice, most standards recommend prefixing globals with something to indicate they are global. Often you will see something like this...

Dim _objSomeObject As Object
Dim _nSomeInt As Integer

The _ indicates it's global. Also, try to limit use of global variables. You can often avoid them with the right use of function parameters or return values/objects, particularly now that .net is fully object oriented.

Peter
------------------------------------------------------
Work smarter, not harder.
  #4 (permalink)  
Old April 5th, 2004, 10:12 AM
Registered User
 
Join Date: Mar 2004
Location: london, Middlesex, United Kingdom.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,
thanx for the guidance ebut i have realised what the problem is now. in my first subroutine, i am passign the values into the global variable but when i am calling the same variable in a second subroutine, expecting it to hold the value from the first subroutine, it doesn't. it only holds the default values. how can i get it to hold the value from the first subroutine so i can use it in the second. i have been told i can call the first subroutine in the second but can anyone tell me if this is the way??? thanx

  #5 (permalink)  
Old April 5th, 2004, 12:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,111
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Use a function to return what you need.

Function blahBeDeBlah as String(Whatever)
    do something
    Return variable_name
End Function

  #6 (permalink)  
Old April 5th, 2004, 11:29 PM
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

I got my lesson & let me to tell u that too:D
ur calss will be as:
Code:
    public class Const
    {
        private static string connectionString = "ur value";
        public static string DabirConn
        {
            set
            {
                connectionString = value;
            }
            get
            {
                return connectionString;
            }
        }
    }

Then to set & get the value
use
Code:
[u]set:</u>
Class_Name.DabirConn = "new value";
[u]get:</u>
a = Class_Name.DabirConn;
This is what I did & works fine.
HTH.

Always:),
Hovik Melkomian.
  #7 (permalink)  
Old April 6th, 2004, 02:48 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

ps124-

Simple data types, like Integers, are "value" types. The variable holds a value. (The other type is a reference type). Typically, when you pass such a variable to a function, you pass the variable's value. In essense, what you are doing in a function declaration is declaring a local scope variable for use inside the function. As a result, any modification to that variable will not be seen outside the function. As such, passing a global variable into a function kind of defeats the purpose of having a global variable. All you need to do is use the global variable inside the function and its value will be persisted between function calls. This way, a value assigned to the global variable in one function will be seen by the second function.

Peter
------------------------------------------------------
Work smarter, not harder.
 


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
Maintaining Global variables tenikiwon Beginning PHP 1 June 19th, 2003 02:11 PM



All times are GMT -4. The time now is 08:09 AM.


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