Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage
Go Back  Xtreme Visual Basic Talk > > > Declaring VB variables with scope that covers the entire webpage


Reply
 
Thread Tools Display Modes
  #1  
Old 05-26-2010, 11:59 AM
garethmann garethmann is offline
Centurion
 
Join Date: Jul 2003
Location: Fort Lauderdale, Florida
Posts: 105
Default Declaring VB variables with scope that covers the entire webpage


Hi,

I am new to Visual Studio.NET but very familiar with the old ASP (using DreamWeaver).

I am unfamiliar with this system of having seperate files for VB code and HTML, in my Visual Studio project I have a Default.aspx file and a Default.aspx.vb file.

Now I want to declare a variable that will be accesible to all my functions on the Default.aspx.vb file. I.e. if a user clicks on one button and it launches a Protected Sub called Button1_Click, I want that button to be able to read or write to that variable and then if a user clicks on another button, I want it to likewise be able to read and write to that variable. So where should these variables be dimmed?

At the top of the aspx.vb file? Before or after the line that says "Partial Class _Default"?

Or at the top of the aspx file? (Within <% %> brackets?)
__________________
"Any sufficiently advanced technology is indistinguishable from magic."
Arthur C. Clarke
Reply With Quote
  #2  
Old 05-29-2010, 12:18 AM
nyeoman nyeoman is offline
Freshman
 
Join Date: Sep 2005
Posts: 40
Default

You'll want to declare it within the class, but outside (above) any subroutines to make it 'global' to the page.
Reply With Quote
  #3  
Old 06-18-2010, 10:51 AM
SteveNutt SteveNutt is offline
Freshman
 
Join Date: Jun 2005
Location: Thailand
Posts: 35
Default

This is what Session state is for. Use something like Session("MyVariable") = 1234 to store the integer 1234. If you wanted to place that value into a textbox as a string, you would do something like
MyTextBox.Text = CStr(Session("MyVariable"))
__________________
IP Alarms - built with VB and ASP.net with a SQL Server back end.
Alarm Monitoring - built with ASP.Net web services.
Reply With Quote
  #4  
Old 06-19-2010, 10:06 PM
Qua's Avatar
QuaDeclaring VB variables with scope that covers the entire webpage Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

ASP.NET Web Forms is such a mess...

You say that you want the scope to "cover the entire webpage". Do you want the variable to be shared among all users of the page? Or should it be shared among all the methods of the page for a single user?

To share a variable between methods you can take both of the approaches suggested by respectively SteveNutt and nyeoman. Nyeoman's approach depends on ASP.NET Web Forms to save the state of the variable between calls, whereas you take care of this manually in Steve's suggestion but in return the variable will be scoped such that it can be accessed from any page on the server.

To share a variable between all sessions / users of the website you could declare the variable shared/static. You should be extremely careful though. Static variables are not ensured to be thread safe, and in most cases they won't be. So you will have to handle this issues yourself.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage
 
Declaring VB variables with scope that covers the entire webpage
Declaring VB variables with scope that covers the entire webpage
 
-->