Tuesday, February 7, 2017

Importance and Use of Loop, Len With Mid Functions ..Visual Basic

Working with Loop and Mid Function
Mid function is used in many purposes. Most of the functions are used of this function. Finding values or finding letters and making comparison with one another, it is very important. Usually this function is used with loop.

Loop is type of function where we can run the program or run the task for how many times we want to run the task. For loop we use many types. Some of them are as follows.
  1. For Next
  2. Do while
  3. Do until
  4. While wend
  5. Nexted loop


Mostly we use For Next for making loop.

How, For Next Runs?
It needs two values to run the task. Firstly needs starting value, second needs 
ending value ....................


and thirdly needs incremental value which is optional. If we don’t define incremental value it increases with 1 value in every step.

For example: if we need to get addition value means sum value of 1 to 10.Then we construct as
-----------------------------------
For x = 1 to 10
Sum = x + sum
Next
--------------------------------------
Now here, x and sum are variables. It runs 10 times.
How it process, initially value of variables are 0

In first step: value of x is 1
Adding sum = 0+1 = 1

In second step: value of x is 2
Adding sum = 1+2 = 3

In third step: value of x is 3
Adding sum = 3+ 3 = 6

This steps runs till x value = 10

Then the result will be 55 at the end.

Looking, Mid function with loop is quite tough and interesting. Mid functions always used for working or retrieving the value related to strings. Mid function needs 3 values, one is of string; second one is initial value and length value.

Syntax: Mid(string, Starting no, length)

Example: I have made a code to find out “@” from the provided string as variables. It needs one textbox and button in a form and place the code to the buttons on click.
………………………………………………………
Private Sub Command1_Click ()
For x = 1 To Len (Text1.Text)
value1 = Mid (Text1.Text, x, 1)
If value1 = "@" Then
    MsgBox "@ found"
    Exit Sub
End If
Next x
MsgBox "not found"
End Sub
………………………………………………………..
Now the program finds the “@” in the text defined. If you try this you can easily find the answer. But here is a knowledge how it finds the “@” letter among many letters. It is because use of Loop and Mid function. It can be done by using Right or Left Function you can try yourself. There are other techniques also to find and use of Mid. It is mostly used if you try to input invalid characters which are not suitable for the defined data. It depends on programmer to handle it whether to input those data or nor to message the invalid characters you found on the string.

You can now try using it and can make your valid information stronger easily. Thanks using and reading the boring chapter of Visual Basic. Be in touch. Comment if you want to know more info or want to get more examples.

Narayan Aryal
Butwal
Nepal 

No comments:

Post a Comment