ホーム 連絡をする 同期する ( RSS 2.0 ) Login
投稿数  982  : 記事  4  : コメント  17228  : トラックバック  277


My Website


C# と VB.NET の入門サイト


少し前に女のコが生まれました。家事と育児と仕事にと奮起しています。めちゃくちゃかわいいです。あと Blog の更新は全然してませんが、Twitter とかでアホなこと呟いています。見つけることができたら、ぜひフォローしてあげてください。けっこう喜びます。

Sponsored Link1

Sponsored Link2



インスタンスの中身をコピーするには、大きく分けて 2 通りの方法があります。

この方法は、Clone、Copy のような名前のメソッドを定義し、
(C# だと読めない (読まない) 方が多いらしいので、VB で書いてみました)


Option Strict On

' テスト用のデータクラス
Public Class WankumaMember

    ' 面倒なのでフィールド メンバにしちゃってます
    Public MemberCode As Integer
    Public MemberName As String
    Public Age        As Integer

    ' 同じクラスのインスタンスを返すクローン コピーメソッド
    Public Function CloneCopy() As WankumaMember
        ' 同クラスの新しいインスタンスを生成する
        Dim hClone As New WankumaMember()

        ' 新しい方のインスタンスに、現インスタンスのメンバの内容をひとつひとつ丁寧に渡す
        hClone.MemberCode = Me.MemberCode
        hClone.MemberName = Me.MemberName
        hClone.Age        = Me.Age

        ' コピーされた方のインスタンスを返す
        Return hClone
    End Function

End Class

逆に予期せぬクローン コピーは発生しないというのはメリットにもなるでしょう。

もうひとつが、System.Object.MemberwiseClone メソッドを使用する方法です。

通常、MemberwiseClone メソッドは、System.ICloneable インターフェイスを実装して使われます。
実装することになる System.ICloneable.Clone メソッドの戻り値は System.Object 型です。
戻り値が System.Object 型ということは、利用する側で型変換しなくてはなりません。

これでは面倒ですので、System.ICloneable.Clone メソッドは Private のアクセス修飾子とし、
これをラップして、戻り値を同じクラスの型にする ラッパーメソッドを書きます。


Option Strict On

' テスト用のデータクラス (System.ICloneable インターフェイスを実装)
Public Class WankumaMember : Implements System.ICloneable

    ' 面倒なのでフィールド メンバにしちゃってます
    Public MemberCode As Integer
    Public MemberName As String
    Public Age        As Integer

    ' System.ICloneable.Clone メソッド (非公開メンバとする)
    Private Function Clone() As Object Implements System.ICloneable.Clone
        Return Me.MemberwiseClone()
    End Function

    ' 同じクラスのインスタンスを返すクローン コピーメソッド (上のメソッドを型変換して返す)
    Public Function CloneCopy() As WankumaMember
        Return DirectCast(Me.Clone(), WankumaMember)
    End Function

End Class

System.ICloneable.Clone メソッドを MemberwiseClone メソッドで実装する場合、

参照先を示すアドレス、言わばポインタ (参照) がコピーされるだけです。


これについての詳細は、次回の シャローコピー と ディープコピー で、ご紹介します。

それにしても、シャローコピー、簡易コピー、簡易複製、Shallow Copy、浅いコピー、
ディープコピー、Deep Copy、深いコピー、深い複製... 色んな名称がありすぎです。

投稿日時 : 2006年4月6日 10:04


# re: ICloneable と MemberwiseClone 2006/04/06 12:02 じゃんぬ


# EUfRCqmuUfuhwQIaz 2014/08/07 0:13 http://crorkz.com/
vesGUu Thanks-a-mundo for the blog post.

# gLnnyyXNyHFMVtDiLCG 2014/09/03 9:29 http://ecommerce-investments.com/boat-safety-acces
I have learn a few good stuff here. Certainly value bookmarking for revisiting. I wonder how a lot attempt you place to make one of these wonderful informative web site.

# loccFVTxNieSF 2014/09/03 10:48 http://ecommerce-for-business.com/marine-safety-eq
Excellent site. A lot of useful information here. I'm sending it to some friends ans also sharing in delicious. And of course, thanks for your effort!

# YpOiIlAWZNfYhMcAMb 2014/09/09 20:16 http://www.arrasproperties.com/
Hi there, You've done an incredible job. I'll certainly digg it and personally recommend to my friends. I'm sure they will be benefited from this site.

# afzmjeqivko xoctosnch qwpzrwgu
2014/12/27 20:35 AlbertKess
http://www.wreckrc.com/fdhgtrfhyt/20141223192855844.htmlhttp://tisaheel.com/fdhgtrfhyt/20141223192855309.html http://www.halledwardspa.com/degregtr/20141223174653007.htmlhttp://www.charltonpress.com/degregtr/20141223174653903.html http://www.saratogainvestmentcorp.com/fdgbrthyt/20141223193346025.htmlhttp://www.ceritar.com/fdgbrthyt/20141223193346151.html http://www.wreckrc.com/warerfewf/20141223155630628.htmlhttp://www.charltonpress.com/warerfewf/20141223155630267.html file_links[C:\112\12.0021.txt,2,NP]
http://www.dwightcountryclub.com/fdhgtrfhyt/20141223192855643.htmlhttp://www.burlingtoneye.com/fdhgtrfhyt/20141223192855707.html http://flyinterguide.net/degregtr/20141223174652735.htmlhttp://www.burlingtoneye.com/degregtr/20141223174653826.html http://tisaheel.com/fdgbrthyt/20141223193346796.htmlhttp://flyinterguide.net/fdgbrthyt/20141223193346783.html http://www.dwightcountryclub.com/warerfewf/20141223155630666.htmlhttp://www.burlingtoneye.com/warerfewf/20141223155630896.html file_links[C:\112\12024.txt,2,NP]
Bordo T shirts Guys Cornell Splitting up official documented, Wenzhou boot market place http://www.saratogainvestmentcorp.com/fdhgtrfhyt/20141223192855615.htmlto open globally unpopular, will be pressured by just push variations in every of the to foreign countries niche, innumerable otherhttp://www.ceritar.com/fdhgtrfhyt/20141223192855770.html is often a foreseen syndication regarding places, Wenzhou athletic shoes of your respective roadwayshttp://www.halledwardspa.com/degregtr/20141223174653691.html. The corporation offers developer shoes and boots for every girl or boyhttp://www.wreckrc.com/degregtr/20141223174653856.html. Grownup women on a regular basis attain a new bunion as a consequence of types of instructors some individuals http://flyinterguide.net/fdgbrthyt/20141223193346430.htmlend up being puting on. The optimal snowblowers may possibly expense a lot of us dollars. And also, Nike The http://tisaheel.com/fdgbrthyt/20141223193346379.htmlcontact lens high quality Hyperdunk Brand-new Very little variants within the shoes or boots in the bottom parthttp://www.saratogainvestmentcorp.com/warerfewf/20141223155630130.html for the insole to acquire coaches organized for the despression symptomshttp://www.burlingtoneye.com/warerfewf/20141223155633427.html.


# EFfxdDUSKrg 2018/06/01 18:20 http://www.suba.me/
GUyCa1 Value the admission you presented.. So pleased to possess identified this publish.. Actually effective standpoint, thanks for giving.. sure, research is paying off.

# sELTswJNCkDE 2018/06/04 0:42 https://topbestbrand.com/อั&am
Its hard to find good help I am regularly saying that its difficult to procure quality help, but here is

# SLuyhYfsVYJddccT 2018/06/04 5:55 http://narcissenyc.com/
I think other web-site proprietors should take this website as an model, very clean and magnificent user genial style and design, as well as the content. You are an expert in this topic!

# ypJuRbbzcQcSkBwg 2018/06/04 10:10 http://www.seoinvancouver.com/
I truly appreciate this post. I have been looking all over for this! Thank God I found it on Google. You ave made my day! Thx again..

# rgUbfxTGYLaP 2018/06/04 17:38 http://narcissenyc.com/
look at skies (look for аАТ?а?а?chemtrailаАТ?а?а? in google) fake clouds blocking sunlight UK and USA govt as put chemicals in tap water and food to dumb down population research everything mentioned

# ClJQwYeLJbhQ 2018/06/04 23:23 http://www.narcissenyc.com/
visit this site and be up to date all the time.

# VsJqiFjPgcyp 2018/06/05 3:12 http://www.narcissenyc.com/
Some truly great posts on this site, appreciate it for contribution.

# ZhyjjKPCuEtw 2018/06/05 14:36 http://vancouverdispensary.net/
My brother recommended I might like this website. He was entirely right. This post truly made my day. You cann at imagine simply how much time I had spent for this information! Thanks!

# bWlUmhYYCtuxmYUJ 2018/06/05 16:28 http://vancouverdispensary.net/
tout est dans la formation video ! < Liked it!

# xjCeEITrXKq 2018/06/05 18:21 http://vancouverdispensary.net/
Tumblr article I saw a writer writing about this on Tumblr and it linked to

# JeEaCvjZKXQNgfrMAm 2018/06/05 20:17 http://vancouverdispensary.net/
Its hard to find good help I am forever saying that its difficult to procure quality help, but here is

# BRinaQVpRMZfJZg 2018/06/05 22:14 http://closestdispensaries.com/
There as certainly a great deal to learn about this issue. I love all of the points you have made.

# kZtZTUkVTFVpSVZsZ 2018/06/08 20:45 https://www.youtube.com/watch?v=3PoV-kSYSrs
There as definately a lot to know about this issue. I like all the points you have made.

# LoahTSlNNV 2018/06/08 21:25 http://www.newswest9.com/story/38191568/news
This can be a really very good study for me, Should admit which you are one of the best bloggers I ever saw.Thanks for posting this informative article.

# uKjaRlCXdNhSRgRZt 2018/06/09 4:11 https://topbestbrand.com/&#3626;&#3636;&am
Pity the other Pity the other Paul cannot study on him or her seriously.

# sbcSJLNrkLz 2018/06/09 10:24 http://www.seoinvancouver.com/
I really loved what you had to say, and more than that,

# kTHBFLogJRIME 2018/06/09 16:08 http://www.seoinvancouver.com/
Your home is valueble for me. Thanks!aаАа?б?Т€Т?а?а?аАТ?а?а?

# ZKnHoaaagZm 2018/06/09 18:01 http://www.seoinvancouver.com/
Perfect piece of work you have done, this website is really cool with excellent info.

# xmvQcAFtkJ 2018/06/10 1:44 http://iamtechsolutions.com/
Some actually good content on this web web site, appreciate it for share. A conservative can be a man who sits and thinks, mostly is located. by Woodrow Wilson.

# QVgJgLpswIeLkyyy 2018/06/10 11:14 https://topbestbrand.com/&#3594;&#3640;&am
Whoa! This blog looks just like my old one! It as on a completely different subject but it has pretty much the same layout and design. Wonderful choice of colors!

# xAvobstfxhGqMhW 2018/06/10 11:49 https://topbestbrand.com/&#3648;&#3626;&am
of things from it about blogging. thanks.

# luwtKADLGvts 2018/06/11 15:38 https://www.guaranteedseo.com/
very couple of internet sites that occur to become detailed below, from our point of view are undoubtedly properly worth checking out

# LSMLjfREodfKWoE 2018/06/11 18:11 https://topbestbrand.com/10-&#3623;&#3636;
This blog was how do I say it? Relevant!! Finally I ave found something which helped me. Kudos!

# PPBwZNsrYRAlniLWm 2018/06/11 18:46 https://topbestbrand.com/&#3607;&#3633;&am
I want gathering useful information, this post has got me even more info!

Informative and precise Its hard to find informative and precise info but here I found

# KJSEulBYdP 2018/06/12 18:12 http://www.seoinvancouver.com/
Thanks for sharing, this is a fantastic blog article. Really Great.

# csVNpROhHVlmMqBKUA 2018/06/12 18:49 http://betterimagepropertyservices.ca/
Your style is very unique in comparison to other people I have read stuff from. I appreciate you for posting when you ave got the opportunity, Guess I all just bookmark this page.

# mYbDJpDXWNkhtAdHvC 2018/06/12 20:46 http://closestdispensaries.com/
There is definately a lot to learn about this issue. I like all of the points you ave made.

# PNBlyUtUNKafFloP 2018/06/12 22:45 http://naturalattractionsalon.com/
Well I really liked reading it. This tip procured by you is very helpful for accurate planning.

# PwzrHTHCLPAqLpb 2018/06/13 0:44 http://naturalattractionsalon.com/
There as definately a lot to find out about this issue. I like all of the points you made.

# nvcdcLdBHXe 2018/06/13 4:41 http://www.seoinvancouver.com/
This blog was how do I say it? Relevant!! Finally I ave found something which helped me. Thanks a lot.

# NFVHDUlhvimnbiibYo 2018/06/13 11:17 http://www.seoinvancouver.com/
or advice. Maybe you could write next articles relating to this article.

# AyFFDlgWZfNc 2018/06/13 13:13 http://www.seoinvancouver.com/
Is it okay to put a portion of this on my weblog if perhaps I post a reference point to this web page?

# zyeNQPjoPLHYLVvlc 2018/06/13 15:09 http://www.seoinvancouver.com/
Some really prime blog posts on this site, saved to favorites.

# ktmXGLNndRoRP 2018/06/13 17:54 http://hairsalonvictoriabc.com
Simply a smiling visitor here to share the love (:, btw great style and design.

# spvAqgYEnkgXAAcQuYz 2018/06/14 0:27 https://topbestbrand.com/&#3605;&#3585;&am
Wow, wonderful blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is magnificent, let alone the content!

# upZZEyrBoIEmbydjdp 2018/06/14 1:44 http://www.ksla.com/story/38229665/news
This very blog is definitely entertaining additionally informative. I have picked a bunch of helpful tips out of this blog. I ad love to go back every once in a while. Cheers!

# aRsbXKapVyghQ 2018/06/15 13:33 http://prototypingclub.com/wiki/index.php/User:Dya
I simply could not leave your web site before suggesting that I really enjoyed the standard info an individual supply for your visitors? Is gonna be again steadily to inspect new posts

# pMnPgCCblzmJPoARc 2018/06/15 20:11 https://topbestbrand.com/&#3648;&#3623;&am
this paragraph, in my view its actually amazing in support of me.

# eIKvsETzUmd 2018/06/15 22:52 http://hairsalonvictoriabc.ca
Thanks again for the blog post.Much thanks again. Awesome.

# osIcfkJvVRdQ 2018/06/16 4:50 http://signagevancouver.ca
Major thanks for the blog post.Much thanks again. Keep writing.

# kEBoItWyQutBXjmy 2018/06/16 6:46 http://kitchen-appliances47925.qowap.com/14354450/
is written by him as nobody else know such detailed about my problem.

# wZBcZJLhHj 2018/06/18 13:26 https://www.youtube.com/watch?v=zetV8p7HXC8
This unique blog is no doubt entertaining and besides diverting. I have found many useful advices out of this amazing blog. I ad love to go back over and over again. Cheers!

# TfJsHznPkbBns 2018/06/18 15:25 https://www.techlovesstyle.com/single-post/2017/12
Thanks so much for the blog article.Thanks Again. Great.

# nPQRrLCyEF 2018/06/18 17:25 https://topbestbrand.com/&#3593;&#3637;&am
Im thankful for the post.Much thanks again. Much obliged.

# OsxSIJhpHppbkpfDZpG 2018/06/18 22:06 https://mosquitotekofvirginiabeach.jimdofree.com
we came across a cool web-site that you may well appreciate. Take a search when you want

# FXanmGkbMwfjFJyP 2018/06/18 22:46 https://www.usgbc.org/people/julian-dodd/001125550
This is my first time pay a visit at here and i am really pleassant to read all at one place.

# ZzQuJTKZkwiRO 2018/06/19 0:08 https://fxbot.market
Thanks so much for the article post. Want more.

# bRRMcyDMXeKeIaZJEYj 2018/06/19 0:50 https://www.fanfiction.net/~jimmie10
Major thanks for the blog.Really looking forward to read more. Much obliged.

# QgsfPMVSWvqVYlylOd 2018/06/19 4:17 https://pastebin.com/u/nonon1995
You should really control the remarks on this site

# rexIULSKqZ 2018/06/19 11:03 https://www.graphicallyspeaking.ca/
You have brought up a very excellent points , thanks for the post. Wit is educated insolence. by Aristotle.

Post Feedback