473,498 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dictionary object

I have a dictionary object, then I create a new dictionary object and sets
it equal to my original, then I pass the new dictionary object to a function
that changes some of my values - but then my original dictionary also gets
changed and that was not the intention, can someone explain to me why it
behaves that way and how do I avoid it, så I van have different dictionary
objects?

Thanks Betina
Jul 28 '06 #1
4 2305
Betina Andersen wrote:
I have a dictionary object, then I create a new dictionary object and sets
it equal to my original, then I pass the new dictionary object to a function
that changes some of my values - but then my original dictionary also gets
changed and that was not the intention, can someone explain to me why it
behaves that way and how do I avoid it, så I van have different dictionary
objects?
Looks like it's because the argument is passed to the function ByRef,
meaning that instead of passing 'the thing' into the function you just
get a reference to 'the thing'. So any changes you make to 'the thing'
will be reflected in the original.

I haven't used ASP for a long time, but you need to change the argument
to be ByVal, which means you get a *copy* of 'the thing', like in the
following code:

<%
function monkey(byval thing)
monkey = thing & "<br/>"
thing = thing + 1
end function

dim a
a = 1

response.write monkey(a)
response.write a
%>

You should see two 1s if it's worked as expected. Take the 'byval'
away and you get 1 and 2, because the argument is Byref again and the
function was able to monkey around with the original variable.

Jul 28 '06 #2

"Betina Andersen" <by*@invalid.comwrote in message
news:O1**************@TK2MSFTNGP04.phx.gbl...
I have a dictionary object, then I create a new dictionary object and sets
it equal to my original, then I pass the new dictionary object to a
function
that changes some of my values - but then my original dictionary also gets
changed and that was not the intention, can someone explain to me why it
behaves that way and how do I avoid it, så I van have different dictionary
objects?
My very old copy of MSDN says...

"Generally, when you use Set to assign an object reference to a variable, no
copy of the object is created for that variable. Instead, a reference to the
object is created. More than one object variable can refer to the same
object. Because these variables are references to (rather than copies of)
the object, any change in the object is reflected in all variables that
refer to it."

I would guess that the following occurs...

dim x
dim z

set x = CreateObject("Scripting.Dictionary")
'x contains (for instance) the number 536474
'which is a memory address that points to the dictionary object

set z = CreateObject("Scripting.Dictionary")
'z contains a pointer to a different object e.g. the memory address 73462

set z = x
'z now contains 536474, and so points to the original object - same as x
I think you need something like this...

set z = copydict(x)

function copydict(x)
dim j, k, i, z

set z = CreateObject("Scripting.Dictionary")

k = x.Keys
i = x.Items
for j = 0 to x.Count - 1
z.Add k(j), i(j)
next
set copydict = z
end function
--
roger
Jul 29 '06 #3

roger wrote:
"Generally, when you use Set to assign an object reference to a variable, no
copy of the object is created for that variable. Instead, a reference to the
object is created. More than one object variable can refer to the same
object. Because these variables are references to (rather than copies of)
the object, any change in the object is reflected in all variables that
refer to it."
I guess that's why these things have .clone() methods (see ADO
recordset). Except in this case, where it would be useful. ;)

Jul 31 '06 #4

"Bobbo" <ro******@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
>
roger wrote:
"Generally, when you use Set to assign an object reference to a
variable, no
copy of the object is created for that variable. Instead, a reference to
the
object is created. More than one object variable can refer to the same
object. Because these variables are references to (rather than copies
of)
the object, any change in the object is reflected in all variables that
refer to it."

I guess that's why these things have .clone() methods (see ADO
recordset). Except in this case, where it would be useful. ;)
I've not seen any objects in common use that have clone method of the type
desired by the OP.

The clone method of a recordset allow a new filter and seek position to be
created independant of the orginal recordset object. However the underlying
data remains the same if you change the data in one the changes are visible
in the other.


Aug 1 '06 #5

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

Similar topics

1
3572
by: none | last post by:
or is it just me? I am having a problem with using a dictionary as an attribute of a class. This happens in python 1.5.2 and 2.2.2 which I am accessing through pythonwin builds 150 and 148...
8
5558
by: Rodd Snook | last post by:
I have an application which makes extensive use of the Scripting.Dictionary object. I'm not doing anything silly like putting them outside the page scope -- just creating quite a few of them and...
26
4019
by: Alan Silver | last post by:
Hello, I have a server running Windows Server 2003, on which two of the web sites use the MegaBBS ASP forum software. Both sites suddenly developed the same error, which seems to be connected to...
2
3406
by: jg | last post by:
I was trying to get custom dictionary class that can store generic or string; So I started with the example given by the visual studio 2005 c# online help for simpledictionay object That seem...
1
9237
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
4
14655
by: NullQwerty | last post by:
Hi folks, I have a Dictionary which contains a string key and an object value. I want the object value to point to a property in my class and I want it to be by reference, so that later on I...
3
4161
by: wildThought | last post by:
If I have an object that contains a generic dictionary inside of it, how do I get access to its properties such as count?
6
10153
by: GiJeet | last post by:
hello, I'm trying to use a dictionary as a class member. I want to use a property to get/set the key/value of the dictionary but I'm confused as how to use a dictionary as a property. Since there...
2
3113
by: Andy B | last post by:
I don't know if this is even working or not but here is the problem. I have a gridview that I databound to a dictionary<string, stringcollection: Contract StockContract = new Contract();...
0
7002
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
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.