Solved

Public Variable Not Always Updated in Class Object

Posted on 2014-03-13
6
385 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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