 |
| 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
|
|
|
|

April 4th, 2004, 08:46 PM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 4th, 2004, 11:11 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
http://p2p.wrox.com/topic.asp?TOPIC_ID=2064
u can find ur answer there as I did!!!
Always:),
Hovik Melkomian.
|
|

April 5th, 2004, 06:35 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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.
|
|

April 5th, 2004, 10:12 AM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 5th, 2004, 12:52 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Use a function to return what you need.
Function blahBeDeBlah as String(Whatever)
do something
Return variable_name
End Function
|
|

April 5th, 2004, 11:29 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
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.
|
|

April 6th, 2004, 02:48 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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.
|
|
 |