Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Visual Basic .NET

Similar presentations


Presentation on theme: "Programming with Visual Basic .NET"— Presentation transcript:

1 Programming with Visual Basic .NET
Using Classes and Method Week # 9 Tariq Ibn Aziz Compunet Corporation

2 The System Namespace The System namespace contains fundamental classes and base classes System.String System.Integer System.Array System.Console System.Convert System.Math System.Random Compunet Corporation

3 Math Class Math class provides constants and static methods for trigonometric, logarithmic, and other common mathematical functions Compunet Corporation

4 Shared Methods in Math Class
Polymorphism example Public Const E As Double Public Const PI As Double Overloads Public Shared Function Abs (Double) as Double Overloads Public Shared Function Abs (Decimal) as Decimal Overloads Public Shared Function Abs (Short) as Short Overloads Public Shared Function Abs (Integer) as Integer Overloads Public Shared Function Abs (Long) as Long Overloads Public Shared Function Abs (Single) as Single Public Shared Function Cos (ByVal d As Double) As Double Public Shared Function Sin (ByVal d As Double) As Double Public Shared Function Tan (ByVal d As Double) As Double Compunet Corporation

5 Shared Methods in Math Class
Public Shared Function Max (Double, Double) As Double Public Shared Function Max (Byte, Byte) As Byte Public Shared Function Min (Double, Double) As Double Public Shared Function Min (Byte, Byte) As Byte Public Shared Function Pow (ByVal x As Double, ByVal y As Double) As Double Public Shared Function Sqrt (ByVal x As Double) As Double Overloads Public Shared Function Round (Double) As Double Compunet Corporation

6 Shared Methods Example 1
Imports System.Console Imports System Public Module MathClass Sub Main() WriteLine(Math.Max(-8,-4)) WriteLine(Math.Min(-8,-4)) WriteLine(Math.Abs(-84)) End Sub End Module Output: -4 -8 84 Compunet Corporation

7 Shared Methods Example 2
Imports System.Console Imports System Public Module EandPI Sub Main() WriteLine("E=" & Math.E) WriteLine("PI=" & Math.PI) End Sub End Module Output: E = PI= Compunet Corporation

8 Shared Methods Exercise
Write a program that displays the square root of 23.45 Write a program that displays the value of 2 raised to the power 12 Write a program to compute the hypotenuse of a right triangle whose sides are 4.5 and 8.9 units long Compunet Corporation

9 String Class The String class provides several good examples of instance methods An instance variable is associated with an object. It is necessary to create an instance of class in order to read or write it. The syntax of accessing instance variable is as follows: objRef.varName The syntax of accessing shared or static variable is as follows: String.varName Compunet Corporation

10 String Class The class String is defined in System class, which automatically imported into every program you write. Dim strFirst As String = "Goodbye" Compunet Corporation

11 Array of String Imports System.Console Public Module StringArray
Sub Main() Dim deptName(3) As String deptName(0)="Accounting" deptName(1)="Human Resources" deptName(2)="Sales" WriteLine("0=" & deptName(0)) WriteLine("1=" & deptName(1)) WriteLine("2=" & deptName(2)) End Sub End Module OUTPUT: 0 = Accounting 1 = Human Resource 2 = Sales Compunet Corporation

12 Instance Methods of String Class
Determines whether two String objects have the same value Function Equals (Object) As Boolean Compares this instance with a specified object Function CompareTo (Object) As Integer Determines whether the end of this instance matches the specified String Function EndsWith( String ) As Boolean Compunet Corporation

13 Instance Methods of String Class
Determines whether two String objects have the same value. Function Equals( Object ) As Boolean Reports the index of the first occurrence of a String, or one or more characters, within this instance. Function IndexOf( Char ) As Integer Retrieves a substring from this instance Function Substring( Integer ) As String Function Substring( Integer, Integer ) As String Compunet Corporation

