<-Back

VBA Component Details

Available Components

CInt(), CDbl(), etc
Inline Declaration
Single-Line If
Functions and Subs and Modularity!

CInt(), CDbl(), etc

What they do:

CInt() is a command used to try and convert whichever argument, i.e. the item placed between the two parenthesis, into an integer.
This is very useful for creating safer programs. Consider the code snippet below:

Dim myInt as Integer myInt = someTextBox.Text

What happens if someone inputs "butts lol" into the textbox?
Typically, the program would crash. Now let's look at the safe version of this same code:

Dim myInt as Integer myInt = CInt(someTextBox.Text)

What happens to this code if someone inputs "LMFAO poooop" into the textbox?
Well, nothing exciting. The value 0 would be assigned to myInt.
There are other built-in safe conversions as well, such as CDbl() for doubles, CDec for decimals, etc.
For a full list, see the MSDN (MicroSoft Developer Network) reference at:
Type Conversion Functions (Visual Basic)

Inline Declaration

What it is:

Inline declaration allows users to both declare multiple variables all in the same line, and assign them a value at creation.
This can turn the following code:

Dim myIntOne as Integer Dim myIntTwo as Integer myIntOne = 1 myIntTwo = 2

Into a single line, such as:

Dim myIntOne as Integer = 1, myIntTwo as Integer = 2

Like (almost) all things in code, this is a trade. The declaration is more compact, but may be less legible for new coders.
Since either is correct, use whichever makes more sense to you. That said, it is far more common to see code more similar to the second snippet in industry.

Single-Line If

What it is:

A single-line if statement allows for compact, simple branching.
This allows us to really crunch down on any if/then/else statement that is only running one line of code per condition.
For example:

If studentGrade >= PASS_CUTTOFF Then MsgBox("The sudent passed!") Else MsgBox("The student diud not pass.") End If

Can be smashed down to:

If CInt(Me.grdTxt.Text) >= PASS_CUTTOFF Then MsgBox("The student passed!") Else MsgBox("The student did not pass.")

Which does the exact same thing.

Functions and Subs and Modularity!

What they are:

Functions and Subroutines (Subs) are used to make code more modular. This seems like more buzzword than something useful,
but hopefully the following examples can demonstrate their usefulness.
The big difference between Functions and Subs is that a Function MUST RETURN A VALUE. What does this mean?
Let's look at the snippet below and see if we can make sense of them.

Public Function addTwo(num1 as Integer, num2 as Integer) as Integer Return num1 + num2 End Function Public Sub addDos(num1 as Integer, num2 as Integer) MsgBox(num1 + num2) End Sub 'assume the following is called in some button-click function MsgBox(addTwo(1,2)) addDos(1,2)

The calls at the bottom do the same thing. So what is the benefit of using a Function as opposed to a Sub?
The idea is really modularity. What if I want to add two values without popping a message box? The addDos() function doesn't help there.
Because the addTwo function allows us to chain, we can also add use the two in unison. What if we want to add 1 through 4? Well, we can just do this:

addDos(addTwo(1,2),addTwo(3,4))

Of course, these example are trivial... since we can just use the "+" operation. But hopefully they illustrate the idea of when to use a function as opposed to a sub.
Modularity and Code Reuse

How does this tie into modularity and code reuse? Think of the following example, related to lab 11. We could do the starsBtn_click like this:

Private Sub starsBtn_Click(sender As Object, e As EventArgs) Handles starsBtn.Click If butterflyRB.Checked Then starIndex = 0 ElseIf crabRB.Checked Then starIndex = 1 ElseIf horseheadRB.Checked Then starIndex = 2 ElseIf monoRB.Checked Then starIndex = 3 ElseIf sombreroRB.Checked Then starIndex = 4 Else starIndex = -1 End If If starIndex > 0 Then Me.starsPcBx.Image = starImages(starIndex) Else starIndex = 0 Me.starsPcBx.Image = starImages(starIndex) End If End Sub

But instead, for legibility (and modularity!) it is done like this:

Private Sub starsBtn_Click(sender As Object, e As EventArgs) Handles starsBtn.Click starIndex = getChecked() If starIndex > 0 Then ShowPicture() Else starIndex = 0 ShowPicture() End If End Sub Private Sub ShowPicture() Me.starsPcBx.Image = starImages(starIndex) End Sub Private Function getChecked() As Integer If butterflyRB.Checked Then Return 0 If crabRB.Checked Then Return 1 If horseheadRB.Checked Then Return 2 If monoRB.Checked Then Return 3 If sombreroRB.Checked Then Return 4 Return -1 End Function

Even ignoring the differences in return/if structure, the second block of code is far more useful if we were to ever use this code for anything else.