life and scope of variables in vb.net?

J

Jason

What's the correct term when talking about the life of a variable in
vb.net web?

in a vb.net codebehind for asp.net, is it possible to define a
variable in one sub, and call another sub and expect to see it? I know
the answer is no, but why is that?

for example:

sub hello()
dim x as string="hi there"
goodbye()
end sub

sub goodbye()
reponse.write(x) ' of course this fails
end goodbye

I know the answer is to use session state or viewstate to store the
variable, but is there another approach, like using a readonly
property from the master class? (sorry if that is not worded
correctly)
 
J

Jim in Arizona

I know the answer is to use session state or viewstate to store the
variable, but is there another approach, like using a readonly
property from the master class? (sorry if that is not worded
correctly)

I've actually run into this problem myself recently. What is strange is that
I could swear I was able to hold a variable somewhere within the page class
and it would not lose its value but this wasn't the case for me this last
time.

My workaround was to put a control at the end of the page (a label control,
for example), set the hidden value to true and store a text value that I
need for later into the control's text property. This has actually worked
pretty well for me. But, if you need to store something else other than
simple text or numbers, then I don't think this would the right option.
 
G

Guest

I've actually run into this problem myself recently. What is strange
is that I could swear I was able to hold a variable somewhere within
the page class and it would not lose its value but this wasn't the
case for me this last time.

ASP.NET has several mechanism for storing values:

ViewState, Session Variables, Static variables, Application variables,
Cache. It depends on what you're going to use it for.
My workaround was to put a control at the end of the page (a label
control, for example), set the hidden value to true and store a text
value that I need for later into the control's text property. This has
actually worked pretty well for me. But, if you need to store
something else other than simple text or numbers, then I don't think
this would the right option.

That's the wrong way - why not use viewstate?
 
G

Guest

What's the correct term when talking about the life of a variable in
vb.net web?

in a vb.net codebehind for asp.net, is it possible to define a
variable in one sub, and call another sub and expect to see it? I know
the answer is no, but why is that?

The page is loaded, created as a class, run, and destroyed each time the
page is requested. Hence unlike an application, it does not run to
maintain state afterwards.
for example:

sub hello()
dim x as string="hi there"
goodbye()
end sub

sub goodbye()
reponse.write(x) ' of course this fails
end goodbye

I know the answer is to use session state or viewstate to store the
variable, but is there another approach, like using a readonly
property from the master class? (sorry if that is not worded
correctly)



In anycase, it depends on what your doing ... However, do you have
something against session variables or viewstate? Session variables no
longer incur the performance penalty like in Classic ASP ... however
they do time out after X seconds. ViewState can bloat the page if you
store too many values and are insecure. You can always write values to
the user's profile (.NET 2.0 has the ability to store user information
on the fly), or you could use a static variable or an application
variable. Those variables could even store a hashtable so that you can
store multiple values within one variable.

Lots of choices, depends on what you want to do.
 
J

Jason

In anycase, it depends on what your doing ... However, do you have
something against session variables or viewstate? Session variables no

I don't, though I find that in some circumstances you can get into
trouble if the client uses the back button. Imagine you are storing
some key in a session variable, you update (a post happens), the
client goes to another page, enters another customer, but the page
fails before setting the new key .. I know good coding can avoid
this..

but for the simple example I listed, my thinking is those two subs
occurred under the same postback, meaing a postback did not occur
between them, so why was I not allowed to set a variable in the the
page_load for example, then reference it in the prerender and then in
some sub called from the prerender? Why require it be passed via
parms? Perhaps static variables (which i never use) is the answer.

