Solved

presistant variables in a VB .net Class

Posted on 2014-02-15
8
389 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

919 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

21 Experts available now in Live!

Get 1:1 Help Now