Solved

shared properties vs shared instance variables

Posted on 2011-03-17
7
660 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 86

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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