Showing posts with label Introduction to Visual Basic. Show all posts
Showing posts with label Introduction to Visual Basic. Show all posts

Saturday, August 29, 2020

String Functions in Visual Basic:

String functions:

LCase() function:

Return a string that has been passed to it as an augment convert it to lowercase string

LCase(String)

e.g LCase(“PRATIK”) returns “pratik”

UCase() function:

Return a string that have been passed to it as an argument converted to uppercase string

UCase(string)

e.g UCase(“pratik”) returns “PRATIK”

Len( string):

Gives the length of the string which is passed to it as an argument.

e.g Len(“Pratik”) gives 6

LTRIM( string)

Return the string without any leading spaces

e.g Print LTRIM(“  BSC  ”) gives “BSC  ”

RTRIM(string)

Return the string without any trailing spaces 

e.g Print RTRIM(“  BSC  ”) gives “  BSC”

TRIM(string)

Return string without any leading or trailing spaces.

e.g Print TRIM(“  BSC  ”) gives “BSC”

LEFT(String, length)

Return the specified number of characters from the left side of this string.

e.g Print LEFT(“Bajaj College of Science”, 5) gives “Bajaj”

RIGHT(String, length)

Return the specified number of character from the right side of the screen

e.g Print RIGHT (“Bajaj College of Science”, 7) gives “Science”

MID(String, Start,Length)

Return the specified number of characters from the specified position of the string

e.g print MID (“Bajaj College of Science”, 7, 7) gives “College”

CHR(charcode)

Return the character associated with the specified ASCII number.

e.g Print CHR(65) return “A”

ASC(String)

Return the ASCII number for the first letter in the string.

e.g Print ASC(“A”) return 65 

InStr() Function

It is used to find the occurrence of the one string inside other string along with the position of it.

Return a Long value specifying the position of the first occurrence of the one string in another.

Syntax:

InStr(String1, String2)

e,g print Instr(“Pratik” , “a”)  return 2 which is the position of string “a” in the string  Pratik.

StrComp() Function:

Return a(Variant) integer indicating the result of the string comparison.

e.g  StrComp(String1, String2)

Function returns an integer value depending on the result of the comparison as follows

String1 is less than string2

-1

String 1 is equal to string 2

0

String 1 greater than string 2

1

Sting 1 or string to is Null

Null

While comparing the two string the ASCII values of the character in it are compared

e.g      StrComp(“Pratik”, “pratik”) gives  -1

StrComp(“Pratik”, “Pratik”) gives  0

StrComp(“Prashant”, “Pratik”) gives 1  


Special Functions in Visual Basic

InputBox() Function: It displays a prompt in a dialogue box, wait for the user to input text or click button,  and returns a string containing the contents of the text box

Syntax:

Inputbox( prompt[,  title] [,  default] [,  xpos] [, ypos], [helpfile, context])

The input box function syntax has this named argument:

Part

 Description 

Prompt

String expression displayed as the message in dialogue box

Title

Optional.  String expression displayed in the title bar of the dialogue box.  If you omit  title,  the application name is place in title bar

Default

Optional.  String expression displayed in the textbox as the default response.  If no other input is provided

Xpos

The horizontal distance of the left edge of the dialogue box from the left edge of the screen

Ypos

Optional.The vertical distance of the upper edge of the dialogue box from the top of the screen. 

Helpfile

Optional. String expression that identifies the help file to use to provide context sensitive help for the dialogue box. 

Context

Optional.  Expression that is the help contact number assigned to the appropriate help topic by the help author. If context is provided  , help file must also be provided

Private Sub Command1_Click()

Dim x as integer

x = Inputbox(“ Enter in number”,” Number input”, “10”,20,30)

Print x

End sub

When a button is clicked,  the input box is displayed as follows

Inputbox

Msgbox() Function: 

Displays a message in dialogue box, Wait for user to click a button, and return an integer indicating which button the user click.

Syntax:

Msgbox(prompt, [ buttons], [ title] [, helpfile, context])

The details of the augments passed to the Msgbox function are as follows.

Part

 Description

Prompt

Required. String expression displayed as the message in the dialogue box

Buttons

Optional.  Numeric expression that is the sum of values specifying the number and types buttons to display,  the Icon style to use, the identify of the default button,  and the modality of the message box. If omitted the default value of the button 0.

Title

Optional.  String expression displayed in the title bar of the dialogue box.  If you omit title the application name is place in the title bar.

Helpfile

Optional.  String expression that identifies a help file to use to provide context sensitive help for the dialogue box.  If help file is provided context must also be provided

Context

Optional.  numeric expression that is to help contact number assigned to the appropriate help topic by the help author.  if context is provided,  helpfile must also be provided 

The Msgbox() function return a value depending upon the button clicked,

Constant

 Value

 Description

VbOK

1

Ok

VbCancel

2

Cancel

VbAbort

3

Abort

VbRetry

4

Retry

VbIgnore

5

Ignore

VbYes

6

Yes

VbNo

7

No

The message box is always displayed at the centre of the screen.

Private Sub Command1_Click()

Dim x as integer

x = Msgbox(“ Are you sure”,  vbYesNoCancel+vbQuestion, ” confirmation”)

End sub

Msgbox


Functions in Visual Basic:

The function is capable of returning a value to the calling program what is procedure can't do

Steps to create a function:

Tool -> Add Procedure -> Name ->Type(Function) -> Scope (Private/Public)

Syntax for the function definition is:

Public Function  Function_name()  as return data_type

……….

Statements

………..

End function

Public Function Fact (num as integer) As Long

Dim f, i as Long

f=1

