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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If My.Settings.isFirstRun = True Then
'CODE HERE - THIS IS THE FIRST RUN
My.Settings.isFirstRun = False
My.Settings.Save()
ElseIf My.Settings.isFirstRun = False Then
'CODE HERE - THIS IS NOT THE FIRST RUN
End If
End Sub
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.
Re: Check if its program's first run
Why would i do that. It does exactly the same thing.
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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If My.Settings.isFirstRun = True Then
'CODE HERE - THIS IS THE FIRST RUN
My.Settings.isFirstRun = False
My.Settings.Save()
ElseIf My.Settings.isFirstRun = False Then
'CODE HERE - THIS IS NOT THE FIRST RUN
End If
End Sub
Re: Check if its program's first run
It's less code and less cluttered. That's all.
Re: Check if its program's first run
Tbh im not too fussed about 1millisecond and it doesn't look cluttered to me.
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.
Re: Check if its program's first run
Simple, Quick, Just what I was looking for. thanks!!
Re: Check if its program's first run
Quote:
Originally Posted by
Emcrank
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