|
-
May 12th, 2004, 03:25 PM
#1
Thread Starter
Lively Member
Public Shared, Private Shared, and memory consumption.
Can someone explain to me the difference between both? What's the point of declaring something Private Shared instead of just Private, or even Public Shared instead of just Public? What exactly does "Shared" do, and how does it affect memory consumption? And what's up with using Private instead of Dim to declare variables outside of a method? Isn't that basically the same thing?
So many questions, such little time,
KT
-
May 12th, 2004, 03:32 PM
#2
Thread Starter
Lively Member
I found some more information regarding this from the MSDN documentation article titled "Lifetime."
Different Lifetimes
A variable declared at module level typically exists for the entire time your application is running. A nonshared variable declared in a class or structure exists as a separate copy for each instance of the class or structure in which it is declared; each such variable has the same lifetime as its instance. However, a Shared variable has only a single lifetime, which lasts for the entire time your application is running.
Okay, so basically a shared variable is kept in memory for the entire time the application is running? Even if the instance is disposed? Okay, now that's great if you have only one instance of the method running at a time, but what if two instances try to modify the variable at the exact same time?
-
May 12th, 2004, 04:11 PM
#3
That really can't happen, since only one thing can happen at a time. The instance that get there first will change it, then the next instance to get there will be using the new value.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
May 12th, 2004, 04:23 PM
#4
Sleep mode
In more general term , Shared (static) variable is not specific to a class or instance , on the other hand , instance variable is specific to a class or instance .
-
May 12th, 2004, 06:18 PM
#5
Thread Starter
Lively Member
Ok. Let's say with Shared methods for example, can I use a shared method without declaring a new instance of the class it's in? No need for New, just do:
object = class.sharedfunction(params)
?
Whaaaat.
-
May 12th, 2004, 08:45 PM
#6
Sleep mode
Originally posted by Kt3
Ok. Let's say with Shared methods for example, can I use a shared method without declaring a new instance of the class it's in? No need for New, just do:
object = class.sharedfunction(params)
?
Whaaaat.
Yes , just provide the full name of the method like this (ClassName.SharedMemberMethod) so no need for instantiation .
-
May 13th, 2004, 10:00 AM
#7
Thread Starter
Lively Member
Originally posted by Pirate
Yes , just provide the full name of the method like this (ClassName.SharedMemberMethod) so no need for instantiation .
Amazing.. Does this help with memory usage since I don't need to declare an instance of the class?
-
May 13th, 2004, 12:13 PM
#8
Sleep mode
Originally posted by Kt3
Amazing.. Does this help with memory usage since I don't need to declare an instance of the class?
I would imagine , yes but it shouldn't be a big deal as OOP concept which is more important in consructing your classes .
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
|
Click Here to Expand Forum to Full Width
|