How To Do It - Shared Class Variables Part IV
This is the last of my how to's, yes Jeff, Rob, Kathleen, et al, on
the use of shared with class variables, methods, properties, and
constructors.
Consider the following program:
Imports System.Console
Class TestClass1
' NOTE 1
'Public Shared Sub New()
'End Sub
' NOTE 2
'Overloads Shared Sub New()
'End Sub
'Overloads Shared Sub New (ByVal intArg As Integer)
'End sub
' NOTE 3
Shared Sub New()
WriteLine("New TestClass1")
End Sub
' NOTE 4
Public Overloads Shared Sub MyProc1()
End Sub
Public Overloads Shared Sub MyProc1(ByVal intArg As Integer)
End Sub
Public Overloads Shared Sub MyProc1(ByVal lngArg As Long)
End Sub
End Class
Class TestClass2
' NOTE 5
Public Overloads Sub new(ByVal lngArg As Long)
WriteLine("New TestClass2(lngArg)")
End Sub
' NOTE 6
Public Overloads Sub new(ByVal intArg As Integer)
WriteLine("New TestClass2(intArg)")
End Sub
End Class
Module Module1
Sub Main()
' NOTE 7
Dim TheClass1 As New TestClass1()
' NOTE 8
' Dim TheClass2 As New TestClass2()
' NOTE 9
Dim TheClass3 As New TestClass2(6)
' NOTE 10
Dim TheClass4 As New TestClass2(6L)
Dim TheClass5 As New TestClass2(CLng(6))
' NOTE 11
' TheClass3.New(7L)
End Sub
End Module
' NOTE 1
' A shared constructor cannot use the 'Public' specifier (error from
' the VB compiler)
' NOTE 2
' Cannot overload a constructor which is marked as 'Shared' (error
' from the VB compiler)
' NOTE 3
' A shared constructor cannot have any arguments (error from VB compiler)
' NOTE 4
' Shared methods may be overloaded but each must have a different
' parameter signature. ByRef vs ByVal does not constitute a
' difference.
' NOTE 5
' Non-shared constructors may be public. (see NOTE 1)
' NOTE 6
' Constructors may be overloaded but each must have a different
' parameter signature.
' NOTE 7
' When instantiating a class the uses a shared constructor, parameters
' are not permitted. (see NOTE 3)
' NOTE 8
' When instantiating a class with overloaded constructors, one of the
' overload's parameter signature must match the instantiating statement.
' In this case no parameterless signature is available.
' NOTE 9
' When instantiating a class with overloaded constructors, one of the
' overload's parameter signature must match the instantiating statement.
' In this case there appears to be 2 separate constructors that would
' work. Appearances are deceptive. The 6 argument is treated as an Integer.
' NOTE 10
' To invoke the other constructor the 6 must indicate that it is a long by
' using 6L or CLng(6) as the argument.
' NOTE 11
' A direct call to a constructor is allowd only as the first statment in
a
' constructor (error from VB Compiler). Even though the constructor is
a
' public method of the class it may not be directly invoked.
Output of this program is:
New TestClass1
New TestClass2(intArg)
New TestClass2(lngArg)
New TestClass2(lngArg)
Re: How To Do It - Shared Class Variables Part IV
Patrick,
Lots of good notes. But I here is more clarification on the difference
between a shared constructor and a normal one.
The shared constructor is called the first time anything touches the class.
The only way I know this will happen is instantiating an object of the
class, calling any shared method of the class, or touching an inherited
class (instantiation or calling a shared method).
The shared constructor call is implicit. This is why it must be private, and
it can not have any arguments. If arguments are required for initialization
of the class, then a separate shared method should be called by the
non-shared constructor. Note that I think there are times that a shared
constructor will be called that a non-shared constructor will not.
A class can, and often will, have a shared and a normal constructor. Let me
rephrase, because I don't think shared constructors will be all that common.
In classes that have a shared constructor, there will often also be a
non-shared constructor. In this case the shared constructor is called first.
--
Kathleen
(MS-MVP)
Reply in the newsgroup so everyone can benefit
--
Re: How To Do It - Shared Class Variables Part IV
Kathleen,
"Kathleen Dollard-Joeris" <kjoeris@noemailplease.com> wrote in message
news:3af55f0a@news.devx.com...
> The shared constructor is called the first time anything touches the
class.
> The only way I know this will happen is instantiating an object of the
> class, calling any shared method of the class, or touching an inherited
> class (instantiation or calling a shared method).
Your description is a bit inaccurate, all that the CLR specifications
require is that the shared constructor is executed *before* any access to
the class. However the specific time it takes place is implementation
defined (that is, differing implementations of the CLR could do this
differently.) It could happen anytime between assembly-load time and
accessing the class.
> Note that I think there are times that a shared
> constructor will be called that a non-shared constructor will not.
Certainly. If, for example, you access a static member of a class without
ever constructing an instance of that class.
> A class can, and often will, have a shared and a normal constructor. Let
me
> rephrase, because I don't think shared constructors will be all that
common.
> In classes that have a shared constructor, there will often also be a
> non-shared constructor. In this case the shared constructor is called
first.
I'd disagree, though we may be talking about slightly different things,
shared constructors will likely be common (though they may be implicitly
generated by the compiler for you, in the majority of cases.) If your
class/module/structure has shared data members that are not
zero-initialized, than it has a class-constructor (shared constructor,)
either provided explicitly or generated implicitly.
Re: How To Do It - Shared Class Variables Part IV
To those reading these discussions between Jeff, Kathleen, and myself
I have added some additional examples that are appropriate for the
discusstion of shared & non shared constructors. Consider the following
code with the output:
Shared New TestClass5A
Public New TestClass5A
Shared New TestClass5B
Public New TestClass5B 1
Shared New TestClass5C
Public New TestClass5C 9
Shared New TestClass5D
Public Property Set TestClass5D 5
Shared New TestClass5E
Public Overloads New TestClass5F int = 8
Shared New TestClass5G
Public Overloads New TestClass5G int = 3
CODE:
Imports System.Console
Class TestClass5A
Shared Sub New()
WriteLine("Shared New TestClass5A")
End Sub
Public Sub New()
WriteLine("Public New TestClass5A")
End Sub
End Class
Class TestClass5B
Shared Sub New()
WriteLine("Shared New TestClass5B")
End Sub
Public Sub New(ByVal intArg As Integer)
WriteLine("Public New TestClass5B {0}", intArg)
End Sub
End Class
Class TestClass5C
Shared Sub New()
WriteLine("Shared New TestClass5C")
End Sub
Public Shared Sub MyProc(ByVal intArg As Integer)
WriteLine("Public New TestClass5C {0}", intArg)
End Sub
End Class
Class TestClass5D
Shared Sub New()
WriteLine("Shared New TestClass5D")
End Sub
Public Shared Property intArg() As Integer
Get
End Get
Set
WriteLine("Public Property Set TestClass5D {0}", Value)
End Set
End Property
End Class
Class TestClass5E
Shared Sub New()
WriteLine("Shared New TestClass5E")
End Sub
Public Shared intArg As Integer
End Class
Class TestClass5F
Public Overloads Sub New()
WriteLine("Public Overloads New TestClass5F")
End Sub
Public Overloads Sub New(ByVal intArg As Integer)
WriteLine("Public Overloads New TestClass5F int = {0}", intArg)
End Sub
End Class
Class TestClass5G
Shared Sub New()
WriteLine("Shared New TestClass5G")
End Sub
Public Overloads Sub New()
WriteLine("Public New TestClass5G")
End Sub
Public Overloads Sub new(ByVal intArg As Integer)
WriteLine("Public Overloads New TestClass5G int = {0}", intArg)
End Sub
End Class
Module Module1
Sub Main()
' NOTE 1
Dim T As New TestClass5A()
' NOTE 2
Dim X As New TestClass5B(1)
' NOTE 3
TestClass5C.MyProc(9)
' NOTE 4
TestClass5D.intArg = 5
' NOTE 5
TestClass5E.intArg = 8
' NOTE 6
Dim Y As New TestClass5F(8)
' NOTE 7
Dim Z As New TestClass5G(3)
End Sub
End Module
' NOTE 1
' Class TestClass5A has a shared and nonshared constructor both
' with the same signature.
' NOTE 2
' Class TestClass5B has a shared and nonshared constructor each
' with differenct signatures.
' NOTE 3
' Class TestClass5C has a shared constructor and a shared method
' This case was covered in a previous How To but shown again for
' completeness of the discussion.
' NOTE 4
' Class TestClass5D has a shared constructor and a shared property.
' This case was covered in a previous How To but shown again for
' completeness of the discussion.
' NOTE 5
' Class TestClass5E has a shared constructor and a shared public
' variable. This case was covered in a previous How To but shown
' again for completeness of the discussion.
' NOTE 6
' Class TestClass5F has 2 nonshared constructors each with unique
' signatures. Note that the overloads is required on the constructors.
' NOTE 7
' Class TestClass5G has a shared constructor and 2 nonshared constructors.
' Note that the overloads is required on the nonshared constructors.