-
How To Do It - Shared Class Variables Part II
Consider the following program:
The class definition has 2 variables, one that is shared
and one that is not. They are both public so that I would
not dirt the water for the example being analyzed. The program
produces the following output:
Value = 6
Value = 7
Value = 7
Value = 8
NonShared = 15
Imports System.Console
Class clsTestClass
Public Shared lngValue As Long = 6
Public lngNonSharedValue As Long = 15
End Class
Module Module1
Sub Main()
Dim TestClass As clsTestClass
' NOTE 1
WriteLine("Value = {0}", clsTestClass.lngValue)
clsTestClass.lngValue = 7
WriteLine("Value = {0}", clsTestClass.lngValue)
' NOTE 2
' WriteLine("Value = {0}", clsTestClass.lngNonSharedValue)
' NOTE 3
TestClass = New clsTestClass()
WriteLine("Value = {0}", TestClass.lngValue)
TestClass.lngValue = 8
WriteLine("Value = {0}", TestClass.lngValue)
' NOTE 4
WriteLine("NonShared = {0}", TestClass.lngNonSharedValue)
End Sub
End Module
NOTE 1
Don't be alarmed at the format for the WriteLine statement. Since VB.NET
and C## share the CLR you can use the same formats with either language.
If you rather, the 1st WriteLine could have been written as:
WriteLine ("Value = " & clsTestClass.lngValue.ToString())
or
WriteLine ("Value = " & CStr(clsTestClass.lngValue))
What is the important part of NOTE 1 is that I am able to refer to
public shared variables of a class by using the class definition name.
I do NOT need to instantiate a copy of the class to refer to these types
of variables. This has immediate implications for run-time initialization
of these type of variables and what every programmer wants a holding class
for global data that doesn't require methods, constructors, or destructors.
NOTE 2
The statement immediately following NOTE 2 is not permitted (VB complains,
as it should). I have attempted to access a variable that is not shared.
Non-shared variables can only be accessed from instantiated class. The
variables do not exists until instantiation. In VB.NET, instantiation
occurs with the application of the New clause to an object.
NOTE 3
I finally get around to instantiation. Now is the first time I can access
the nonshared variable of the class. As I demonstrated in my earlier
epistle "How To Do It - Shared Class Variables", you can access shared
variables by either their class definition name or instantiation name.
NOTE 4
When I reference a nonshared variable I must do so using the named object
I instantiated, in this case, TestClass.
-
Re: How To Do It - Shared Class Variables Part II
Hi Patrick,
> NOTE 3
>
> I finally get around to instantiation. Now is the first time I can access
> the nonshared variable of the class. As I demonstrated in my earlier
> epistle "How To Do It - Shared Class Variables", you can access shared
> variables by either their class definition name or instantiation name.
I think the latter (that you can access a shared member using the
reference-to-instance var) could potentially be confusing. Although often
Shared methods have one additional parameter (which takes the place of [or
part of] the instance data), it still appears that I'm operating on the
instance, which is not the case.
As it's not hard to use the class name instead of the reference, I'd prefer
not having this option.
Regards,
Gregor
-
Re: How To Do It - Shared Class Variables Part II
Gregor,
I understand the possibility for the confusion. Such is the nature of
VB.NET and C#. As to the suitability of the usage that is left to the
programmer. I, as a passed compiler writer, find such characteristics
interesting and amusing. I'll pass on more capabilities when I post
shared methods and shared constructors. For me, the best usage of
public static variables is a single global variable holding tank such as
class GobalData
.. details left to programmer
end class
So all my global data is "protected" ha! in a class. Stay tuned!
"Gregor R. Peisker" <gregor@peisker.de> wrote:
>Hi Patrick,
>
>> NOTE 3
>>
>> I finally get around to instantiation. Now is the first time I can access
>> the nonshared variable of the class. As I demonstrated in my earlier
>> epistle "How To Do It - Shared Class Variables", you can access shared
>> variables by either their class definition name or instantiation name.
>
>I think the latter (that you can access a shared member using the
>reference-to-instance var) could potentially be confusing. Although often
>Shared methods have one additional parameter (which takes the place of [or
>part of] the instance data), it still appears that I'm operating on the
>instance, which is not the case.
>
>As it's not hard to use the class name instead of the reference, I'd prefer
>not having this option.
>
>Regards,
>Gregor
>
>
-
Re: How To Do It - Shared Class Variables Part II
Hi Patrick,
> I understand the possibility for the confusion. Such is the nature of
> VB.NET and C#.
It looks like a syntax detail to me, not nature.
> As to the suitability of the usage that is left to the
> programmer. I, as a passed compiler writer, find such characteristics
> interesting and amusing. I'll pass on more capabilities when I post
> shared methods and shared constructors. For me, the best usage of
> public static variables is a single global variable holding tank such as
>
> class GobalData
> .. details left to programmer
> end class
>
> So all my global data is "protected" ha! in a class. Stay tuned!
So this is the OO way of global variables? :-)
I use Shared data for things like an instance count, or for private data of
singleton "objects". Sure, I have a need for some global data, but mostly, I
bundle them with some functionality as well, so I don't view singletons as
variable tanks. But I guess this depends on the design.
Regards,
Gregor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks