Solved

presistant variables in a VB .net Class

Posted on 2014-02-15
8
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 29

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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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
 
LVL 29

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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