Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 4th, 2004, 08:46 PM
Registered User
 
Join Date: Mar 2004
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


 
Old April 4th, 2004, 11:11 PM
Friend of Wrox
 
Join Date: Jun 2003
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.
 
Old April 5th, 2004, 06:35 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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.
 
Old April 5th, 2004, 10:12 AM
Registered User
 
Join Date: Mar 2004
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

 
Old April 5th, 2004, 12:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
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

 
Old April 5th, 2004, 11:29 PM
Friend of Wrox
 
Join Date: Jun 2003
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.
 
Old April 6th, 2004, 02:48 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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.





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.