14 Instance Methods of String Class
Returns a copy of this String in lowercase. Function ToLower() As String Converts the value of this instance to a String Function ToString() As String Returns a copy of this String in uppercase. Function ToUpper() As String Removes all occurrences of a set of specified characters from the beginning and end of this instance Function Trim() As String Compunet Corporation

15 Shared Methods of String Class
Compares two specified String objects Shared Function Compare (String, String) As Integer Concatenates one or more instances of String Shared Function Concat (Object, Object) As String Creates a new instance of String with the same value as a specified String Shared Function Copy( String ) As String Compunet Corporation

16 Instance Method of String Class Example-I
Imports System.Console Public Module First10Char Sub Main() Dim s0 As String = "One Two Three Four " WriteLine(S0.substring(0,10)) ' One Two Th End Sub End Module Output Instance Method Compunet Corporation

17 Instance Method of String Class Example-II
Imports System.Console Public Module First10Char Sub Main() Dim s1 As String = "One" Dim s2 As String = "Two" Dim s3 As String = "One" WriteLine(S1.compareTo(s2)) '-1 WriteLine(S2.compareTo(s1)) ' 1 WriteLine(S1.compareTo(s3)) ' 0 End Sub End Module Output Instance Method Compunet Corporation

18 Instance Method of String Class Example-III
Imports System.Console Public Module DollarAmount Sub Main() Dim s1 As String = "The Total Amount is $45.78" Dim i As Integer= 0 i= s1.indexOf("$") Dim s2 AS String = s1.substring(i) WriteLine(s2) End Sub End Module $45.78 Output Compunet Corporation

19 Shared Method of String Class Example-IV
Imports System.Console Public Module DollarAmount Sub Main() Dim s1 As String = "CA-" Dim s2 As String = "121" Dim s3 As String s3= String.concat(s1,s2) WriteLine(s3) End Sub End Module Compunet Corporation

20 Shared Method of String Class Example-V (Contd.)
Imports System Class Sample Public Shared Sub Main() Dim str1 As String = "abc" Dim str2 As String = "xyz" Console.WriteLine("1) str1 = '{0}'", str1) Console.WriteLine("2) str2 = '{0}'", str2) Console.WriteLine("Copy...") str2 = [String].Copy(str1) Console.WriteLine("3) str1 = '{0}'", str1) Console.WriteLine("4) str2 = '{0}'", str2) End Sub 'Main End Class 'Sample Compunet Corporation

21 Example-V Output ' 'This example produces the following results:
'1) str1 = 'abc' '2) str2 = 'xyz' 'Copy... '3) str1 = 'abc' '4) str2 = 'abc' Compunet Corporation

22 String Class Exercise Write a program that displays the substring formed by the last ten characters of a string. Hint: Use Length Property of String Class A string contains five numbers separated by commas. Write a program that displays the last number Hint: Search for String Members in help and look for LastIndexOf ( ) method Compunet Corporation

23 Integer Class Instance Methods of Integer Class
A System.Int32 object encapsulates a simple Integer value. A System.Int32 class defines MaxValue and MinValue as two of its static/Shared variable Instance Methods of Integer Class Function CompartTo (Object) As Integer Function Equals (Object) As Boolean Function ToString () As String Compunet Corporation

24 Integer Class Shared Methods of Integer Class
Overloads public Shared Function Parse (String) As Integer Compunet Corporation

25 Integer Class Instance Method Example
Imports System Class Sample Public Shared Sub Main() Dim str1 As Object = "Hello" Dim v1 As Integer = Str1.CompareTo("Hello") Dim v2 As Integer = Str1.CompareTo("Tariq") Dim v3 As Integer = Str1.CompareTo("CA121") Console.WriteLine("V1 = " & V1 )' V1 = 0 Console.WriteLine("V2 = " & V2 )' V2 =-1 Console.WriteLine("V3 = " & V3 )' V3 = 1 End Sub 'Main End Class Output Compunet Corporation

