Tutorials Point, posted on 25th Apr, 2018

VB.Net - Regular Expressions Example1

Imports System.Text.RegularExpressions
Module regexProg
   Sub showMatch(ByVal text As String, ByVal expr As String)
      Console.WriteLine("The Expression: " + expr)
      Dim mc As MatchCollection = Regex.Matches(text, expr)
      Dim m As Match
      For Each m In mc
          Console.WriteLine(m)
      Next m
   End Sub
   Sub Main()
      Dim str As String = "A Thousand Splendid Suns"
      Console.WriteLine("Matching words that start with 'S': ")
      showMatch(str, "\bS\S*")
      Console.ReadKey()
   End Sub
End Module

Tutorials Point, posted on 24th Apr, 2018

VB.Net Regex Class Example3

Imports System.Text.RegularExpressions
Module regexProg
   Sub Main()
      Dim input As String = "Hello    World   "
      Dim pattern As String = "\\s+"
      Dim replacement As String = " "
      Dim rgx As Regex = New Regex(pattern)
      Dim result As String = rgx.Replace(input, replacement)
      Console.WriteLine("Original String: {0}", input)
      Console.WriteLine("Replacement String: {0}", result)
      Console.ReadKey()
   End Sub
End Module

Tutorials Point, posted on 24th Apr, 2018

VB.Net Regex Class Example2

Imports System.Text.RegularExpressions
Module regexProg
   Sub showMatch(ByVal text As String, ByVal expr As String)
      Console.WriteLine("The Expression: " + expr)
      Dim mc As MatchCollection = Regex.Matches(text, expr)
      Dim m As Match
      For Each m In mc
          Console.WriteLine(m)
      Next m
   End Sub
   Sub Main()
      Dim str As String = "make a maze and manage to measure it"
      Console.WriteLine("Matching words that start with 'm' and ends with 'e': ")
      showMatch(str, "\bm\S*e\b")
      Console.ReadKey()
   End Sub
End Module

Tutorials Point, posted on 24th Apr, 2018

VB.Net Regex Class

Imports System.Text.RegularExpressions
Module regexProg
   Sub showMatch(ByVal text As String, ByVal expr As String)
      Console.WriteLine("The Expression: " + expr)
      Dim mc As MatchCollection = Regex.Matches(text, expr)
      Dim m As Match
      For Each m In mc
          Console.WriteLine(m)
      Next m
   End Sub
   Sub Main()
      Dim str As String = "A Thousand Splendid Suns"
      Console.WriteLine("Matching words that start with 'S': ")
      showMatch(str, "\bS\S*")
      Console.ReadKey()
   End Sub
   End Module

Tutorials Point, posted on 24th Apr, 2018

VB.Net BinaryWriter Class

Imports System.IO
Module fileProg
   Sub Main()
      Dim bw As BinaryWriter
      Dim br As BinaryReader
      Dim i As Integer = 25
      Dim d As Double = 3.14157
      Dim b As Boolean = True
      Dim s As String = "I am happy"
      'create the file
      Try
          bw = New BinaryWriter(New FileStream("mydata", FileMode.Create))
      Catch e As IOException
          Console.WriteLine(e.Message + "\n Cannot create file.")
          Return
      End Try
      'writing into the file
      Try
          bw.Write(i)
          bw.Write(d)
          bw.Write(b)
          bw.Write(s)
      Catch e As IOException
          Console.WriteLine(e.Message + "\n Cannot write to file.")
          Return
      End Try
      bw.Close()
      'reading from the file
      Try
          br = New BinaryReader(New FileStream("mydata", FileMode.Open))
      Catch e As IOException
          Console.WriteLine(e.Message + "\n Cannot open file.")
          Return
      End Try
      Try
           i = br.ReadInt32()
          Console.WriteLine("Integer data: {0}", i)
          d = br.ReadDouble()
          Console.WriteLine("Double data: {0}", d)
          b = br.ReadBoolean()
          Console.WriteLine("Boolean data: {0}", b)
          s = br.ReadString()
          Console.WriteLine("String data: {0}", s)
      Catch e As IOException
          Console.WriteLine(e.Message + "\n Cannot read from file.")
          Return
      End Try
      br.Close()
      Console.ReadKey()
   End Sub
