Solved

presistant variables in a VB .net Class

Posted on 2014-02-15
8
394 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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