26 Integer Class Example WriteLine(System.Int32.MinValue) '-2147483648
Imports System.Console Public Module DollarAmount Sub Main() Dim i As System.Int32 WriteLine(i.MaxValue) ' WriteLine(System.Int32.MinValue) ' End Sub End Module Output Compunet Corporation

27 Integer Class Shared Method Example
Imports System Class Sample Public Shared Sub Main() Dim str1 As String = "125" Dim no As Integer = Integer.Parse(str1) Console.WriteLine("Total = " & no + 5 ) End Sub 'Main End Class Output Compunet Corporation

28 Integer Example Output Imports System.Console
Public Module StringToInt Sub Main() Dim s As String = "100" Dim i as Integer = s System.console.WriteLine(i) ' 100 i = i + 10 System.console.WriteLine(i) ' 110 End Sub End Module Compunet Corporation

29 Char Class Instance Methods
Public Overridable Function CompareTo (Object) As Integer Public Overridable Function Equal (Object) As Boolean Compunet Corporation

30 Char Class Shared Methods "
Public Shared Shared Function IsControl (Char) As Boolean Public Shared Function IsDigit (Char) As Boolean Public Shared Function IsLetter (Char) As Boolean Public Shared Function IsLower (Char) As Boolean Public Shared Function IsNumber (Char) As Boolean Public Shared Function IsPunctuation (Char) As Boolean Public Shared Function IsSeperator (String,Integer) As Boolean Public Shared Function IsSymbol (Char) As Boolean Public Shared Function IsWhiteSpace (String,Integer) As Boolean " Compunet Corporation

31 Char Example imports System.Console Module CharStructure
Public Sub Main() Dim chA As Char chA = "A"c Dim ch1 As Char ch1 = "1"c Dim str As String str = "test string“ ' Output: "-1" (meaning 'A' < 'B') WriteLine(chA.CompareTo("B"c)) WriteLine(chA.Equals("A"c)) ' Output: "True" WriteLine(Char.GetNumericValue(ch1)) ' Output: "1" Compunet Corporation

32 Char Example WriteLine(Char.IsControl(Chr(9))) ' Output: "True"
WriteLine(Char.IsDigit(ch1)) ' Output: "True" WriteLine(Char.IsLetter(","c)) ' Output: "False" WriteLine(Char.IsLower("u"c)) ' Output: "True" WriteLine(Char.IsNumber(ch1)) ' Output: "True" WriteLine(Char.IsPunctuation("."c)) ' Output: "True" WriteLine(Char.IsSeparator(str, 4)) ' Output: "True" WriteLine(Char.IsSymbol("+"c)) ' Output: "True" WriteLine(Char.IsWhiteSpace(str, 4)) ' Output: "True" WriteLine(Char.Parse("S")) ' Output: "S" WriteLine(Char.ToLower("M"c)) ' Output: "m" WriteLine("x"c.ToString()) ' Output: "x" End Sub End Module Compunet Corporation

33 Command Line Arguments
' CommandLineArg.vb Public Module CommandLineArgument Sub Main() Dim commands As String commands = Microsoft.VisualBasic.Command() System.console.WriteLine (commands) End Sub End Module OUTPUT: H:\CA121> CommandLineArg Tariq Aziz Tariq Aziz H:\CA121> CommandLineArg Hello 123 Hello 123 Compunet Corporation

34 Command Line Arguments
' CommandLineArg.vb Public Module CommandLineArgument Sub Main() Dim separators As String = " " Dim commands As String Dim args() As String commands = Microsoft.VisualBasic.Command() args=commands.Split(separators.ToCharArray) System.console.WriteLine(" Args: " & args.length) System.console.WriteLine(args(0)) ' Tariq System.console.WriteLine(args(1)) ' 123 End Sub End Module Output Compunet Corporation

35 Command Line Arguments
Output: H:\CA121> CommandLineArg Hello 123 Hello 123 Compunet Corporation