End Module

Tutorials Point, posted on 24th Apr, 2018

VB.Net StreamWriter Class

Imports System.IO
Module fileProg
   Sub Main()
      Dim names As String() = New String() {"Zara Ali", _
         "Nuha Ali", "Amir Sohel", "M Amlan"}
      Dim s As String
      Using sw As StreamWriter = New StreamWriter("names.txt")
          For Each s In names
              sw.WriteLine(s)
          Next s
      End Using
      ' Read and show each line from the file. 
      Dim line As String
      Using sr As StreamReader = New StreamReader("names.txt")
          line = sr.ReadLine()
          While (line <> Nothing)
              Console.WriteLine(line)
              line = sr.ReadLine()
          End While
      End Using
      Console.ReadKey()
   End Sub
End Module

Tutorials Point, posted on 24th Apr, 2018

VB.Net FileStream Class

Imports System.IO
Module fileProg
   Sub Main()
      Dim f1 As FileStream = New FileStream("sample.txt", _
              FileMode.OpenOrCreate, FileAccess.ReadWrite)
      Dim i As Integer
      For i = 0 To 20
          f1.WriteByte(CByte(i))
      Next i
      f1.Position = 0
      For i = 0 To 20
          Console.Write("{0} ", f1.ReadByte())
      Next i
      f1.Close()
      Console.ReadKey()
   End Sub
End Module

Tutorials Point, posted on 24th Apr, 2018

VB.Net Creating User-Defined Exceptions

Module exceptionProg
   Public Class TempIsZeroException : Inherits ApplicationException
      Public Sub New(ByVal message As String)
          MyBase.New(message)
      End Sub
   End Class
   Public Class Temperature
      Dim temperature As Integer = 0
      Sub showTemp()
          If (temperature = 0) Then
              Throw (New TempIsZeroException("Zero Temperature found"))
          Else
              Console.WriteLine("Temperature: {0}", temperature)
          End If
      End Sub
   End Class
   Sub Main()
      Dim temp As Temperature = New Temperature()
      Try
          temp.showTemp()
      Catch e As TempIsZeroException
          Console.WriteLine("TempIsZeroException: {0}", e.Message)
      End Try
      Console.ReadKey()
   End Sub
End Module

Tutorials Point, posted on 24th Apr, 2018

VB.Net Handling Exceptions

Module exceptionProg
   Sub division(ByVal num1 As Integer, ByVal num2 As Integer)
      Dim result As Integer
      Try
          result = num1 \ num2
      Catch e As DivideByZeroException
          Console.WriteLine("Exception caught: {0}", e)
      Finally
          Console.WriteLine("Result: {0}", result)
      End Try
   End Sub
   Sub Main()
      division(25, 0)
      Console.ReadKey()
  End Sub
End Module

Tutorials Point, posted on 24th Apr, 2018

VB.Net Base Class Initialization

' Base class
Class Rectangle
   Protected width As Double
   Protected length As Double
   Public Sub New(ByVal l As Double, ByVal w As Double)
      length = l
      width = w
   End Sub
   Public Function GetArea() As Double
      Return (width * length)
   End Function
   Public Overridable Sub Display()
      Console.WriteLine("Length: {0}", length)
      Console.WriteLine("Width: {0}", width)
      Console.WriteLine("Area: {0}", GetArea())
   End Sub
   'end class Rectangle  
End Class
'Derived class
Class Tabletop : Inherits Rectangle
   Private cost As Double
   Public Sub New(ByVal l As Double, ByVal w As Double)
      MyBase.New(l, w)
   End Sub
   Public Function GetCost() As Double
      Dim cost As Double
      cost = GetArea() * 70
      Return cost
   End Function
   Public Overrides Sub Display()
      MyBase.Display()
      Console.WriteLine("Cost: {0}", GetCost())
   End Sub
    'end class Tabletop
End Class
Class RectangleTester
   Shared Sub Main()
      Dim t As Tabletop = New Tabletop(4.5, 7.5)
      t.Display()
      Console.ReadKey()
   End Sub
End Class

1 2 3 4 5 6 7 Next
Advertisements
Loading...