And off the topic.. clearly, i'm not a hard core VB developer from my
questions. But I manage to get by building intranet websites that work
and are stable. In a past life I was a COBOL developer and about 5
years ago I jumped straight into .net 1.1, I've never done vb6, I
never built a winform. My asp.net 2.0 is very strong, my vb.net is
strong enough though my OOP is far from great. With so much demand
for .net these days (and it's really hot), do I stand of chance if
walk into an interview and say.. I've never done vb6 or built a
winform and have no desire to do so..? seems most shops are moving
away from that anyway.

Thanks for any help or information.
 
G

Guest

I don't, though I find that in some circumstances you can get into
trouble if the client uses the back button. Imagine you are storing
some key in a session variable, you update (a post happens), the
client goes to another page, enters another customer, but the page
fails before setting the new key .. I know good coding can avoid
this..

Just make sure you handle error conditions correctly. Once you become
familiar with ASP.NET's page lifecycle, this becomes less of an issue.
but for the simple example I listed, my thinking is those two subs
occurred under the same postback, meaing a postback did not occur
between them, so why was I not allowed to set a variable in the the
page_load for example, then reference it in the prerender and then in
some sub called from the prerender? Why require it be passed via
parms? Perhaps static variables (which i never use) is the answer.

Within a same postback you definately can, just declare a class level
variable (i.e. a Private variable).

And off the topic.. clearly, i'm not a hard core VB developer from my
questions. But I manage to get by building intranet websites that work
and are stable. In a past life I was a COBOL developer and about 5
years ago I jumped straight into .net 1.1, I've never done vb6, I
never built a winform. My asp.net 2.0 is very strong, my vb.net is
strong enough though my OOP is far from great. With so much demand
for .net these days (and it's really hot), do I stand of chance if
walk into an interview and say.. I've never done vb6 or built a
winform and have no desire to do so..? seems most shops are moving
away from that anyway.

If you're comfortable with COBOL... there are a couple COBOL
implementations for .NET :)

http://www.netcobol.com/products/windows/netcobol.html

http://www.microfocus.com/products/netexpress/

In fact there are about 100+ .NET language implementations:

http://www.dotnetpowered.com/languages.aspx
 
J

Jason

Within a same postback you definately can, just declare a class level
variable (i.e. a Private variable).

Does anybody have a simple example of this? Where is this defined?
 
C

Cor Ligthert [MVP]

private x as string
sub hello()
x ="hi there"
goodbye()
end sub

sub goodbye()
reponse.write(x) ' of course this fails
end goodbye
Don't mix up the scope of a variable and the lifetime of an object in
VB.Net.

As soon as you send your page (last statement) then everything goes out of
scope, however as long as you are processing the page then the global
variable is intact. Do you need it after a send, then you can save it in a
session or a viewstate (my preference is the session because that is
completely hidden for the user). You can restore it then after the
IsPostBack to x. (x = session.item("x"))

Cor
 
G

Guest

Does anybody have a simple example of this? Where is this defined?

Private MyClassVariable as String = "Hello World"

Public Sub DisplayMessage
DisplayMessageLabel.Text = MyClassVariable
End Sub

Public Sub DisplayMessageAnotherMessage
DisplayMessageAnotherMessage.Text = MyClassVariable
End Sub

I hate to point it out but you wrote:

"My asp.net 2.0 is very strong, my vb.net is
strong enough though my OOP is far from great."

The above is pretty basic ;-)
 
J

Jason

Private MyClassVariable as String = "Hello World"

Public Sub DisplayMessage
DisplayMessageLabel.Text = MyClassVariable
End Sub

Public Sub DisplayMessageAnotherMessage
DisplayMessageAnotherMessage.Text = MyClassVariable
End Sub

Presuming that first line is defined at the very top of the codebehind
outside any of the Subs.

But If I set it at the class level, but what's available to me when I
set it? At the time I'm setting it, do I have access to session
variables? Do I have access to values of controls on the page?
 
G

Guest

But If I set it at the class level, but what's available to me when I
set it? At the time I'm setting it, do I have access to session
variables? Do I have access to values of controls on the page?

When you declare at class level, the variable is created in the constructor
of the page. At this point, you do not have access to session variables,
cookies, etc. yet, but I think you can access controls after
Initializecomponent is called. However, in the constructor you could
execute database queries or any other non-ASP.NET code and populate the
variables.

If you need to populate class variables with ASP.NET data, wait for the
Page_Init or Page_Load fire. By this point of the page lifecycle, the
ASP.NET session variables, cookies, etc should be populated in HTTPContext.

Does this answer your q?
 

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