Solved

presistant variables in a VB .net Class

Posted on 2014-02-15
8
386 Views
Last Modified: 2014-02-17
New to .Net coming from Visual Basic a dozen years ago.  Using MS Visual Web Developer 2010 Express for some simple web server pages.

Trying to figure out variable persistence.  Have a timer control on an aspx page.  Want a variable to be preserved between timer tick events.  My iCounter variable should be incrementing between each tick (each 5 seconds) but it's not.   Can someone help me understand how to declare the iCounter variable so it is preserved between ticks.   Here's the VB code:

Partial Class Test
    Inherits System.Web.UI.Page
    Protected iCounter As Integer


    Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
        iCounter = iCounter + 1
        Label1.Text = iCounter
    End Sub
End Class


Here's the aspx page:

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Test.aspx.vb" Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   
    </div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:Timer ID="Timer1" runat="server" Interval="5000">
    </asp:Timer>
    </form>
</body>
</html>
0
Comment
Question by:pwashburn1224
8 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 39861890
Where are you setting the initial value for your ICounter?

An alternative to your approach would be to use datetime


	Label1.Text = DateTime.Now.ToString()

Open in new window


And it will increment by 5 seconds that you set on your markup.


Besides, .Net has a built-in timer control.

Why not use it?

Check this out:

http://msdn.microsoft.com/en-us/library/system.web.ui.timer.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

or the ASP.Net Ajax control:

http://www.asp.net/AJAX/Documentation/Live/tutorials/IntroToTimerControl.aspx

If this is not what you are after, please post back
0
 

Author Comment

by:pwashburn1224
ID: 39861899
It is a .Net Timer.

I'm able to get it working if I store the iCounter contents in a Session variable.  

This works

Partial Class Test
    Inherits System.Web.UI.Page


    Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
        Session("iCounter") = Session("iCounter") + 1
        Label1.Text = Session("iCounter")
    End Sub
End Class


Thought there was a more elegant way to do that in .Net rather than using a Session variable.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39861905
Can you create a private variable in the form rather than being declared in the event handler

eg Private iCounter as integer

This should persist while the form is on screen
0
 

Author Comment

by:pwashburn1224
ID: 39861925
Remember, I'm still new to this .Net stuff from Visual Basic from a dozen years ago.  So please forgive my beginners level understanding of web pages.


I think that the Private iCounter isn't working because the Timer.Tick event causes a PostBack of the entire page.   So the entire page is being rewritten every 5 seconds so all variables in the Form are being reinitialized.  

Using Session variables seems to overcome that.  But the page apparently is going through a PostBack every time the Timer.Tick event fires.  

What I'm ultimately trying to do is to display changing pages in an iFrame.  Every 15 seconds I check to see if the page that should be in the iFrame has changed.  If so, change the "src" attribute in the iFrame and redisplay the page.  But it looks like the page is reposted/recreated every time the Timer.Tick fires.  I see the page flashing/being rewritten every 15 seconds even if there's not a new page to be displayed in the iFrame.  

I'll start a new question for that one.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 39861955
Have you tried what I posted?

I think that there will just be a slight change to your markup.

Now, you just need to add update panel to yours:

        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:Timer runat="server" id="Timer1" interval="5000"  />
        <asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger controlid="Timer1" eventname="Tick" />
            </Triggers>
            <ContentTemplate>
                <asp:Label runat="server" id="Label1" />
            </ContentTemplate>
        </asp:UpdatePanel>

Open in new window

0
 

Author Comment

by:pwashburn1224
ID: 39862175
Let me look at that tomorrow.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39862517
The way you have it does give you a persistant variable in that class instance.  Note however it ought to be given an initial value (for clarity - I believe it is set to zero by default by the compiler in .net).
As you are not getting what you wanted it does imply the class instance is being destroyed and recreated (I suspect you guess about postback is correct).
0
 

Author Comment

by:pwashburn1224
ID: 39865333
Thank Sammy!

Although we wondered off the original topic of this question, your suggestion of the .Net UpdatePanel helped get me much further along on the ultimate goal that triggered the question.   Now I just have to figure out how to update the contents of the iFrame that is outside of the UpdatePanel when it needs to change.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now