473,714 Members | 2,545 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 2323
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.co mwrote in message
news:O1******** ******@TK2MSFTN GP04.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("S cripting.Dictio nary")
'x contains (for instance) the number 536474
'which is a memory address that points to the dictionary object

set z = CreateObject("S cripting.Dictio nary")
'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("S cripting.Dictio nary")

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.goo glegroups.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
3588
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 respectively In the sample code you see that I have class Item and class Dict class Dict contains a dictionary called items. The items dictionary will contain instances of Item that are keyed off of the Item name. In __main__ I create two...
8
5587
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 stuffing quite a lot of data (from and MS SQL database) into them. On Windows 2000 server, everything is fine. If the data structures get really big it slows down, but for normal operation it's no problem. Recently our hosting provider moved to...
26
4059
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 the dictionary object. After some tinkering, I whittled it down to the following (complete) ASP... <%@ CodePage=65001 Language="VBScript"%>
2
3420
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 to miss a few things including #endregion directive and the ending class } Is there not a simple way like in dotnet vb? I managed to get the sample to code to this:
1
9253
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 DataBinding accepts as a data source either an IList or an IListSource at System.Windows.Forms.ListControl.set_DataSource(Object value)
4
14751
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 can change the value of the property through the dictionary. I am having difficulty making the value be by reference. Is this possible? I've even tried unsuccessfully to use unsafe code with pointers.
3
4183
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
10176
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 are 2 parts, I don't know how to setup the get/sets. I tried searching but could not find any examples. I'd appreciate an example of how to get/ set the two parts of a dictionary via a property of a class. tia
2
3136
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(); StockContract.Dictionary = ContractDictionary<string, string>(); GridView1.DataSource=StockContract.Dictionary; So far so good. Now I assign something to the Dictionary collection through some textboxes and a button:
0
8795
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9068
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9009
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4462
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2103
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.