For i = 1 to num

f=f*i

Next i

Fact=f

End Function

Private Sub Command1_Click()

Dim x as Long

x=Fact (5)

Msgbox  x

End Sub

A function can return a value in the following format

function_name =return_value

Calling a function:

Usually, you can call a function procedure you have written yourself the same way call intrinsic Visual Basic function like Abs that is by using its name in an expression

MsgboxFact(4)

It is also possible to call a function just like you would call a sub procedure. Following statement both call the same function.

Call Fact(4)

Or 

Fact 6

When you call a function this way, Visual Basic throw away the return value


Monday, August 17, 2020

Procedures in Visual Basic:

Procedures in Visual Basic:

Procedure is a sequence of statements executed as a unit.  Procedure normally simplifies programming by breaking the program into smaller logic components. Procedures are useful for condense repeating or sharing the task.

Benefits of programming with procedure

  1. The repetition of the code is avoided.
  2. If program size is reduced, which consume less memory and result in faster program execution?
  3. Maintenance of program (applying changes) become is   easier.
  4. The debugging of the program( pressing and eliminating errors) also become easier
  5. The program become easy to understand

Types of procedure

  1.  General procedure
  2.  Event procedure

General procedure: A sub procedure is a block of code which is not associated with an event and is to be explicitly called. By breaking the code into sub procedures, it becomes easier to find or modify the code in your application.

Event procedure: In an event procedure the code gets automatically trigger when an object in Visual Basic recognizes that any event has occurred like a click or a double click.  The name of the procedure established and association between the object and the code, event procedure is attach to forms and controls

All event procedures used the same general syntax.

Private sub  controlname_ eventname( arguments)

            Statements

End sub

Creating a General procedure

You can create general procedures in two ways.  One way by manually going to the general declaration section and typing through to the menu

To create a new general procedure manually type a procedure heading in the code window and press enter. The procedure heading can be as simple as sub or function followed by name

e.g. 

Sub Show()

Function Fact()

When you press enter Visual Basic responds by completing the template for the procedure.

Sub show()

End sub

Function Fact()

End  Function

Then you can type code to complete procedure

Sub Show()

Msgbox “hello”

End Sub

Creating a General procedure through the menu:

To add a procedure with the help of menu from the tool menu to display the following add procedure dialogue box.

Creating General Procedure

Type the name of the procedure in the name box; select the type of procedure and scope of the click ok button.

This would automatically enter the sub and end sub statements within the declaration section with the specified name

Calling a procedure

There are two ways to call a sub procedure:

Both this statement call a sub named add,

Call Show()

Or

Show()

The procedure accepting the arguments:

Arguments can be passed to the procedure which is to be mentioned in the pair of parentheses following the procedure name.

The values for the procedure or to be passed while calling the particular procedure.


Public Sub Add( a as integer, b  as integer)

Dim c as integer

 c= a + b

Msgbox “ The sum of two arguments =”&c

End sub


Private sub command1_click ()

Dim num1,num2 as integer

num1 = Inputbox(“ Enter the first number”)

num2 = Inputbox(“ Enter the Second number”)

call Add( num1 num2)

End sub

Argument ByValue and ByRef

By default Visual Basic procedure accept the argument by reference.  The user can also pass argument by Value using ByVal keyword before the argument name.

Public Sub Add( a is integer)

End Sub

The procedure Add() accepts the augment of type integer by reference.  To pass an argument by value to the procedure

Private Sub Add( ByVal a as integer)

End sub

Difference between passing and argument By Value and By Reference:

By Value

 By Reference

A separate copy of that variable is passed as an argument

A reference of a variable is pass to the procedure

The variable value in the calling and the called procedure is stored at separate location

The variable value in the calling and the called procedure is stored at same location

Any changes made in the called procedure are not updated in calling procedure

Any changes made in called procedure are updated in the calling procedure

Consumption of memory space is more

Consumption of memory space is less


The optional arguments:

When a procedure is called then it is must to pass the list of arguments it is going to accept.

e,g The add procedure is designed to accept two integer as the augments and if it is called as

Call  add(10)

Or 

Call add()

It will result in an error.  The argument can be made as an optional argument by preceding its declaration with the keyword optional.  If the value is not pass for an optional argument then it will not result in an error.

Public Sub Add(a As Integer, Optional b As Integer)

MsgBox "Sum of two numbers is" & a + b

End Sub

Providing the default value for the optional argument:

If the value is not passed to the optional argument then its value is assumed to be zero.  If some default value is to be assigned to the function then the default value can be mentioned in the following format.

Public sub pro_name(arg_name1 as data_type, Optional arg_name2 as data_type=default _value)

Public Sub Add(a As Integer, Optional b As Integer=20)

MsgBox "Sum of two numbers is" & a + b

End Sub

Private sub Command1_click()

Call add(10)

End sub

If the value is not passed to the second argument then it will consider the default value for the optional argument b. 

The scope of the Procedure:

The scope of the procedure may be declared either as a private or public. If the scope of the procedure is declared as a private then that particular procedure can be called from the same form

If the scope of the procedure is declared as a public it can be called from the same form as well as from the different forms. 

Add one more forms to the project

PROJECT-> ADD FORM

If the following procedure is written in form2 and its scope is declared as public then it can be called from form 1 as well as form 2.

Public  Sub  Add( a as integer,  b as integer)

Dim c as Integer

Msgbox “The sum of two numberis”&c

End sub

Executing procedure on Different Form

To call the add procedure present on form2 from the code in form1. Write the following code in the code window of the form.

Private Sub Command1_Click()

Call Form2.Add(10,20)

End Sub