Solved

shared properties vs shared instance variables

Posted on 2011-03-17
7
658 Views
Last Modified: 2012-05-11
If I have a class

public myClass {

    private shared m_intCnt as integer

   public shared property Count as integer
      get
            return(m_intCnt)
      end get
       set(byVal value as integer)
            m_intCnt = value
       end set
     end Property
end class


Is the above the same as if I just did this and didn't use a property at all?

public shared Count as integer

I'm trying to create a variable that can used across multiple threads.
0
Comment
Question by:rutledgj
7 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 75 total points
ID: 35159838
There is no such thing as a "shared instance variable". If a variable is shared, it is a class-level variable and can be used by any instance of the class.
0
 

Author Comment

by:rutledgj
ID: 35160087
I can do either of the above in a class. The question is what is the difference?
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 175 total points
ID: 35160163
As a best practice, all fields should be private to the class.  So, you should favor a shared property over a shared field.  (http://msdn.microsoft.com/en-us/library/ms229057.aspx)

Below is a shorter version of the code you posted, which uses auto-implemented properties (http://msdn.microsoft.com/en-us/library/dd293589.aspx)

public myClass

   public shared property Count as integer

end class

Open in new window


I hope this helps.

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35160179
Well, in VB you can't have a property without a backing variable to hold data (unlike C#'s automatic properties which create the backing variable for you). Because you defined the property as Shared, so too must the variable it accesses. Shared members don't have access to instance members.

This is not to say you can't have a shared variable by itself (i.e. with no attached property). A shared variable can exist on its own. You would access it the same way you would a property; you would just be working with the variable directly. Properties in .NET are just fancy ways of using getter and setter methods. In reality, the runtime creates hidden getter and setter methods for any property you create. With this in mind, you should be able to understand why the backing variable is needed.

The concept of "shared" is just like it sounds--this member is accessible to all instances of the class--and some other things too. Whether you use a property (with a backing variable) or a variable alone is a question of design choice. OO best practices call for data hiding, and so having the property with a backing variable would be the preferred method.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35160191
@wdosanjos

Such a concept only exists in .NET 4.0. Pre-4.0 did not have automatic properties (C# did/does though).
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35160198
P.S.

I wasn't aware 4.0 had introduced the concept, so that is the reason for the discrepancy in my two statements above.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35160473
What's the difference?...with a PROPERTY you have the ability to enforce rules upon it.  If the variable is simply a public member than any value can be assigned to it.  Whether this matters is completely dependent on the project in question...
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question