Visual Basic 2015 Variables
In programming, variables are objects; storage areas used to store information that can later be referenced and manipulated. You can consider a variable as a sort of a container where you store an information that can later be accessed using the variable name.
The variable is determined by its:
Scope - the scope level determines from where the variable can be accessed(e.g. private, public ...).
Data type - can be a string, boolean, integer...
Accessibility - determines which code in other modules can access the variable.
Lifetime - determines how long the variable value will be valid.
Rules for Naming Variables in Visual Basic 2015
The name must start with a letter or an underscore.
The name can contain only letters, numbers, and the underscore character. No punctuation characters, special characters, or spaces are allowed in the name.
Although the name can contain thousands of characters, 32 characters is the recommended maximum number of characters to use.
The name cannot be a reserved word, such as Sub or Double
code example: Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim number1 As Integer number1 = 5 End Sub
Let's explain the code above:
Dim - Short for Dimension. The Dim statement is used for variable declaration. The Dim statement is used at class, module, structure, procedure or block level. number1 - this is the variable, our storage area. After the Dim statement, the name of variable is specified. As Integer - we are indicating Visual Basic 2015 that variable is going to be a number (integer). number1 = 5 - the = sign means assign value. We are telling Visual Basic 2015 to assign a value of 5 to the variable number1. Visual Basic 2015 Data types
Variables in Visual Basic 2015 can be of a different data type. The data type of a variable is important because it specifies the kind of information that can be stored inside a variable. For example, to store numeric information, you need a variable of the type numeric. You can’t store a string in a variable designed to store numeric information. You will need to create a variable of the string type to store a string.
Here is a table of available data types in Visual Basic 2015:
The following example demonstrates the usage of some of the variable data types:
Dim val1 As Byte
Dim val2 As Integer
Dim val3 As Single
Dim val4 As Double
Dim val5 As Date
Dim val6 As Char
Dim content As String
Dim val7 As Boolean
val1 = 1
val2 = 1234567
val3 = 0.11223344556677881
val4 = 0.11223344556677881
val5 = Today
val6 = "a"
content = "Me"
val7 = True
Console.WriteLine(val1)
Console.WriteLine(val2)
Console.WriteLine(val3)
Console.WriteLine(val4)
Console.WriteLine(val5)
Console.WriteLine(val6)
Console.WriteLine(content)
Console.WriteLine(val7)
Here is the output of the code above:
1
1234567
0.11223344556677881
0.11223344556677881
Today
a
Me
True
String Variables
String variables are nothing more than text. Strings in Visual Basic can be used to store a set of characters.
To define a string, you simple type the characters within the double quotes. For example, if the information we want to store in our variable is a name, first we need to set up the variable like this:
Dim FirstName As String
We have started our code with Dim keyword. With this word we declare or tell Visual Basic that you are setting up a variable. With FirstName As String, we are telling Visual Basic that the variable is going to be a string. Once we've declared our variable, we can store a name of person into it using the equal sign:
FirstName = "Jack"
Let's start our new example by adding one button and one textbox on our form.
You should get the screen like this:
Change the properties set in the bottom right corner for these controls as follows:
(Button)
Name: btnName
Text: Test String
(TextBox)
Name: txtName
Text: leave blank
Now, double-click the button. The Code editor should open. This is where you write your code. Add the following code:
Private Sub btnName_Click(sender As Object, e As EventArgs) Handles btnName.Click Dim firstName As String firstName = txtName.Text MessageBox.Show(firstName) End Sub
Run your program to test it out. Click inside a TextBox and add a name. Next, click on the button and you should get something like this:
As you can see from the output above, the MessageBox control displayed the name you've entered in the TextBox. We will be using this control a lot in the
book. For now, just remember that this control displays the string you have passed in the message box.
Working with dates
Sometimes, you need the ability to manipulate the date and time. In Visual Basic 2015, date and time can be formatted using predefined or user-defined formats.
Here are the predefined formats of date and time:
Here is an example. Add five new TextBoxes and one button to your Form. The form should look like this:
Double click the Date-Time button to Open up the editor for your Date - Time button and add the following code to it:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click TextBox1.Text = Format(Now, "General Date”) TextBox2.Text = Format(Now, "Long Date”) TextBox3.Text = Format(Now, "short Date”) TextBox4.Text = Format(Now, "Long Time”) TextBox5.Text = Format(Now, "Short Time”) End Sub
Run your program and click the button. Your form should look like this:
You can also use user-defined formatting functions to define the format you want. This can be useful if you want to display international formats for numbers, dates, and times.
Let's start by creating a new project in our Visual Studio 2015. Before we start coding, we will add seven textboxes and one button on our form.
When you are finished, your form should look like this one below:
Now, double click the button and add the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click TextBox1.Text = Format(Now, "M”) TextBox2.Text = Format(Now, "MM”) TextBox3.Text = Format(Now, "MMM”) TextBox4.Text = Format(Now, "MMMM”) TextBox5.Text = Format(Now, "dd/MM/yyyy”) TextBox6.Text = Format(Now, "MMM,d,yyyy”) TextBox7.Text = Format(Now, "MM/dd/yyyy h:mm:ss tt”) End Sub
Press F5 key on your keyboard to launch your program. When the form loads, click the Date Time user-defined button, and you should get the something
like this:
Comments
You can use comments in your Visual Basic source code in order to document it. With comments, you can remind yourself what a specific portion of the code does and make your code more accessible to other developers.
Since comments are written inside a file that is going to be executed, Visual Basic needs some way to determine that the text you write is a comment, and not a command that needs to be executed. The comment in Visual Basic starts with a single quotation mark (') and everything until the end of the line is part of the comment and ignored by Visual Basic.
It is good programming practice to use comments to make your code clear and understandable:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click MessageBox.Show("Hello World") 'This is comment End Sub Events
Visual Basic 2015 is an event driven language. This means the flow of execution is determined by the external occurrences known as events.
An event is a signal that informs an application that something has occurred.
For example, when a user clicks a button on a form, the form can raise a Click event and invoke a procedure that handles the event. Events also allow separate tasks to communicate. Say, for example, that your application performs a sort task separately from the main application. If a user cancels the sort, your application can send a cancel event instructing the sort process to
stop.
After creating your application’s interface, you can begin writing the Visual Basic code that instructs the controls to respond to the user’s actions. Those actions - such as clicking or double-clicking - are called events.
You can tell a control how to respond to an event by writing an event procedure, which is a set of Visual Basic instructions that are processed only when the event occurs.
Example
Add a new button to a form and double click it. You should see the following code in the code editor:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click End Sub
Here is a breakdown of the syntax: Private Sub - a private subroutine. Button1_Click - the name of the sub. Button1.Click - an event.
In this example, you will write a Click event procedure for the Message
button, which should pop up a message when it is clicked:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click MsgBox("Some message") End Sub
Run your application by pressing F5 on your keyboard and click on the button.The click event shoud be invoked and the message "Some message" should be displayed.
Do you want the book introduction to visual basic 2015? Just donate through the button below and inform me to send u the book.
Comments
Post a Comment