Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

presistant variables in a VB .net Class

Posted on 2014-02-15
8
Medium Priority
?
399 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
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 

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 1500 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

688 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