Solved

Public Variable Not Always Updated in Class Object

Posted on 2014-03-13
6
386 Views
Last Modified: 2014-03-15
Unfortunately, I need to use some Public Byte variables which serve as switch settings for certain functions in an OOP application.   There are also some Public Integer variables used to denote numbers of things.  

Question is: I have observed that within a class (instantiated object), that the Public variable could be changed (ByRef used in the Constructor) within a method of a class, but when I leave the class the Public variable is not updated to the values within the class, and takes on the old value before instantiating and going into the class.

Therefore,

1. Are Public variables updated in methods of classes?
2. What about constructors, it seems that what happens in New() is special as well(?)

The time has come for me to understand the rules regarding where Public variables are actually updated.
0
Comment
Question by:lep1
  • 2
  • 2
  • 2
6 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 39928283
Public variables in an instantiated object will lose any changed value as the value is only changed in that instance of the object, unless it is saved back to a database or other storage medium outside the class.

If you want a Public variable to have the same value across the application you would need to use a static class that does not get instantiated, hence the same class is used by all calls to it as opposed to a new instance of the class being created when ever it is created using the new constructor.

More info about static classes here :

http://msdn.microsoft.com/en-us/library/79b3xss3.aspx
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39928318
Your question is a bit misguided. You ask, "Are Public variables updated in methods of classes?" But "public variables" where? In the same class? In a different class? In a module? You ask, "What about constructors, it seems that what happens in New() is special as well(?)" The only thing special about a constructor is that it creates a new instance of a class. That's it. We either need more detail or we need to see the code in question. What you have stated right now doesn't make the issue very clear.
0
 

Author Comment

by:lep1
ID: 39929133
"Public" refers to a public variable defined in a module.  I observed that such a variable can be set to a value prior to being used in the method of an instantiated object (class), and can take on different values within such a method, but when leaving the class this public variable is reset to the original value it had before being updated in the class method.  

Question is, why aren't the new values of the public variable (defined in a module) that are set inside the method not retained upon leaving the method?  Basically, under the current structure VB.NET will let you use but ignores updating of a project-wide public variable (defined as public in a module) that is changed in a class method.
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39930008
What are the types of the variables in question?  Integer? Boolean? String? A class?
0
 

Author Comment

by:lep1
ID: 39930012
Integer
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39931015
Please post your code of the module and class.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
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…

830 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