Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 542685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:28:22+00:00 2026-05-13T10:28:22+00:00

In Visual Basic 2008, there’s two different ways that I know of to accomplish

  • 0

In Visual Basic 2008, there’s two different ways that I know of to accomplish the same thing:

The Dim on the member level:

Dim counter1 as integer = 0
Dim counter2 as integer = 180
Public Sub SampleSub1()
   Counter1 += 1 : If (Counter1 > 14) Then Counter1 = 0
   Counter2 += 1 : If (Counter2 > 240) Then Counter2 = 0
End Sub

Then there is the static on the procedure level:

Public Sub SampleSub2()
   Static Dim counter1 as integer = 0
   Static Dim counter2 as integer = 180
   Counter1 += 1 : If (Counter1 > 14) Then Counter1 = 0
   Counter2 += 1 : If (Counter2 > 240) Then Counter2 = 0
End Sub

I’m running a loop about 8 million times through this in about 7 seconds (with more data being processed), and using the static method on the counters actually takes about 500ms longer. Does the static method provide better memory management? Why is it slower?

Additionally, I declare my objects that are re-used with a dim on the member level or at least outside for loops, like:

Dim SampleObject as SampleClass
Public Sub SampleSub3()
   SampleObject = TheQueue.Dequeue()
End Sub

Should I use the Static method (which seems to be slower) on situations like this, or the dim on member level method that I already use?

I’ve reproduced the problem with this code in a test app:

Public Class Form1
Dim EndLoop As Integer = 50000000
Dim stopwatch1 As New Stopwatch

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    stopwatch1.Reset()
    stopwatch1.Start()

    For cnt As Integer = 1 To EndLoop
        test1()
    Next
    stopwatch1.Stop()

    Label1.Text = "Loop1: " & stopwatch1.ElapsedMilliseconds

    stopwatch1.Reset()
    stopwatch1.Start()
    For cnt As Integer = 1 To EndLoop
        test2()
    Next
    stopwatch1.Stop()

    Label2.Text = "Loop2: " & stopwatch1.ElapsedMilliseconds
End Sub
End Class

Public Module TestModule
Dim counter1 As Integer = 0
Dim counter2 As Integer = 180
Public Sub test1()
    counter1 += 1 : If (counter1 > 14) Then counter1 = 0
    counter2 += 1 : If (counter2 > 240) Then counter2 = 0
    COW1(counter1, counter2)
End Sub

Public Sub test2()
    Static counter3 As Integer = 0
    Static counter4 As Integer = 180
    counter3 += 1 : If (counter3 > 14) Then counter3 = 0
    counter4 += 1 : If (counter4 > 240) Then counter4 = 0
    COW1(counter3, counter4)
End Sub


Public Sub COW1(ByVal counter1 As Integer, ByVal counter2 As Integer)

End Sub
End Module

The slow down with the usage of the static variables does not occur unless I’m calling a sub from another module like the above example.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:28:22+00:00Added an answer on May 13, 2026 at 10:28 am

    Static local variables are a weird feature of VB.Net. When you compile them, they just get represented by a class level static variable behind the scenes.

    However, if you’re initializing and declaring a static local variable at the same time, what actually happens is the compiler sticks a Monitor class around it, to ensure that it can’t be initialized by multiple threads at the same time.

    It’s the overhead of this Monitor that you’re likely seeing. Try removing the initialization from the declarative statement and see if you notice a performance improvement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I implemented a fonction in Visual Basic 2008 that takes the content of all
I want a code that when applied to text in Visual Basic 2008 it
Language Visual Basic 6. I want to know if there is an open source
I have Visual Studio 2008 with Resharper, does anyone know if there is any
hello i'm using Visual Basic 2008 Express is there another webbrowsers such as gekoWebBrowser
I there I have this scenario: a) Notebook :Windows Server 2008 R1, Visual basic
I'm trying to determine if there's a way in Visual Basic 2008 (Express edition
Im using Visual Basic 2008 Express Is there a way to disable mouse click
I am using Visual Studio 2008. I have created a visual basic form that
I'm looking to create a Visual Studio 2008 template that will create a basic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.