36 Command Line Arguments
Public Module Add2Arg Sub Main() Dim separators As String = " " Dim commands As String Dim args() As String commands = Microsoft.VisualBasic.Command() args = commands.Split(separators.ToCharArray) Dim i As Integer = args(0) ' 10 Dim j As Integer = args(1) ' 25 Dim k As integer = i + j System.console.WriteLine (k) ' 35 End Sub End Module Output Compunet Corporation

37 Local variables and scope
Local variable are declared inside a method and exist only during its execution Local variables are only available in their scope only Imports System.Console Public Module Add2Arg Sub Calculate() Dim a As Integer= 10 Dim b As Integer= 11 WriteLine (a + b) ' 21 End Sub Sub Main() Calculate() WriteLine (a) End Module Not Available here Compunet Corporation

38 Local variables and scope
Local variables are created upon entry into block and destroyed upon exit Variable can be defined inside statement block e.g inside do, for, or while loop Imports System.Console Public Module Add2Arg Sub Calculate() For a As Integer= 1 To 2 Dim b As Integer= 11 WriteLine (b) Next a WriteLine (b)'Not visible End Sub Sub Main() Calculate() End Module Not Available here Compunet Corporation

39 Local variables and scope
Declaring a local variable with the same name as static or instance variable will hide the static and instance variable Imports System.Console Dim b As Integer= 15 Public Module Add2Arg Sub Calculate() For a As Integer= 1 To 2 Dim b As Integer= 11 WriteLine (b) '11 Next a WriteLine (b) '15 End Sub Sub Main() Calculate() End Module It will hide b=15 Compunet Corporation

40 Variable Scope Keywords
Keywords specify the variable scope Private - available only to code within the local class Public - used outside the class Friend - used only within the current application or project Compunet Corporation

41 Variable Scope Block scope — available only within the code block in which it is declared Procedure scope — available only within the procedure in which it is declared Module scope — available to all code within the module, class, or structure in which it is declared Namespace scope — available to all code in the namespace Imports System.Console Public Module Add2Arg Dim b As Integer= 15 Sub Calculate() Dim c As Integer= 13 For a As Integer= 1 To 2 Dim b As Integer= 11 WriteLine (b) '11 Next a WriteLine (b) '15 WriteLine (c) '13 End Sub Sub Main() Calculate() End Module Block Scope Procedure Scope Module Scope Compunet Corporation

42 Block Scope A block is a set of statements terminated by an End, Else, Loop, or Next statement, for example within a For...Next or If...Then...Else...End If construction. An element declared within a block can be used only within that block. In the following example, the scope of the integer variable Cube is the block between If and End If, and Cube can no longer be referenced when execution passes out of the block: If N < 1291 Then Dim Cube As Integer Cube = N ^ 3 End If Imports System.Console Public Module Add2Arg Sub Calculate() For a As Integer= 1 To 2 Dim b As Integer= 11 WriteLine (b) '11 Next a WriteLine (b) 'Not visible End Sub Sub Main() Calculate() End Module Compunet Corporation

43 Procedure Scope An element declared within a procedure is not available outside that procedure. Only the procedure that contains the declaration can use it. Elements at this level are also known as local elements. You declare them with the Dim statement, with or without the Static keyword. Imports System.Console Public Module Add2Arg Sub Calculate() Dim b As Integer= 13 For a As Integer= 1 To 2 WriteLine (b) '13 Next a End Sub Sub Main() Calculate() WriteLine (b) 'Not visible End Module Compunet Corporation

44 Module Scope For convenience, the single term module level applies equally to modules, classes, and structures. You can declare elements at this level by placing the declaration statement outside of any procedure or block within the module, class, or structure. Imports System.Console Public Module Add2Arg Dim b As Integer= 15 Sub Calculate() For a As Integer= 1 To 2 WriteLine (b) '15 Next a End Sub Sub Main() Calculate() End Module Compunet Corporation

