473,545 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delcare a Public Variable.

Newbie learning in VB.NET 2.

I'm creating a simple ASP.NET 2 page and I pulling a querystring from a link
and I want to use this querystring over and over again through out the page.

Example.

Public Variable
Dim myVar as string = Request.queryst ring("MyVar")
End Public Variable

Then I can just call this variable from anywhere to grab MyVar.

Thanks!
Phil
Feb 15 '07 #1
10 1860
Just put:

Dim myVar as string

in your page class but not inside any particular sub or function and then
put:

myVar = Request.queryst ring("MyVar")

in your Page_Load sub and it will be available to all other subs & functions
throughout the page.


"Phillip Vong" <phillip_vong*a t*yahoo*dot*com wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
Newbie learning in VB.NET 2.

I'm creating a simple ASP.NET 2 page and I pulling a querystring from a
link and I want to use this querystring over and over again through out
the page.

Example.

Public Variable
Dim myVar as string = Request.queryst ring("MyVar")
End Public Variable

Then I can just call this variable from anywhere to grab MyVar.

Thanks!
Phil

Feb 15 '07 #2
Declare the string variable above the Page_Load method (not inside the body
of the method. This puts it's scope to class level.
Then, assign the value either there or inside the Page_Load method.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Phillip Vong" wrote:
Newbie learning in VB.NET 2.

I'm creating a simple ASP.NET 2 page and I pulling a querystring from a link
and I want to use this querystring over and over again through out the page.

Example.

Public Variable
Dim myVar as string = Request.queryst ring("MyVar")
End Public Variable

Then I can just call this variable from anywhere to grab MyVar.

Thanks!
Phil
Feb 15 '07 #3
Or create a property with lazy intialization:

Private _myVar As String
Protected ReadOnly Property MyVar() As String
Get
If _myVar Is Nothing Then
_myVar = Request.QuerySt ring("MyVar")
If _myVar Is Nothing Then
_myVar = String.Empty
End If
End If
Return _myVar
End Get
End Property

I may look horrible, but it's actually simple idea. Then wherever on the
page code you can use the property:

Protected Sub Page_Load(ByVal sender As Object,
ByVal e As System.EventArg s) Handles Me.Load

If Not IsPostBack Then
DisplaySomeInfo rmationBasedOnM yVarValue(MyVar )
End If

end sub

--
Milosz
"Peter Bromberg [C# MVP]" wrote:
Declare the string variable above the Page_Load method (not inside the body
of the method. This puts it's scope to class level.
Then, assign the value either there or inside the Page_Load method.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Phillip Vong" wrote:
Newbie learning in VB.NET 2.

I'm creating a simple ASP.NET 2 page and I pulling a querystring from a link
and I want to use this querystring over and over again through out the page.

Example.

Public Variable
Dim myVar as string = Request.queryst ring("MyVar")
End Public Variable

Then I can just call this variable from anywhere to grab MyVar.

Thanks!
Phil

Feb 16 '07 #4
By making the variable the way we have suggested, it become a class field
and is accessible anywhere within the class by simply using its name or, if
you like, me.myVar, to get intelliSense. I prefer the 2 line solution.
"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plwrot e in message
news:FA******** *************** ***********@mic rosoft.com...
Or create a property with lazy intialization:

Private _myVar As String
Protected ReadOnly Property MyVar() As String
Get
If _myVar Is Nothing Then
_myVar = Request.QuerySt ring("MyVar")
If _myVar Is Nothing Then
_myVar = String.Empty
End If
End If
Return _myVar
End Get
End Property

I may look horrible, but it's actually simple idea. Then wherever on the
page code you can use the property:

Protected Sub Page_Load(ByVal sender As Object,
ByVal e As System.EventArg s) Handles Me.Load

If Not IsPostBack Then
DisplaySomeInfo rmationBasedOnM yVarValue(MyVar )
End If

end sub

--
Milosz
"Peter Bromberg [C# MVP]" wrote:
>Declare the string variable above the Page_Load method (not inside the
body
of the method. This puts it's scope to class level.
Then, assign the value either there or inside the Page_Load method.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Phillip Vong" wrote:
Newbie learning in VB.NET 2.

I'm creating a simple ASP.NET 2 page and I pulling a querystring from a
link
and I want to use this querystring over and over again through out the
page.

Example.

Public Variable
Dim myVar as string = Request.queryst ring("MyVar")
End Public Variable

Then I can just call this variable from anywhere to grab MyVar.

Thanks!
Phil

Feb 16 '07 #5
Actually, if the value is expensive to get, it makes sense only to get
it if needed.

The choise of method depends on the situation, i.e. how the value is
fetched.

Scott M. wrote:
By making the variable the way we have suggested, it become a class field
and is accessible anywhere within the class by simply using its name or, if
you like, me.myVar, to get intelliSense. I prefer the 2 line solution.
"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plwrot e in message
news:FA******** *************** ***********@mic rosoft.com...
>Or create a property with lazy intialization:

Private _myVar As String
Protected ReadOnly Property MyVar() As String
Get
If _myVar Is Nothing Then
_myVar = Request.QuerySt ring("MyVar")
If _myVar Is Nothing Then
_myVar = String.Empty
End If
End If
Return _myVar
End Get
End Property

I may look horrible, but it's actually simple idea. Then wherever on the
page code you can use the property:

Protected Sub Page_Load(ByVal sender As Object,
ByVal e As System.EventArg s) Handles Me.Load

If Not IsPostBack Then
DisplaySomeInf ormationBasedOn MyVarValue(MyVa r)
End If

end sub

--
Milosz
"Peter Bromberg [C# MVP]" wrote:
>>Declare the string variable above the Page_Load method (not inside the
body
of the method. This puts it's scope to class level.
Then, assign the value either there or inside the Page_Load method.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Phillip Vong" wrote:

Newbie learning in VB.NET 2.

I'm creating a simple ASP.NET 2 page and I pulling a querystring from a
link
and I want to use this querystring over and over again through out the
page.

Example.

Public Variable
Dim myVar as string = Request.queryst ring("MyVar")
End Public Variable

Then I can just call this variable from anywhere to grab MyVar.

Thanks!
Phil


--
Göran Andersson
_____
http://www.guffa.com
Feb 16 '07 #6
Yes, but that is not the case here, the value is simply a queryString value.
"Göran Andersson" <gu***@guffa.co mwrote in message
news:uV******** ******@TK2MSFTN GP02.phx.gbl...
Actually, if the value is expensive to get, it makes sense only to get it
if needed.

The choise of method depends on the situation, i.e. how the value is
fetched.

Scott M. wrote:
>By making the variable the way we have suggested, it become a class field
and is accessible anywhere within the class by simply using its name or,
if you like, me.myVar, to get intelliSense. I prefer the 2 line
solution.
"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plwrot e in message
news:FA******* *************** ************@mi crosoft.com...
>>Or create a property with lazy intialization:

Private _myVar As String
Protected ReadOnly Property MyVar() As String
Get
If _myVar Is Nothing Then
_myVar = Request.QuerySt ring("MyVar")
If _myVar Is Nothing Then
_myVar = String.Empty
End If
End If
Return _myVar
End Get
End Property

I may look horrible, but it's actually simple idea. Then wherever on the
page code you can use the property:

Protected Sub Page_Load(ByVal sender As Object,
ByVal e As System.EventArg s) Handles Me.Load

If Not IsPostBack Then
DisplaySomeIn formationBasedO nMyVarValue(MyV ar)
End If

end sub

--
Milosz
"Peter Bromberg [C# MVP]" wrote:

Declare the string variable above the Page_Load method (not inside the
body
of the method. This puts it's scope to class level.
Then, assign the value either there or inside the Page_Load method.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Phillip Vong" wrote:

Newbie learning in VB.NET 2.
>
I'm creating a simple ASP.NET 2 page and I pulling a querystring from
a link
and I want to use this querystring over and over again through out the
page.
>
Example.
>
Public Variable
Dim myVar as string = Request.queryst ring("MyVar")
End Public Variable
>
Then I can just call this variable from anywhere to grab MyVar.
>
Thanks!
Phil
>
>
>



--
Göran Andersson
_____
http://www.guffa.com

Feb 16 '07 #7
If you only ever write answers to the exact question, often you will not
be able to give the answer that the poster needs. Many times people
don't know what to ask for, or are only asking for the method that they
believe is the solution. Sometimes they even forget to ask a question,
so then you would not be able to reply at all...

I think that a read only property is one option that should be presented
in a discussion about "public" variables.

Scott M. wrote:
Yes, but that is not the case here, the value is simply a queryString value.
"Göran Andersson" <gu***@guffa.co mwrote in message
news:uV******** ******@TK2MSFTN GP02.phx.gbl...
>Actually, if the value is expensive to get, it makes sense only to get it
if needed.

The choise of method depends on the situation, i.e. how the value is
fetched.

Scott M. wrote:
>>By making the variable the way we have suggested, it become a class field
and is accessible anywhere within the class by simply using its name or,
if you like, me.myVar, to get intelliSense. I prefer the 2 line
solution.
"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plwrot e in message
news:FA****** *************** *************@m icrosoft.com...
Or create a property with lazy intialization:

Private _myVar As String
Protected ReadOnly Property MyVar() As String
Get
If _myVar Is Nothing Then
_myVar = Request.QuerySt ring("MyVar")
If _myVar Is Nothing Then
_myVar = String.Empty
End If
End If
Return _myVar
End Get
End Property

I may look horrible, but it's actually simple idea. Then wherever on the
page code you can use the property:

Protected Sub Page_Load(ByVal sender As Object,
ByVal e As System.EventArg s) Handles Me.Load

If Not IsPostBack Then
DisplaySomeI nformationBased OnMyVarValue(My Var)
End If

end sub

--
Milosz
"Peter Bromberg [C# MVP]" wrote:

Declare the string variable above the Page_Load method (not inside the
body
of the method. This puts it's scope to class level.
Then, assign the value either there or inside the Page_Load method.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
>
>
>
>
"Phillip Vong" wrote:
>
>Newbie learning in VB.NET 2.
>>
>I'm creating a simple ASP.NET 2 page and I pulling a querystring from
>a link
>and I want to use this querystring over and over again through out the
>page.
>>
>Example.
>>
>Public Variable
>Dim myVar as string = Request.queryst ring("MyVar")
>End Public Variable
>>
>Then I can just call this variable from anywhere to grab MyVar.
>>
>Thanks!
>Phil
>>
>>
>>

--
Göran Andersson
_____
http://www.guffa.com


--
Göran Andersson
_____
http://www.guffa.com
Feb 17 '07 #8
Fine, but if you only provide solutions without explanations as to why and
where they should be used (as you did in your first reply) you aren't
serving the OP either.

A specific question was asked and a specific answer was given. You could
have simply started your post with: In situations where retrieving the
property value is expensive, a property might be a better alternative...
"Göran Andersson" <gu***@guffa.co mwrote in message
news:uQ******** *****@TK2MSFTNG P03.phx.gbl...
If you only ever write answers to the exact question, often you will not
be able to give the answer that the poster needs. Many times people don't
know what to ask for, or are only asking for the method that they believe
is the solution. Sometimes they even forget to ask a question, so then you
would not be able to reply at all...

I think that a read only property is one option that should be presented
in a discussion about "public" variables.

Scott M. wrote:
>Yes, but that is not the case here, the value is simply a queryString
value.
"Göran Andersson" <gu***@guffa.co mwrote in message
news:uV******* *******@TK2MSFT NGP02.phx.gbl.. .
>>Actually, if the value is expensive to get, it makes sense only to get
it if needed.

The choise of method depends on the situation, i.e. how the value is
fetched.

Scott M. wrote:
By making the variable the way we have suggested, it become a class
field and is accessible anywhere within the class by simply using its
name or, if you like, me.myVar, to get intelliSense. I prefer the 2
line solution.
"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plwrot e in message
news:FA***** *************** **************@ microsoft.com.. .
Or create a property with lazy intialization:
>
Private _myVar As String
Protected ReadOnly Property MyVar() As String
Get
If _myVar Is Nothing Then
_myVar = Request.QuerySt ring("MyVar")
If _myVar Is Nothing Then
_myVar = String.Empty
End If
End If
Return _myVar
End Get
End Property
>
I may look horrible, but it's actually simple idea. Then wherever on
the
page code you can use the property:
>
Protected Sub Page_Load(ByVal sender As Object,
ByVal e As System.EventArg s) Handles Me.Load
>
If Not IsPostBack Then
DisplaySome InformationBase dOnMyVarValue(M yVar)
End If
>
end sub
>
--
Milosz
>
>
"Peter Bromberg [C# MVP]" wrote:
>
>Declare the string variable above the Page_Load method (not inside
>the body
>of the method. This puts it's scope to class level.
>Then, assign the value either there or inside the Page_Load method.
>Peter
>--
>Site: http://www.eggheadcafe.com
>UnBlog: http://petesbloggerama.blogspot.com
>Short urls & more: http://ittyurl.net
>>
>>
>>
>>
>"Phillip Vong" wrote:
>>
>>Newbie learning in VB.NET 2.
>>>
>>I'm creating a simple ASP.NET 2 page and I pulling a querystring
>>from a link
>>and I want to use this querystring over and over again through out
>>the page.
>>>
>>Example .
>>>
>>Public Variable
>>Dim myVar as string = Request.queryst ring("MyVar")
>>End Public Variable
>>>
>>Then I can just call this variable from anywhere to grab MyVar.
>>>
>>Thanks!
>>Phil
>>>
>>>
>>>
--
Göran Andersson
_____
http://www.guffa.com



--
Göran Andersson
_____
http://www.guffa.com

Feb 17 '07 #9
Scott M. wrote:
Fine, but if you only provide solutions without explanations as to why and
where they should be used (as you did in your first reply) you aren't
serving the OP either.
Perhaps you should read my first reply again? Where do you see me
providing a solution without any explanation?
A specific question was asked and a specific answer was given. You could
have simply started your post with: In situations where retrieving the
property value is expensive, a property might be a better alternative...
I started my post with: "Actually, if the value is expensive to get, it
makes sense only to get it if needed."'

I don't see why you think that this differs in any way that matters from
what you suggested.
>
"Göran Andersson" <gu***@guffa.co mwrote in message
news:uQ******** *****@TK2MSFTNG P03.phx.gbl...
>If you only ever write answers to the exact question, often you will not
be able to give the answer that the poster needs. Many times people don't
know what to ask for, or are only asking for the method that they believe
is the solution. Sometimes they even forget to ask a question, so then you
would not be able to reply at all...

I think that a read only property is one option that should be presented
in a discussion about "public" variables.

Scott M. wrote:
>>Yes, but that is not the case here, the value is simply a queryString
value.
"Göran Andersson" <gu***@guffa.co mwrote in message
news:uV****** ********@TK2MSF TNGP02.phx.gbl. ..
Actually, if the value is expensive to get, it makes sense only to get
it if needed.

The choise of method depends on the situation, i.e. how the value is
fetched.

Scott M. wrote:
By making the variable the way we have suggested, it become a class
field and is accessible anywhere within the class by simply using its
name or, if you like, me.myVar, to get intelliSense. I prefer the 2
line solution.
>
>
"Milosz Skalecki [MCAD]" <mi*****@DONTLI KESPAMwp.plwrot e in message
news:FA**** *************** *************** @microsoft.com. ..
>Or create a property with lazy intialization:
>>
>Private _myVar As String
>Protecte d ReadOnly Property MyVar() As String
>Get
>If _myVar Is Nothing Then
>_myVar = Request.QuerySt ring("MyVar")
>If _myVar Is Nothing Then
>_myVar = String.Empty
>End If
>End If
>Return _myVar
>End Get
>End Property
>>
>I may look horrible, but it's actually simple idea. Then wherever on
>the
>page code you can use the property:
>>
>Protecte d Sub Page_Load(ByVal sender As Object,
>ByVal e As System.EventArg s) Handles Me.Load
>>
>If Not IsPostBack Then
>DisplaySom eInformationBas edOnMyVarValue( MyVar)
>End If
>>
>end sub
>>
>--
>Milosz
>>
>>
>"Peter Bromberg [C# MVP]" wrote:
>>
>>Declare the string variable above the Page_Load method (not inside
>>the body
>>of the method. This puts it's scope to class level.
>>Then, assign the value either there or inside the Page_Load method.
>>Peter
>>--
>>Site: http://www.eggheadcafe.com
>>UnBlog: http://petesbloggerama.blogspot.com
>>Short urls & more: http://ittyurl.net
>>>
>>>
>>>
>>>
>>"Philli p Vong" wrote:
>>>
>>>Newbie learning in VB.NET 2.
>>>>
>>>I'm creating a simple ASP.NET 2 page and I pulling a querystring
>>>from a link
>>>and I want to use this querystring over and over again through out
>>>the page.
>>>>
>>>Exampl e.
>>>>
>>>Public Variable
>>>Dim myVar as string = Request.queryst ring("MyVar")
>>>End Public Variable
>>>>
>>>Then I can just call this variable from anywhere to grab MyVar.
>>>>
>>>Thanks !
>>>Phil
>>>>
>>>>
>>>>
--
Göran Andersson
_____
http://www.guffa.com

--
Göran Andersson
_____
http://www.guffa.com


--
Göran Andersson
_____
http://www.guffa.com
Feb 17 '07 #10

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

Similar topics

9
4975
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived...
10
5765
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this (also copied below for easier reference): http://groups.google.it/groups?hl=en&lr=&safe=off&selm=6beiuk%24cje%40netlab.cs.rpi.edu&rnum=95 I don't...
2
1098
by: Kai Wu | last post by:
Hello, Suppose a template class template<typename T> class A{ ....... }; now i wish to typedef a vector of class A, how it can be done?
2
1719
by: THY | last post by:
Hi, I am having some problem, I declare few variable in a public module and use them in the web application. But after that I found that the variable declared in public variable = application("variable"), and it will be shared by all user ... but I am not very sure about it, anyone know it ? Thanks, Tee
10
3664
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global' variable for the application. Any other code within the application can access it. public - can be shared across the application if instatiated. Does...
3
30044
by: Tor Inge Rislaa | last post by:
How to use a public variable In VB.6.0 I could in the declaration part of a form declare a public variable, then assign a value to the variable, open a new form and address the variable and read it's value in my new form. Form1:
5
8425
by: Neil Steventon | last post by:
Hi, I am new to VB.net , well the objects concept anyway and was wondering if there were any good articles / turorials on when and why you would use certain declarations such as Private test as string public test as string Public Shared as string Protected test as string
7
2063
by: Steve Mauldin | last post by:
I have a public variable that is declared in a public module. This Variable is stored into a Session variable and used to pass data from page to page. I am seeing on my local development box that once the variable is created and loaded with data and stored into the session variable that on the next aspx page, before the first line of the page...
11
1922
by: dgk | last post by:
If I have a class with a public variable, isn't this the same as a private variable with a property? ie, isn't this: Public Class MyTest Public MyVar as String End Class the same as this: Public Class MyTest
0
7479
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7411
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.