VB.Net Tutorial/Class Module/Function

Материал из VB Эксперт
Перейти к: навигация, поиск

Assign function return value to variables

Module Module1
    Function GetBookPrice() As Double
        GetBookPrice = 49.99
    End Function
    Function GetBookTitle() As String
        GetBookTitle = "Title"
    End Function
    Sub Main()
        Dim Price As Double
        Dim Title As String
        Price = GetBookPrice()
        Title = GetBookTitle()
        Console.WriteLine(Price)
        Console.WriteLine(Title)
    End Sub
End Module
49.99
Title

Calculates the power of a value, defaults to square

Public Class Tester
   Public Shared Sub Main
      Console.WriteLine(Power(12))
      Console.WriteLine(Power(2,2))
    End Sub 
   
  " use iteration to calculate power
   Shared Function Power(ByVal base As Integer, _
      Optional ByVal exponent As Integer = 2) As Integer
      Dim total As Integer = 1
      Dim i As Integer
      For i = 1 To exponent
         total *= base
      Next
      Return total
   End Function " Power
End Class
144
4

Call function as a statement

Module Module1
    Function GetBookPrice() As Double
        GetBookPrice = 49.99
    End Function
    Function GetBookTitle() As String
        GetBookTitle = "Title"
    End Function
    Sub Main()
        Console.WriteLine(GetBookPrice())
        Console.WriteLine(GetBookTitle())
    End Sub
End Module
49.99
Title

Define function and call it in a Module

Module Module1
    Sub ShowBookInformation()
        Console.WriteLine("A")
        Console.WriteLine("B")
        Console.WriteLine("C")
        Console.WriteLine("D")
    End Sub
    Sub GreetInEnglish()
        Console.WriteLine("Hello, world")
    End Sub
    Sub GreetInSpanish()
        Console.WriteLine("Hola, mundo")
    End Sub
    Sub ShowTime()
        Console.WriteLine("Current time is: " & Now)
    End Sub
    Sub Main()
        ShowTime()
        GreetInEnglish()
        GreetInSpanish()
        ShowBookInformation()
    End Sub
End Module
Current time is: 11/05/2007 9:29:38 PM
Hello, world
Hola, mundo
A
B
C
D

Define function in a Module

Module Module1
  Const Sale As Decimal = 100
  Const TaxRate As Decimal = 0.04D
  Function GetTotalSale() As Decimal
    Return Sale * (1 + TaxRate)
  End Function
  Sub Main()
    Dim TotalSale As Decimal = GetTotalSale()
    Console.WriteLine(TotalSale)
  End Sub
End Module
104.00

Finds the maximum of three numbers input

Module Tester
    Sub Main()
      Dim value1, value2, value3 As Double
      value1 = 1.1
      value2 = 2.2
      value3 = 3.3
      Console.WriteLine( Maximum(value1, value2, value3) )
    End Sub 
   
   Function Maximum(ByVal valueOne As Double, _
      ByVal valueTwo As Double, ByVal valueThree As Double) _
      As Double
      Return Math.Max(Math.Max(valueOne, valueTwo), valueThree)
   End Function
End Module
3.3

Function procedure to square a number.

Module Tester
   Sub Main()
      Dim i As Integer 
      Console.WriteLine("Number" & vbTab & "Square" & vbCrLf)
      For i = 1 To 10
         Console.WriteLine(i & vbTab & Square(i))
      Next
   End Sub 
   Function Square(ByVal y As Integer) As Integer
      Return y ^ 2
   End Function 
End Module
Number  Square
1       1
2       4
3       9
4       16
5       25
6       36
7       49
8       64
9       81
10      100

Functions with different parameter number

