VB.Net Tutorial/Data Type/Integer — различия между версиями

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

Текущая версия на 15:54, 26 мая 2010

Add two integers together

<source lang="vbnet">Module Tester

  Sub Main()
     Dim firstNumber, secondNumber As String
     Dim number1, number2, sumOfNumbers As Integer
     firstNumber = 10
     secondNumber = 20
     number1 = firstNumber
     number2 = secondNumber
     sumOfNumbers = number1 + number2 " add numbers
     Console.WriteLine("The sum is {0}", sumOfNumbers)
  End Sub " Main

End Module</source>

The sum is 30

Compare Integer value in If statement

<source lang="vbnet">Module Module1

   Sub Main()
       Dim TestScore As Integer = 80
       If TestScore >= 90 Then
           Console.WriteLine("Test grade: A")
       ElseIf TestScore >= 80 Then
           Console.WriteLine("Test grade: B")
       ElseIf TestScore >= 70 Then
           Console.WriteLine("Test grade: C")
       Else
           Console.WriteLine("Test grade: F")
       End If
   End Sub

End Module</source>

Test grade: B

Define Integer variable and assign value

<source lang="vbnet">Module Module1

   Sub Main( )
      Dim myInt As Integer = 7
      Console.WriteLine("Initialized myInt: {0}", myInt)
      myInt = 5
      Console.WriteLine("After assignment myInt: {0}", myInt)
   End Sub
End Module</source>
Initialized myInt: 7
After assignment myInt: 5

Explicit conversion of an integer to a string

<source lang="vbnet">Module Tester

   Public Sub Main()
       Dim iInteger As Integer = 5280
       Dim lLong As Long
       Dim bytByte As Byte
       Dim sngSingle As Single
       Dim dblDouble As Double
       Dim decDecimal As Decimal
       Console.WriteLine("Explicit conversion of an integer to a string: {0}", CStr(iInteger))
   End Sub

End Module</source>

Explicit conversion of an integer to a string: 5280

Implicit conversion of an integer to a string

<source lang="vbnet">Module Tester

   Public Sub Main()
       Dim iInteger As Integer = 5280
       Dim lLong As Long
       Dim bytByte As Byte
       Dim sngSingle As Single
       Dim dblDouble As Double
       Dim decDecimal As Decimal
       Console.WriteLine("Implicit conversion of an integer to a string: {0}", iInteger)
   End Sub

End Module</source>

Implicit conversion of an integer to a string: 5280

Integer boolean calculation: Or, And, Xor, Not

<source lang="vbnet">public class Test

  public Shared Sub Main
       Dim I As Integer
   
   
       I = 3 Or 4
       Console.WriteLine(I)
   
       I = 2 And 4
       Console.WriteLine(I)
   
       I = 3 Xor 3
       Console.WriteLine(I)
   
       I = Not 5
       Console.WriteLine(I)
  End Sub

End class</source>

7
0
0
-6

Integer calculation

<source lang="vbnet">public class Test

  public Shared Sub Main
       Dim n As Integer
       " try adding numbers...
       n = 16
       n += 10.23
       Console.WriteLine("Addition " & n)
       " try subtracting numbers...
       n = 24
       n -= 2
       Console.WriteLine("Subtraction " & n)
       " try multiplying numbers...
       n = 6
       n *= 10
       Console.WriteLine("Multiplication " & n)
       " try dividing numbers...
       n = 12
       n /= 6
       Console.WriteLine("Division " & n)
  End Sub

End class</source>

Addition 26
Subtraction 22
Multiplication 60
Division 2

Integer format: D10

<source lang="vbnet">Public Class Tester

   Public Shared Sub Main
       Dim intNumber As Integer = 12345
       Console.WriteLine(intNumber.ToString("D10"))
   End Sub

End Class</source>

0000012345

Integer.GetType

<source lang="vbnet">Module Module1

 Const x As String = "This is a string"
 Sub Main()
   Dim a As Double = 5.678
   Dim b As Int32 = 123
   Console.WriteLine(a.ToString)
   Console.WriteLine(b.ToString)
   Console.WriteLine(456.987.ToString)
   Dim c As Integer
   Console.WriteLine(c.GetType())
   Console.WriteLine(c.GetType().ToString)
 End Sub

End Module</source>

5.678
123
456.987
System.Int32
System.Int32

Integer OverflowException

<source lang="vbnet">Public Class Tester

   Public Shared Sub Main
       Dim A, B As Integer
       Dim C As Integer
       Try
           A = 9999
           B = 9999
           C = A * B * B * B
       Catch Except As OverflowException
           Console.WriteLine("Overflow error detected")
       End Try
   End Sub

End Class</source>

Overflow error detected

MinValue and MaxValue of Integer

<source lang="vbnet">public class Test

  public Shared Sub Main
              Dim iNum As Integer
              Console.WriteLine("Integer: " & iNum.MinValue & " to " & iNum.MaxValue)
  End Sub

End class</source>

Integer: -2147483648 to 2147483647

Parse Integer

<source lang="vbnet">public class Test

  public Shared Sub Main
       Try
           Dim num_items As Integer = Integer.Parse("123")
       Catch ex As Exception
           Console.WriteLine(ex.Message)
       End Try
  End Sub

End class</source>

Parse Integer variable and get hash code from Integer

<source lang="vbnet">public class Test

  public Shared Sub Main
   Dim Number As String
   Number = "4"
   Console.WriteLine(Integer.Parse(Number))
   Console.WriteLine(Number.GetHashCode())
  End Sub

End class</source>

4
-842352756

Pass Integer to a function by reference

<source lang="vbnet">Module Module1

   Sub ParameterChange(ByRef A As Integer)
       A = 1001
       Console.WriteLine("Value of A in subroutine " & A)
   End Sub
   Sub Main()
       Dim Number As Integer = 100
       Console.WriteLine("Number before function call: " & Number)
       ParameterChange(Number)
       Console.WriteLine("Number before function call: " & Number)
   End Sub

End Module</source>

Number before function call: 100
Value of A in subroutine 1001
Number before function call: 1001

Pass Integer to a function by value

<source lang="vbnet">Module Module1

   Sub NoChangeToParameter(ByVal A As Integer)
       A = 1001
       Console.WriteLine("Value of A in subroutine " & A)
   End Sub
   Sub Main()
       Dim Number As Integer = 100
       Console.WriteLine("Number before function call: " & Number)
       NoChangeToParameter(Number)
       Console.WriteLine("Number before function call: " & Number)
   End Sub

End Module</source>

Number before function call: 100
Value of A in subroutine 1001
Number before function call: 100

Swap two integers without using a third

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Collections Public Class Tester

   Public Shared Sub Main
       
       Dim firstValue As Integer
       Dim secondValue As Integer
       firstValue = 17
       secondValue = 123
       Console.WriteLine("Before swap: {0}, {1}",firstValue, secondValue)
       firstValue = firstValue Xor secondValue
       secondValue = firstValue Xor secondValue
       firstValue = firstValue Xor secondValue
       Console.WriteLine("After swap: {0}, {1}",firstValue, secondValue)
   End Sub

End Class</source>

Before swap: 17, 123
After swap: 123, 17

Use Integer.CompareTo to compare two integers

<source lang="vbnet">public class Test

  public Shared Sub Main
       Dim I As Integer = 0
       Dim S As Integer = 8
       Console.WriteLine(I.rupareTo(S))
  End Sub

End class</source>

-1