Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

shared properties vs shared instance variables

Posted on 2011-03-17
7
Medium Priority
?
663 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 300 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 700 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

604 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