Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Dev Hardware ForumsSOFTWAREProgramming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Hardware Forums Sponsor:
  Trader Rating: 0 · #1  
Old October 24th, 2005, 11:21 AM
Evoltix's Avatar
Evoltix Evoltix is offline
Hardware Noobie
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 88 Evoltix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 11 m 46 sec
Reputation Power: 6
VB.NET + Static Variables

I'm having a problem retaining values in static variables. I'll go to debug the program and have a breakpoint at a button. Of course the value in static variables is supposed to remain there after it has left this subroutine but it doesn't. I was just wondering if there is anything I'm doing wrong here or how you retain data in variables after they leave function scope. Thanks.

Code:
Private Sub cmdNewRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNewRecord.Click
        Static start_time As DateTime
        Static stop_time As DateTime
        Dim elapsed_time As TimeSpan

        If Me.cmdNewRecord.Text = "New Record" Then
            Me.txtComments.Text = ""
            start_time = Now
            Me.cmdNewRecord.Text = "Save"
        Else
            stop_time = Now
            elapsed_time = stop_time.Subtract(start_time)
            Me.txtComments.Text = _
                elapsed_time.TotalSeconds.ToString("0.000000")
            Me.cmdNewRecord.Text = "New Record"
        End If
End Sub

Reply With Quote
  Trader Rating: 0 · #2  
Old November 3rd, 2008, 02:37 AM
vgangal vgangal is offline
n00b DevH'er
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 1 vgangal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 16 sec
Reputation Power: 0
static variables in vb.net

Try below code:

Shared start_time As DateTime
Shared stop_time As DateTime

Private Sub cmdNewRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNewRecord.Click

Dim elapsed_time As TimeSpan

If Me.cmdNewRecord.Text = "New Record" Then
Me.txtComments.Text = ""
start_time = Now
Me.cmdNewRecord.Text = "Save"
Else
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
Me.txtComments.Text = _
elapsed_time.TotalSeconds.ToString("0.000000")
Me.cmdNewRecord.Text = "New Record"
End If
End Sub

Reply With Quote
  Trader Rating: 0 · #3  
Old November 3rd, 2008, 10:54 AM
DrStrangluv's Avatar
DrStrangluv DrStrangluv is offline
Contributing User
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 222 DrStrangluv User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)DrStrangluv User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)DrStrangluv User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)DrStrangluv User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)DrStrangluv User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)DrStrangluv User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)DrStrangluv User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)DrStrangluv User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)DrStrangluv User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)DrStrangluv User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)DrStrangluv User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 6 Days 14 h 2 m 20 sec
Reputation Power: 438
Static <> Shared in VB.Net.

Shared is more analogous to C#/C++ static. Static in VB.Net is a little... special. Static members in a function are scoped to the function, in that they are not accessible outside the function. Therefore you debugger shouldn't be able to see them. They should be available (with their previous values) on the next function call, however.

You could of course also declare them outside the function (scoped to the class) as vgangal suggests. However, if you do that they shouldn't be declared as Shared anymore. Just make them private. The advantage to keeping them in the function is that then they are guaranteed to be threadsafe.

You might also want to consider using System.Diagnostics.StopWatch rather than DateTime.Now for this.

Reply With Quote
  Trader Rating: 0 · #4  
Old February 4th, 2009, 01:22 AM
rajdeep rajdeep is offline
n00b DevH'er
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: Feb 2009
Posts: 3 rajdeep User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 50 sec
Reputation Power: 0
I have never read about vb.net that's why i had entered for gain any type of information from there so i think these posts useful for me but this is not enough for me so i am waiting for the other posts..

Reply With Quote
Reply

Viewing: Dev Hardware ForumsSOFTWAREProgramming > VB.NET + Static Variables


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
     
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek