Delcare a Public Variable.

P

Phillip Vong

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.querystring("MyVar")
End Public Variable

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

Thanks!
Phil
 
S

Scott M.

Just put:

Dim myVar as string

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

myVar = Request.querystring("MyVar")

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

Guest

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
 
G

Guest

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.QueryString("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.EventArgs) Handles Me.Load

If Not IsPostBack Then
DisplaySomeInformationBasedOnMyVarValue(MyVar)
End If

end sub
 
S

Scott M.

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

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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.
Yes, but that is not the case here, the value is simply a queryString value.
 
S

Scott M.

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 said:
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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Scott said:
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.
 
S

Scott M.

Sorry, got you and Milosz confused. My first reply was to him. Letting him
know that for this situation, his solution was over the top.


Göran Andersson said:
Scott said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top