Re: Structures not behaving as value types
Peter Obiefuna <[email protected]> Mon, 25 Feb 2008 11:54:51 -0700
| Newsgroups | gmane.comp.windows.devel.dotnet.clr |
|---|---|
| Message-ID | <[email protected]> |
>> So each request for another mask is obviously getting the same object >> reference If posted the code you keep describing, fewer persons would have to shoot in the wild. Anyhow, object instances do not share variables just because they are reference types. They do because they are so defined by attribution. At least copy and post here I) the line that declares your "... one array of N 32-bit integers ...", and ii) the constructor of your structure and the declaration of local variables used by your constructor. P -------------------------------------------------- From: "Richard Kucia" <[email protected]> Sent: Monday, February 25, 2008 11:44 AM To: <[email protected]> Subject: Re: [DOTNET-CLR] Structures not behaving as value types > Well, in fact it does. The mask structure contains one array of N 32-bit > integers to hold the bits. Each time any function or property of the mask > is invoked, that code tests the array to see if it is (nothing), and > Redim's it. > > So that explains it. The formula mentioned below takes the most recently > used mask structure and shifts it left 1 bit. So each request for another > mask is obviously getting the same object reference, and when the > bit-shift occurs, it effectively occurs in every mask structure, since > they are all referencing the same array. > > So what is the correct way to handle this? I want each structure to have > its own independent array of N 32-bit integers. > > Rick > -------------- Original message from Matthew Wills > <[email protected]>: -------------- > > >> Richard, >> >> Show us the code for strFieldMask. I would bet you are using some >> reference >> types in it. >> >> Seeya >> Matthew Wills | Solution Designer | Adviser Tools and Services | >> Financial >> Planning and Third Party | NAB Technology >> >> Phone: 02 9376 4029 | Mobile: 0435 002 520 | Email: >> [email protected] >> >> >> >> >> Richard Kucia >> >> To >> [email protected] >> cc >> >> Subject >> [DOTNET-CLR] Structures not >> behaving as value types >> >> I have a VB.Net declaration of a structure strFieldMask. The intended >> purpose is to store a bunch of bits, like a bit vector. The structure >> also >> has operator declarations for =, <>, And, Or, Not and <<, plus some other >> methods for bit manipulation. >> >> The problem is that variables of type strFieldMask seem to behave as >> though there was a singleton strFieldMask and all instances shared it. >> >> Consider this: >> >> Dim Mask1 as strFieldMask >> Dim Mask2 as strFieldMask >> >> Mask1 = (formula for computing the next unused mask value) >> Debug.WriteLine(Mask1.ToString)) >> Mask2 = (same formula) >> Debug.WriteLine(Mask1.ToString)) ' Yes, Mask1, not Mask2 >> >> The result from the first WriteLine might be "0x10" and the result from >> the second WriteLine might be "0x20". >> >> I've successfully used structures before, but this is the first time I've >> tried overloading the operators. Has that caused the behavior I'm seeing? >> >> Thanks. >> >> >> >> National Australia Bank Ltd - ABN 12 004 044 937 >> This email may contain confidential information. If you are not the >> intended >> recipient, please immediately notify us at [email protected] or by >> replying >> to >> the sender, and then destroy all copies of this email. Except where this >> email >> indicates otherwise, views expressed in this email are those of the >> sender and >> not >> of National Australia Bank Ltd. Advice in this email does not take >> account of >> your >> objectives, financial situation, or needs. It is important for you to >> consider >> these >> matters and, if the e-mail refers to a product(s), you should read the >> relevant >> Product Disclosure Statement(s)/other disclosure document(s) before >> making any >> decisions. If you do not want email marketing from us in future, forward >> this >> email >> with "unsubscribe" in the subject line to [email protected] in >> order to >> stop marketing emails from this sender. National Australia Bank Ltd does >> not >> represent that this email is free of errors, viruses or interference. >> >> =================================== >> This list is hosted by DevelopMentor® http://www.develop.com >> >> View archives and manage your subscription(s) at >> http://discuss.develop.com > > =================================== > This list is hosted by DevelopMentor® http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com