Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Public Variable Not Always Updated in Class Object

Posted on 2014-03-13
6
Medium Priority
?
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 2000 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview

610 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