Hi,
I'm creating a struct hat contains an array. Unfortunatly, all arrays are
reference types, which means that when someone copies/assigns it, they have a
reference to the orginal object, which defies the point of creating a struct!
And to top it off, my array contains reference objects!
If anyone has any ideas could they please tell me?
Reuben 3 995
Reuben <Re****@discussions.microsoft.com> wrote: I'm creating a struct hat contains an array. Unfortunatly, all arrays are reference types, which means that when someone copies/assigns it, they have a reference to the orginal object, which defies the point of creating a struct! And to top it off, my array contains reference objects! If anyone has any ideas could they please tell me?
Well, what are you trying to achieve? A deep copy? I'd just provide a
method which creates a deep copy. You're unlikely to want to start
creating a deep copy *every* time the value is assigned though, I'd
have thought.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
My problem is that when you assign a value type to a variable, modifications
on the original shouldn't affect the copy. However, because the both copies
contain the same reference to the same array, changes on one affect the
other, like you would expect from a reference type. I need a way to implement
this for the assignment operator (=) so that my type behaves in a manner
appropiate to value types.
I hope this clears that up.
Reuben.
Reuben <Re****@discussions.microsoft.com> wrote: My problem is that when you assign a value type to a variable, modifications on the original shouldn't affect the copy.
Well, that depends on the members. If they're reference type members,
I'd expect the modifications to affect the copy, because the copy is a
copy of the references.
However, because the both copies contain the same reference to the same array, changes on one affect the other, like you would expect from a reference type. I need a way to implement this for the assignment operator (=) so that my type behaves in a manner appropiate to value types.
You can't.
You might want to consider making your type immutable instead, so that
a new copy is created any time you want to make a change. You could
then make it a reference type instead. String is built this way, by the
way.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Abe Frohnman |
last post by:
Hello all,
I'm passing a reference to a class into the constructor of a form, like
so: public MyForm(int count, ref Area myArea) {...}
How can I use myArea outside the constructor? Should I...
|
by: Javier Campos |
last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-)
I can reformat a non-HTML post if you want me to (and...
|
by: daniel |
last post by:
This is a pretty basic-level question, but I'd really like to know, so
thanks for any help or pointers you can provide (like what I would
google for ;o)
Suppose:
<code>
myFunc()
{
|
by: Maxim |
last post by:
Hi!
A have a string variable (which is a reference type).
Now I define my Method like that:
void MakeFullName(string sNamePrivate)
{
sNamePrivate+="Gates"
}
|
by: Andrew Bullock |
last post by:
Hi,
I have two classes, A and B,
B takes an A as an argument in its constructor:
A a1 = new A();
B b = new B(a1);
|
by: Edward Diener |
last post by:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference
to a reference in C#. I know one can use it on a value type to create a
reference to that value.
|
by: Terry |
last post by:
I am getting the following warning for the below function. I understand what
it means but how do I handle a null reference? Then how do I pass the
resulting value?
Regards
Warning 1...
|
by: Robert Dailey |
last post by:
Hi,
I noticed in Python all function parameters seem to be passed by
reference. This means that when I modify the value of a variable of a
function, the value of the variable externally from the...
|
by: az.anonymous |
last post by:
Im starting to learn C#, and I made a simple stack class. Inside the
stack class I had the following:
class StackElement
{
object info;
StackElement below;
}
Everything works fine cause...
|
by: Astley Le Jasper |
last post by:
Sorry for the numpty question ...
How do you find the reference name of an object?
So if i have this
bob = modulename.objectname()
how do i find that the name is 'bob'
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |