17.
Class and Objects
'Practical 17 : Develope a program to create a class. Create an object to access the
class.
Imports System
Imports System.Reflection.Metadata.Ecma335
Imports System.Runtime.Intrinsics.Arm
Imports System.Security.Cryptography.X509Certificates
Module Program
Public Class studet
Dim rollno As Integer
Dim name As String
Dim marks As Decimal
Function getdata()
Console.WriteLine("Enter roll no : ")
rollno = Console.ReadLine()
Console.WriteLine("Enter name : ")
name = Console.ReadLine()
Console.WriteLine("Enter marks : ")
marks = Console.ReadLine()
End Function
Function display()
Console.WriteLine("****************Enter Student
Information************")
Console.WriteLine("Roll no = {0}", rollno)
Console.WriteLine("name = {0}", name)
Console.WriteLine("marks = {0}", marks)
End Function
End Class
Sub Main()
Dim s1 As New studet()
Dim s2 As New studet()
s1.getdata()
s2.getdata()
s1.display()
s2.display()
End Sub
End Module
Parameterised constructor
Imports System
Module Program
Public Class book
Dim bookid As Integer
Dim bookname As String
Dim bookprice As Decimal
Function bookinfo(id As Integer, name As String, price As Decimal)
bookid = id
bookname = name
bookprice = price
End Function
Function display()
Console.WriteLine("*********Book Information***************")
Console.WriteLine("Book id : {0}", bookid)
Console.WriteLine("Book id : {0}", bookname)
Console.WriteLine("Book id : {0}", bookprice)
End Function
End Class
Sub Main()
Dim b1 As New book()
b1.bookinfo(1, "saad", 850.5)
b1.display()
End Sub
End Module
Default Constructor
Imports System
Module Program
Public Class vechicle
Sub New()
Console.WriteLine("Constructor called successfully!")
End Sub
Sub display()
Console.WriteLine("This is sub procedure")
End Sub
End Class
Sub Main(args As String())
Dim v1 As New vechicle()
v1.display()
End Sub
End Module
Parameterised Constructor
Imports System
Module Program
Public Class vechicle
Dim vechicleno As String
Sub New(vno As String)
vechicleno = vno
Console.WriteLine("Constructor called successfully!")
End Sub
Sub display()
Console.WriteLine("vechicle no is {0}", vechicleno)
End Sub
End Class
Sub Main()
Dim v1 As New vechicle("MH14gh3423")
v1.display()
End Sub
End Module
Constructor Oveloading
Imports System
Module Program
Public Class vechicle
Dim vechicle_number As String
Sub New(vn As String)
vechicle_number = vn
Console.WriteLine("Constructor called successfully!")
End Sub
Sub New()
vechicle_number = "hfks3344"
Console.WriteLine("Constructor called successfully!")
End Sub
Sub display()
Console.WriteLine("vechicle no is {0}", vechicle_number)
End Sub
End Class
Sub Main()
Dim v1 As New vechicle("MH14gh3423")
Dim v2 As New vechicle()
v1.display()
v2.display()
End Sub
End Module
Inheritance
Imports System
Module Program
Sub Main()
Dim b1 As New B
b1.test
b1.hello
b1.hello("hello world!")
End Sub
Public Class A
Public Overridable Sub test()
Console.WriteLine("This is inside test!")
End Sub
End Class
Public Class B
Inherits A
Public Overrides Sub test()
Console.WriteLine("This is overridable version of test")
Console.ReadLine()
End Sub
Public Sub hello()
Console.WriteLine("Hello")
Console.ReadLine()
End Sub
Public Sub hello(ByVal s1 As String)
Console.WriteLine(s1)
Console.ReadLine()
End Sub
End Class
End Module
Constructor – destructor
Imports System
Module Program
Class sample
Sub New()
Console.WriteLine("Constructor called!")
End Sub
Protected Overrides Sub Finalize()
Console.WriteLine("Destructor called!")
MyBase.Finalize()
End Sub
End Class
Sub Main(args As String())
Dim d1 As New sample
D1.new()
End Sub
End Module
Picturebox and panel
Public Class Form1
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles
PictureBox1.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
PictureBox1.Image = Image.FromFile("C:\Users\mahamadsad\Downloads\OIP
(2).jpg")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
Button2.Click
MsgBox("You clicked submit")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles
Button3.Click
MsgBox("You clicked cancel!")
End Sub
End Class
Custom exception
program
Public Class MyCustomException
Inherits System.Exception
Public Sub New()
MyBase.New("An error occurred in the MyCustomException class.")
End Sub
Public Sub New(message As String)
MyBase.New(message)
End Sub
Public Sub New(message As String, innerException As Exception)
MyBase.New(message, innerException)
End Sub
End Class
Module1
Public Class Program
Public Shared Sub Main()
Try
' Some code that might throw the custom exception...
Throw New MyCustomException("Custom exception message")
Catch ex As MyCustomException
Console.WriteLine("Custom exception caught: " & ex.Message)
Catch ex As Exception
Console.WriteLine("An unexpected error occurred: " & ex.Message)
End Try
End Sub
End Class
errorprovider
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
If String.IsNullOrWhiteSpace(TextBox1.Text) Then
ErrorProvider1.SetError(TextBox1, "Please enter username")
ElseIf String.IsNullOrWhiteSpace(TextBox2.Text) Then
ErrorProvider1.SetError(TextBox2, "Please enter password")
Else
MsgBox("Welcome")
End If
End Sub
End Class