Professional Web Applications Themes

ASP global variable - ASP.NET General

> How do I hold a global variable in VB.NET? With both hands so you don't spill it....

  1. #1

    Default Re: ASP global variable

    > How do I hold a global variable in VB.NET?

    With both hands so you don't spill it.


    Guenther Guest

  2. #2

    Default Re: ASP global variable

    Hi Joseph,

    Every time your page posts back it reloads the Page class. Take a look at
    the code you have just under your Class declaration:
     

    The New constructor creates a new empty instance of a class. That is why
    your ListItemCollection is empty. You made it empty.

    Of course, unless you re-create the ListItemCollection with every PostBack,
    it won't exist. Leaving off the "New" won't help, as the variable will be
    uninitialized. So, the first thing to do would be to remove the "New" from
    your variable declaration. You need to figure out where and how to
    initialize it. It needs to be declared (Dimmed). But how you initialize it,
    persist it, and add items to it is where your problem is.

    Your question Title indicates that you have some understanding of the
    problem. But be careful of the word "global." That is an word which can mean
    any of a number of things. The term "global" refers to the "scope" of a
    variable. In an ASP.Net application, variables can have a wide variety of
    scopes, and a fairly large number of "global" scopes. A variable can be
    global to a page, to a class in a page, to an Assembly, to a User Session,
    or to the entire web application. What we need to do is identify the proper
    scope of your variable, and find the appropriate mechanism for making it
    such.

    As this variable is page-specific, and you have declared it as a private
    field in the Page class, it is already global to the Page. But what you need
    to do is either rebuild it with every PostBack, or persist it somewhere.
    There are several possibilites for this: ViewState and Session spring to
    mind. ViewState is maintained by passing the data back and forth between
    client and server.On the server it represents a "StateBag" type of
    Collection, similar to a Dictionary object. If you want to store your
    Collection in ViewState, the easiest thing for you to do with it would be to
    store it in an ArrayList or HashTable, which can easily and automatically be
    serialized into ViewState.

    You can also store it in Session State. However, be careful about using
    Session State. It can time out and disappear after a (default) 20-minute
    interval of inactivity on the part of the client. In addition, Session State
    is global to all pages of a user's Session, and you may run into
    complications with other data in Session State, as well as memory issues
    (there is a separate Session memory space for every client on the server, so
    the amount that you store in Session is multiplied by the number of
    concurrent User Sessions).

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    Neither a follower nor a lender be.


    "Joseph George" <com> wrote in message
    news:JQO_a.2562$atl2.webusenet.com... 
    lose 
    not 


    Kevin Guest

  3. #3

    Default Re: ASP global variable

    Thanks Kevin. ViewState works fine!
    Joseph

    "Kevin Spencer" <com> wrote in message
    news:phx.gbl... 
    >
    > The New constructor creates a new empty instance of a class. That is why
    > your ListItemCollection is empty. You made it empty.
    >
    > Of course, unless you re-create the ListItemCollection with every[/ref]
    PostBack, 
    it, 
    mean 
    proper 
    need 
    to 
    be 
    State 
    so [/ref]
    form [/ref]
    to 
    > lose 
    > not 
    >
    >[/ref]



    Joseph Guest

  4. #4

    Default Example: ViewState & ArrayList

    Here is a small example: The WebForm has a TexBox1, a ListBox1 and
    AddButton.



    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load


    If IsNothing(ViewState("list")) Then

    ViewState("list") = myArray

    End If

    End Sub

    Private Sub AddButton_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles AddButton.Click

    myArray = ViewState("list")

    myArray.Add(TextBox1.Text)

    ViewState("myAL") = myArray

    ListBox1.DataSource = myArray

    ListBox1.DataBind()

    End Sub



    Joseph Guest

Similar Threads

  1. global variable
    By Ben in forum PHP Development
    Replies: 2
    Last Post: May 19th, 10:00 PM
  2. global variable problem
    By david taylor in forum Macromedia Director Basics
    Replies: 1
    Last Post: November 14th, 01:59 PM
  3. Global variable issues
    By mrsr84 in forum Macromedia Director Lingo
    Replies: 5
    Last Post: October 17th, 02:21 PM
  4. Global variable and tempo
    By Ned in forum Macromedia Director Lingo
    Replies: 0
    Last Post: September 23rd, 08:54 PM
  5. Replies: 0
    Last Post: July 17th, 09:23 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139