Check if its program's first run-VBForums
Results 1 to 9 of 9

Thread: Check if its program's first run

  1. #1

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    562

    Check if its program's first run

    A simple but useful code to have. This checks if the user has run the application before.
    Instructions
    1. Add a setting to My.Settings named isFirstRun by going to Project -> Properties -> Settings Tab. Also make this setting a Boolean and set its value to True
    VB.NET Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         If My.Settings.isFirstRun = True Then
    3.             'CODE HERE - THIS IS THE FIRST RUN
    4.             My.Settings.isFirstRun = False
    5.             My.Settings.Save()
    6.         ElseIf My.Settings.isFirstRun = False Then
    7.             'CODE HERE - THIS IS NOT THE FIRST RUN
    8.         End If
    9.     End Sub

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Check if its program's first run

    Why are you checking against True and False? Just remove the = True and change the ElseIf to Else.

  3. #3

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    562

    Re: Check if its program's first run

    Why would i do that. It does exactly the same thing.

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    3,817

    Re: Check if its program's first run

    It doesn't really matter, as this:
    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If My.Settings.isFirstRun Then
                My.Settings.isFirstRun = False
                My.Settings.Save()
                'CODE HERE - THIS IS THE FIRST RUN
            Else
                'CODE HERE - THIS IS NOT THE FIRST RUN
            End If
        End Sub
    will run 1 millisecond faster than this:
    VB.NET Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         If My.Settings.isFirstRun = True Then
    3.             'CODE HERE - THIS IS THE FIRST RUN
    4.             My.Settings.isFirstRun = False
    5.             My.Settings.Save()
    6.         ElseIf My.Settings.isFirstRun = False Then
    7.             'CODE HERE - THIS IS NOT THE FIRST RUN
    8.         End If
    9.     End Sub

  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Check if its program's first run

    It's less code and less cluttered. That's all.

  6. #6

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    562

    Re: Check if its program's first run

    Tbh im not too fussed about 1millisecond and it doesn't look cluttered to me.

  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Check if its program's first run

    It doesn't matter much in this case, because looking up the value of a boolean property takes negligible time. But what if your property needs to do a huge amount of processing before it can return its value? In that case, you would do that processing twice, while you don't need to do it the second time (as you already have your answer... If it's not True then it's False).

    It's just good practice to use Else whenever you can.

  8. #8
    New Member
    Join Date
    Aug 2010
    Posts
    8

    Re: Check if its program's first run

    Simple, Quick, Just what I was looking for. thanks!!

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    4,105

    Re: Check if its program's first run

    Quote Originally Posted by Emcrank View Post
    Tbh im not too fussed about 1millisecond and it doesn't look cluttered to me.
    It's noise. Noise = bad in my opinion. Why need an extra level of comparison????

    I was always taught never compare Boolean properties to True or False in If statements or any other conditional. They're already booleans. It ultimately expands to pseudocode like this:

    Code:
    If (chkShowPreview.Checked = True) = True Then

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.