Re: copying objects
- From: "Mike" <vimakefile@xxxxxxxxx>
- Date: Sat, 3 Sep 2005 17:29:53 -0700
> currVal and newVal will be simple types (ie. long, string, date...)
If they are all value types and immutable as you state, then there is no
reason to copy them, as they have no state and cannot change between
commit() and reset(). (Unless I'm not understanding what you're saying.)
If they aren't immutable, then you could copy via:
(a) ICloneable
(b) Refection calls
(c) A switch of known Types/Classes
(d) probably other ways
m
"Bob Weiner" <bob@xxxxxxxxxxxxxx> wrote in message
news:O1ZbGCMsFHA.1132@xxxxxxxxxxxxxxxxxxxxxxx
> It appears this solution only moves the problem from my current methods
> (commit and reset) into another method named Clone().
>
> currVal and newVal will be simple types (ie. long, string, date...) though
> that will be defined in whatever class derives from this one. If I knew
> the type I could unbox it, copy the value, then box it back up. Since I
> don't know the type - nor do I want to unnesessarily constrain future
> types - this isn't possible.
>
> Is there no way to dereference the object currVal, copy whatever is found
> into the space referenced by newVal?
>
>
>
>
> "Richard" <Richard@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:3105F832-AAC6-486A-85BF-B18CAF4ABF5A@xxxxxxxxxxxxxxxx
>>
>> If you want "copy by value" aka "deep copy" semantincs implement
>> ICloneable
>> on your objects:
>>
>> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemicloneableclasstopic.asp
>>
>> Somewhere in MSDN there is a good article about proper implementation of
>> a
>> C# class that can do a deep copy - sorry but I couldn't find it for you.
>>
>> It's kind of a pain to implement if I remember correctly because you're
>> supposed to implement ICloneable, define your .Clone() method with
>> specific
>> behaviors, and then make a static form of an equality operator to test if
>> two
>> objects are equal. -- If you do all of those steps then in code you
>> write:
>> currVal = newVal.Clone()...
>>
>>
>>
>>
>> "Bob Weiner" wrote:
>>
>>> This seems real simple but I don't know how to do it. Obviously, what
>>> I'm
>>> looking for isn't what I typed.
>>>
>>> I don't want the assignment operator in commit() and reset() to work by
>>> reference but System.Object doesn't have a copy or clone method
>>> available.
>>> I'm sure I'm missing something simple.
>>>
>>> public class MyProperty {
>>> private object currVal;
>>> private object newVal;
>>>
>>> public MyProperty(object initialValue) {}
>>>
>>> public abstract object Value { get; set; }
>>>
>>>
>>> public void commit () { currVal = newVal; }
>>> public void reset () { newVal = currVal; }
>>>
>>> }
>>>
>>>
>>>
>
>
.
- References:
- copying objects
- From: Bob Weiner
- RE: copying objects
- From: Richard
- Re: copying objects
- From: Bob Weiner
- copying objects
- Prev by Date: Error:System.Data.NoNullAllowedException on non-null column
- Next by Date: Re: I need code for conditioned-redirection please
- Previous by thread: Re: copying objects
- Next by thread: Re: copying objects
- Index(es):
Relevant Pages
|