Shared / Static Class in vb.net/C#
Shared / Static Class in vb.net/C#
VB.net
: Shared
C# : Static
You
can’t create object of Shared Class
Below
Image show Properties and Method are not visible for Shared/Static Class
Public Class Form1
Private Sub
Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
'Module is
VB.net are Shared Type Class.
'Shared Class
Example is ARC.Tools.GRIDManager
'When Class s
Defined as Shared There no need to create object of Class.
'If you just want
to create a class that you can't inherit, in C# you can use
' Sealed, and in VB.Net use NotInheritable.
'The VB.Net
equivalent of static is shared.
'From the CLR
point of view, C# static class is just "sealed" and
"abstract" class.
'You can 't
create an instance, because it is abstract, and you can't inherit from it
'since
it is sealed. 'The rest is just some compiler magic.
' Shared Class
Can't Have Interface or Inheritance POssible
'You can't
create object of Shared Or Static of Class. When No Method or Function is
'Define
without Shared KeyWord
'Dim A As New
Student
'A. ' This will not Show any Properties or Method
from Shared class
Student.Name = "Swtin"
Student.Age = 16
Student.DoSomething()
Student.GetSomething(132123132)
'This will
Overwrite Earlier Value
Student.Name = "ABC"
Student.Age = 20
Student.DoSomething()
'Allow to create
Multiple Objects but all Object will Retuen Same Value.
School.Schoolname = "SARC"
'Object of
Class is Allowed as Does't have Private Constructor and it has non Shared
Function
'Dim S As New
School
'S.name =
"ARC" ' This will Throws Exceptation as Shared Type of Proeprty and
Method are not visble in Object
' Object Of
Class will not Return Shared Methods,Function AND Properties
Dim X As New School
Console.WriteLine(X.getSchoolname())
Dim X2 As New School
Console.WriteLine(X2.getSchoolname())
'All Object of Scholl Class will return Same Value which is
assigned to Shared Variable
End Sub
Private Sub
Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim X As New School
Console.WriteLine(X.getSchoolname())
'Even this will Return Same value as above. It will Across
Module/Class/Forms
End Sub
Private Sub
Button3_Click(sender As Object, e As EventArgs) Handles
Button3.Click
'SingleTone
Sample
ClassRooms.ClassRoomForStd = 10
ClassRooms.ClassRoomForDiv = "A"
'The private
constructor simply prevents client code creating an instance (which isn't
needed for
' anything).
'Dim C As New
ClassRooms 'This will Throw Error As PRIVATE constructor Defined in Class
Dim C As ClassRooms
C = ClassRooms.GetInstance
C.DoSomething() 'Both method will Return Same Value. As its Point to same Instance
Dim D As ClassRooms
D = ClassRooms.GetInstance
D.DoSomething() 'Both method
will Return Same Value. As its Point to same Instance
End Sub
End Class
Public Interface ITest
Sub DoSomething()
End Interface
'//
Shared/Static Class
Public NotInheritable Class Student
'Implements
ITest ' Shared Class Can't Have
Interface or Inheritance POssible
Public Shared Property Name As String
Public Shared Property Age As Integer
'This will
Throw Error
'Public Shared
Sub DoSomething() Implements ITest.DoSomething
Public Shared Sub DoSomething()
Console.WriteLine("Name :- " & Name & " - Age " + Age.ToString)
End Sub
Public Shared Function GetSomething(Value) As String
Console.WriteLine(Value)
Return "Get Something"
End Function
'IF this
Defined the Object of this Class will be Allowed
'Public Sub
Test()
' ''
'End Sub
End Class
'//
Shared/Static Class
Public Class School
Public Shared
Schoolname As String
Public Function getSchoolname() As String
Return Schoolname
End Function
End Class
'//
Singleton Class
Public Class ClassRooms
Implements ITest ' Singleton Class will Allow to have
Inherits and Implementation
'Singelton
provides you the facility to implement the interfaces
'Singelton
pattren has wide usage specialy where you have to limit your application to
create only one instance
'just like in
many games you have only one instance
Private Shared ClassRm As ClassRooms
Public Shared Property ClassRoomForStd() As Integer
Public Shared Property ClassRoomForDiv() As String
'When Private
Construtor is Defined. Object of Class Can't have New while Defining.
Private Sub New()
'This Done to
not allow User to Create Instance of Class.
End Sub
'This is
Singleton Implemention where Object of Static or Shared Class Are created
inside and returned using object
Public Shared Function GetInstance() As ClassRooms
If IsNothing(ClassRm) = True Then
ClassRm = New ClassRooms
End If
Return ClassRm
End Function
'Implemented
Method
Public Sub
DoSomething() Implements ITest.DoSomething
Console.WriteLine("Interface Implemented
Method")
Console.WriteLine(ClassRoomForStd.ToString & "- " &
ClassRoomForDiv.ToString)
End Sub
End Class
Singleton
object stores in Heap but, static object stores in stack
We
can clone the object of Singleton but, we can not clone the static class object
Singleton
class follow the OOP(object oriented principles) but not static class
we
can implement interface with Singleton class but not with Static class.
Static
Class:-
You
cannot create the instance of static class.
Loaded
automatically by the .NET Framework common language runtime (CLR) when the
program or namespace containing the class is loaded.
Static
Class cannot have constructor.
We
cannot pass the static class to method.
We
cannot inherit Static class to another Static class in C#.
A class
having all static methods.
Better
performance (static methods are bonded on compile time)
Singleton:-
You
can create one instance of the object and reuse it.
Singleton
instance is created for the first time when the user requested.
Singleton
class can have constructor.
You
can create the object of singleton class and pass it to method.
Singleton
class does not say any restriction of Inheritance.
We
can dispose the objects of a singleton class but not of static class.
Methods
can be overridden.
Can
be lazy loaded when need (static classes are always loaded).
We
can implement interface(static class can not implement interface).
Comments
Post a Comment