Global Variables on a VB.NET Web Page

F

Fred Nelson

Hi:

I'm a relative newby so hopefully this is a simple question!

I have found that I can create global variables easily on a web page by
placing the dim statement before the first "private sub" in a program:

dim mydata as (whatever)

Public Sub Page_Load(etc..)

End Sub

This makes information that I place in this global variable available to
every click event on the page that I create just as it does in regular
VB.NET apps.

I'm curious to know if this is not a "best practice" - is info left in
memory if the user clicks away - or is this a "great practice". I haven't
seen any demos that use this so I'm curious if there is a down side

Thanks very much!

Fred
 
K

Kevin Spencer

Hi Fred,

You need to start thinking in terms of objects and classes here. An ASP.Net
Page is a class definition, which, when run, generates an object that does
the work of streaming content to the browser. this is the biggest difference
between "classic" (read "procedural") ASP and ASP.Net. The VB/VBScript Dim
statement is a statement that declares a variable. However, what you are
referring to as "Global Variables" are actually what is called in OO terms a
"field" of a class. A class is an encapsulation of data and functionality
which has members that are accessible at various levels, including Public,
Private, Protected, to name the most common ones. These terms indicate what
classes (external to the present class) can access these fields, properties,
and methods. Microsoft, in its' wisdom, has made it easier for VB developers
to migrate by automatically translating the "Dim" syntax, when used at class
level, to "Private" without even telling you. As you can see, I'm not all
that thrilled about it, as it doesn't help the VB developer to move forward,
but instead makes it seem somewhat confusing. In other words, "Dim" is not
meaningful at the class level. It isn't an accessibility modifier, but the
compiler will fix it for you without you know what it is doing or why. As a
result, we have a whole slew of confused VB developers who are inching
forward painfully to an understanding of object-oriented development.

So, I would say that using a Dim statement at class level (it is necessary
to use it at the Sub/Function/Method level) is not a "Best Practice" as it
is misleading. It is better to get used to thinking in terms of classes and
objects. Once you can get your head wrapped around the object-oriented
paradigm, it becomes incredibly intuitive.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
A

Alvin Bruney [MVP]

The reason you haven't seen it because it's not a good practice. Global
variables come with their gammot of problems which is best avoided. If you
are going to need a global variable, use a static variable inside the class
 
R

Raterus

I would put a Private/Public/Protected in front of it instead of Dim. (I
honestly don't even know what "Dim" declares as) But anyhow, that is
perfectly fine way of doing things, but they aren't really Global variables,
but class level variables.

Public - would be accessible from anywhere (closest thing to global)
Private - only accessible from within that class itself
Protected - accessible in this class, and any class that inherits from it.

There is also Friend, which I believe allows other nonrelated classes in the
same namespace to access it. Dunno, never used it. Anyway this is all
object orientated programming. Teaching you that is way beyond the scope of
this newsgroup, but there are many good resources out there on it, and the
theory behind programming in it.

Michael
 
R

Raterus

<raises hand as another confused VB developer about the "Dim" statement at
the class level>

Now I know...thanks :)
 
F

Fred Nelson

Wow!

Thank you all VERY much - your responses answer many questions for me!

Fred
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top