Module Module1
    Sub OneValue(ByVal Name As String)
        Console.WriteLine("Hello, " & Name)
    End Sub
    Sub TwoValues(ByVal Age As Integer, ByVal Name As String)
        Console.WriteLine("Age: " & Age)
        Console.WriteLine("Name: " & Name)
    End Sub
    Sub ThreeValues(ByVal Name As String, ByVal Age As Integer, ByVal Salary As Double)
        Console.WriteLine("Name: " & Name)
        Console.WriteLine("Age: " & Age)
        Console.WriteLine("Salary: " & Salary)
    End Sub
    Sub Main()
        OneValue("Mr. Gates")
        Console.WriteLine()
        TwoValues(50, "Mr. Gates")
        Console.WriteLine()
        ThreeValues("Mr. Gates", 50, 250000.0)
    End Sub
End Module
Hello, Mr. Gates
Age: 50
Name: Mr. Gates
Name: Mr. Gates
Age: 50
Salary: 250000

Function with string return

public class Test
   public Shared Sub Main
        Dim strISBN As String = "078212283"
        Console.WriteLine("The check Digit is " & ISBNCheckDigit("078212283") & ".")
        Console.WriteLine("The complete ISBN is " & strISBN & ISBNCheckDigit(strISBN) & ".")
   End Sub
   Shared Function ISBNCheckDigit(ByVal strISBN As String) As String
        Dim i, intCheckSum, intCheckDigit As Integer
        For i = 0 To 8
            intCheckSum = intCheckSum + (10 - i) * strISBN.Substring(i, 1)
        Next
        intCheckDigit = 11 - (intCheckSum Mod 11)
        If intCheckDigit = 10 Then
            Return ("X")
        Else
            Return (intCheckDigit.ToString)
        End If
    End Function
End class
The check Digit is 3.
The complete ISBN is 0782122833.

Pass Double to a function

public class Test
   public Shared Sub Main
        Dim area As Double
        area = CalculateAreaFromRadius(100)
        Console.WriteLine(area)
   End Sub
   Shared Function CalculateAreaFromRadius(ByVal radius As Double) As Double
        Dim radiusSquared As Double
        radiusSquared = radius * radius
        Dim result As Double
        result = radiusSquared * Math.PI
        Return result
    End Function
End class
31415.9265358979

Return double type value from Function

public class Test
   public Shared Sub Main
        Console.WriteLine(ComputeSalesTax(34.34, 0.07))
    End Sub
    
    Shared Function ComputeSalesTax(ByVal amount As Double, _
            ByVal tax As Single) As Double
        Return amount * tax
    End Function
End class
2.40380001023412

Return string type value from function

Public Class Tester
    Shared Sub Main()
        Dim iFirstDay As Integer
        Dim iLastDay As Integer
        Dim iCurrentDay As Integer
        iFirstDay = 2
        iLastDay = 6
        For iCurrentDay = iFirstDay to iLastDay
            System.Console.WriteLine(WeekdayName(iCurrentDay))
        Next iCurrentDay
    End Sub
    Shared Function WeekdayName(ByVal iDayNumber As Integer) As String
        Dim sWeekdayName As String
        Select Case iDayNumber
            Case 1
                sWeekdayName = "Sunday"
            Case 2
                sWeekdayName = "Monday"
            Case 3
                sWeekdayName = "Tuesday"
            Case 4
                sWeekdayName = "Wednesday"
            Case 5
                sWeekdayName = "Thursday"
            Case 6
                sWeekdayName = "Friday"
            Case 7
                sWeekdayName = "Saturday"
            Case Else
                sWeekdayName = "Invalid Day Number"
        End Select
        Return sWeekdayName
    End Function
End Class
Monday
Tuesday
Wednesday
Thursday
Friday

Use function in If statement

Module Module1
    Function GetBookPrice() As Double
        GetBookPrice = 49.99
    End Function
    Function GetBookTitle() As String
        GetBookTitle = "Title"
    End Function
    Sub Main()
        If (GetBookPrice() = 49.99) Then
            Console.WriteLine("The book is 49.99")
        End If
    End Sub
End Module
The book is 49.99