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, 12:21 PM
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: 5
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, 03: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, 11:54 AM
DrStrangluv's Avatar
DrStrangluv DrStrangluv is offline
Contributing User
Dev Hardware Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 221 DrStrangluv User rank is Major (30000 - 40000 Reputation Level)DrStrangluv User rank is Major (30000 - 40000 Reputation Level)DrStrangluv User rank is Major (30000 - 40000 Reputation Level)DrStrangluv User rank is Major (30000 - 40000 Reputation Level)DrStrangluv User rank is Major (30000 - 40000 Reputation Level)DrStrangluv User rank is Major (30000 - 40000 Reputation Level)DrStrangluv User rank is Major (30000 - 40000 Reputation Level)DrStrangluv User rank is Major (30000 - 40000 Reputation Level)DrStrangluv User rank is Major (30000 - 40000 Reputation Level)DrStrangluv User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 6 Days 13 h 49 m 4 sec
Reputation Power: 389
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, 02: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!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

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




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