If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

' 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