473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

scope and object assignment

Hi.

I just had success doing something that I thought should fail.

Well maybe I did. I'm not sure.

In short, I assigned a locally declared object to a module level object
and the module level object retained the values of the local even after
the local went out of scope.

I was expecting the module level object to have a reference to an
object on the stack, which in my old C programming days would have
killed the program.

Does anyone know why this apparent copying of an object worked?

Public class Aaa
Private m_MyObjects as New MyObjects ' collection of MyObject
Private m_MyObject as MyObject '

'
' lots of code snipped
'

Private Sub Example(ByVal testID As Integer)
For each myObject as MyObject in m_MyObjects
If myObject.TestID = testID Then
m_MyObject = myObject
Exit For
End if
Next
End Sub

End Class

Apr 28 '06 #1
2 896

James wrote:
Hi.

I just had success doing something that I thought should fail.

Well maybe I did. I'm not sure.

In short, I assigned a locally declared object to a module level object
and the module level object retained the values of the local even after
the local went out of scope.

I was expecting the module level object to have a reference to an
object on the stack, which in my old C programming days would have
killed the program.

Does anyone know why this apparent copying of an object worked?


Because the object is not created on the stack. The object variable is
on the stack - but the actual object lives on the heap. So, when you
make the assignment you are copying the address of the object, not the
object.

HTH

--
Tom Shelton [MVP]

Apr 28 '06 #2
That's good enough for me.

Thanks Tom.

Apr 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
6
by: Jackson A. Marshall | last post by:
I have a been testing the try/catch exception mechanism (as implemented by DigitalMars) and found that if I throw an object its dtor gets called 3 times, if I throw a reference its dtor gets...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
15
by: Paddy | last post by:
Hi, I am trying to work out why I get UnboundLocalError when accessing an int from a function where the int is at the global scope, without explicitly declaring it as global but not when accessing...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
2
by: Nitro | last post by:
Hello, today I wrote this piece of code and I am wondering why it does not work the way I expect it to work. Here's the code: y = 0 def func(): y += 3 func()
7
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
3
by: John Dann | last post by:
Trying to learn Python here, but getting tangled up with variable scope across functions, modules etc and associated problems. Can anyone advise please? Learning project is a GUI-based...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.