Solved

shared properties vs shared instance variables

Posted on 2011-03-17
7
656 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 74

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 74

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 74

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 74

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now