Re: copying objects

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




> 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; }
>>>
>>> }
>>>
>>>
>>>
>
>


.



Relevant Pages

  • Re: copying objects
    ... into another method named Clone(). ... >> I don't want the assignment operator in commit() and reset() to work by ... >> private object currVal; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can I reset a password or remove a username?
    ... it appears I must know the password answer to reset the ... passwordAnswer As String _ ... If you, as an admin, need to do this, Code against the Membership ... One gotcha. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Can I reset a password or remove a username?
    ... passwordAnswer As String _ ... "Gregory A. Beamer" wrote in message ... I will have to peruse my code base and find a sample, as I have already created a fairly simple admin page to reset a user's password. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: .version keeps being updated
    ... 8993780a6e44fb4e7ed34e33458506a775356c6e is first bad commit ... depends on getting the kernel version from the kernel binary. ... This just restores "linux_banner" as a static string, ... Reverting this from 2.6.20-rc1 made the build behave again, ...
    (Linux-Kernel)
  • Re: Copy/Clone object
    ... This can be used to clone an object: serialize the object to a string, and use deserializing to create the clone from the string. ...
    (microsoft.public.dotnet.languages.csharp)