Solved

Public Variable Not Always Updated in Class Object

Posted on 2014-03-13
6
382 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 74

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 74

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

12 Experts available now in Live!

Get 1:1 Help Now