45 B is visible in namespace because of Public
Namespace Scope If you declare an element at module level using the Friend or Public keyword, it becomes available to all procedures throughout the namespace in which the element is declared. Imports System.Console Public Module Add2Arg Public B As Integer= 15 Sub Calculate() For a As Integer= 1 To 2 WriteLine (B) '15 Next a End Sub Sub Main() Calculate() End Module B is visible in namespace because of Public Compunet Corporation

46 Lifetime Variables Module, shared, instance, and static variables consume memory resources until your application stops running, so use them only when necessary. Compunet Corporation

47 Scope of K is inside the For Loop
Block Scope Exercise Scope of K is inside the For Loop Imports System.Console class VariableScope Shared Sub Main() For j As Integer = 0 To 5 Dim k As Integer=100 WriteLine ("j = " & j & ";K = " & k) k = k + 1 Next j End Sub End Class Output j = 0; k = 100 j = 1; k = 100 j = 2; k = 100 j = 3; k = 100 j = 4; k = 100 Compunet Corporation

48 Procedure Scope Exercise
Local variables S and J hide the class variable class MyObject Dim S As Short = 400 Dim J As Integer = 200 Sub myMethod() Dim S As Short = ' local variable Dim J As Integer = 100 ' local variable Dim D As Double = 1E100 ' local variable System.Console.WriteLine (S & "," & J & "," & D) End Sub End Class Class MainClass public Shared Sub Main() Dim m As New MyObject mo.myMethod() Output: 300,100,1.0E100 Compunet Corporation

49 Module variables s and j
Module Scope Exercise Module variables s and j class MyObject Dim s As Short = 400 Dim j As Integer = 200 Sub f() Dim d As Double = 1E100 'local variable System.Console.WriteLine (s & "," & j & "," & d) End Sub public Shared Sub Main() Dim mo As New MyObject mo.f() End Class Output: 400,200,1.0E100 Compunet Corporation

50 Argument Passing All method arguments are passed by value. This means that copies of the arguments are provided to a method. Any changes to those copies are not visible outside the methods Imports System.Console Public Module Add2Arg Sub Print( x As Integer) x=10 WriteLine ( x ) '10 End Sub Sub Main() Dim x As Integer=5 Print( x ) WriteLine ( x ) '5 End Module Compunet Corporation

51 Argument Passing The situation changes when an arrays or object is passed as an argument. In this case entire array or object is not copied, instead only a copy of reference is provided. Therefore any changes to the array or object are visible outside the method. However reference itself is passed by value Imports System.Console Class Argument Shared sub a (k As Integer()) k(0)=11 End Sub Shared Sub display(k As Integer()) For m As Integer =0 To 3 WriteLine (k(m)) Next m Shared Sub Main() Dim k As Integer()={1,2,3,4} display(k) a(k) End Class Compunet Corporation

52 Argument Passing (Home Practice)
Imports System.Console Public Module Add2Arg Sub GetMaximum(x As Integer, y As Integer ) If x > y Then WriteLine ( x & " is bigger number" ) Else WriteLine ( y & " is bigger number" ) End If End Sub Sub Main() GetMaximum( 2 , 3) End Module Compunet Corporation

53 Argument Passing (Home Practice)
Class CallByValue Shared sub a (j As Integer, k As Integer()) j=7 k(0)=11 End Sub Shared Sub display (j As Integer, k As Integer()) System.Console.WriteLine ("J=" & j) For m As Integer =0 To 3 System.Console.WriteLine (k(m)) Next m Compunet Corporation

54 Argument Passing (Home Practice)
Shared Sub Main() Dim j As Integer=5 Dim k As Integer()={1,2,3,4} display(j, k) a(j, k) End Sub End Class Output 5 Compunet Corporation

55 Exercise Write an application that accepts one command-line argument and displays its Spanish equivalent. For example, the tokens “uno,” “dos,” “tres,” “quatro,” and “cinco” are the Spanish tokens for the numbers “one” through “five”. Create a class with a static method to accomplish this task Compunet Corporation


Download ppt "Programming with Visual Basic .NET"

Similar presentations


Ads by Google