PDA

View Full Version : مشکل با رفرنس بودن داده در VB.NET



VisualStudio
شنبه 15 دی 1386, 17:34 عصر
چه کار میشه کرد که داده ها به صورت پیش فرض در VB.NET یک کپی از آن ها گرفته بشه خود مقدار ارجاع داده نشه
برای مثال :



Dim a(0 To 10), b() AsInteger
Dim i AsInteger
For i = 0 To 10
a(i) = i
Next
For i = 0 To 10
Debug.Print(a(i).ToString)
Next
b = a
b(0) = 100
For i = 0 To 10
Debug.Print(a(i).ToString)
Next


همانطور که مشاهده می کنید موفعی که دوباره Print میگیریم اندیس 0 آرایه a هم 100 شده اما ما a را به b نسبت دادیم نه b را به a دلیل این مشکل این هست که در VB.NET رفرنس ها به یکدیگر نسبت داده میشوند چه طور میشه از این کار جلوگیری کرد(فقط مشکل بر روی آرایه ها نیست منظورم هدف کلی هست )
خواهشا سریع آیا متدی هست که داده ها را کپی کنی نه برای آرایه برای یک کلاسی که خودمان می نویسیم یا از کلاسی ارثبری کنیم

amirsajjadi
شنبه 15 دی 1386, 19:56 عصر
با سلام

Dim a(0 To 10), b() AsInteger
Dim i AsInteger
For i = 0 To 10
a(i) = i
Next
For i = 0 To 10
Debug.Print(a(i).ToString)
Next
ReDim b(UBound(a))
Array.Copy(a, b, UBound(a))
b(0) = 100
For i = 0 To 10
Debug.Print(a(i).ToString)
Next


خواهشا سریع آیا متدی هست که داده ها را کپی کنی نه برای آرایه برای یک کلاسی که خودمان می نویسیم یا از کلاسی ارثبری کنیم
برای آرایه زیر برنامه ی Array.Copy این کار رو انجام میده ولی من متوجه نشدم منظورتون دقیقا چیه

VisualStudio
شنبه 15 دی 1386, 22:04 عصر
من ذکر کردم که من منظورم به آرایه نیست کلی چطور این کار را کنم

titbasoft
شنبه 15 دی 1386, 23:59 عصر
از Object هاتون باید Clone بگیرید. چیزی مثل:

b=a.clone()یک مقاله بدرد بخور:
http://msdn.microsoft.com/msdnmag/issues/02/06/Instincts/

As you have seen, value types support copy-on-assignment while reference types do not. This means that there is no built-in support that allows you to create a copy of an object. Therefore, whenever you design a class, ask yourself if you want the class to support cloning. An object that supports cloning allows a client to call a special method that creates a copy of the object on demand

sinpin
یک شنبه 16 دی 1386, 01:12 صبح
... دلیل این مشکل این هست که در VB.NET رفرنس ها به یکدیگر نسبت داده میشوند چه طور میشه از این کار جلوگیری کرد(فقط مشکل بر روی آرایه ها نیست منظورم هدف کلی هست )
این مساله ربطی به vb نداره.
توی گوگل سرچ کنید : "reference type vs value type "


چه کار میشه کرد که داده ها به صورت پیش فرض در VB.NET یک کپی از آن ها گرفته بشه خود مقدار ارجاع داده نشه... خواهشا سریع آیا متدی هست که داده ها را کپی کنی نه برای آرایه برای یک کلاسی که خودمان می نویسیم یا از کلاسی ارثبری کنیم
اگه کلاسی که نوشتید حاوی مقادیر ارجاعی نیست و ساده است از یک کپی سطحی (Shallow Copy) استفاده کنید،
در غیر اینصورت؛
برای اینکه یک کپی کلی (Deep Copy) از کل ساختار آبجکتتون بگیرید دو راه دارید :
1- از سریالیزشین استفاده کنید
2- کلاستون باید اینترفیس IClonable (و متد Clone اون) رو پیاده سازی کنه
رجوع کنید به : (http://www.larkware.com/Articles/CloninginVB.NET.html)http://www.larkware.com/Articles/CloninginVB.NET.html

http://en.wikipedia.org/wiki/Object_copy
Deep vs. Shallow


The design goal of most objects is to make the programmer think that they are made out of one monolithic block even though most are not. As objects are made up of several different parts, copying becomes non trivial. Several strategies exist to attack this problem.
Consider two objects, A and B, which each refer to two memory blocks xi and yi. Think of A and B as strings and of xi and yi as the characters they contain.
http://upload.wikimedia.org/wikipedia/en/thumb/6/6c/Pre_shallow_deep_copy.svg/400px-Pre_shallow_deep_copy.svg.png (http://en.wikipedia.org/wiki/Image:Pre_shallow_deep_copy.svg)
The following paragraphs explain different strategies for copying A into B.

[edit (http://en.wikipedia.org/w/index.php?title=Object_copy&action=edit&section=2)] Shallow copy

One of them is the shallow copy. In this process B is attached to the same memory block as A.
http://upload.wikimedia.org/wikipedia/en/thumb/3/3e/Shallow_copy_in_progress.svg/400px-Shallow_copy_in_progress.svg.png (http://en.wikipedia.org/wiki/Image:Shallow_copy_in_progress.svg)
http://upload.wikimedia.org/wikipedia/en/thumb/e/e1/Shallow_copy_done.svg/400px-Shallow_copy_done.svg.png (http://en.wikipedia.org/wiki/Image:Shallow_copy_done.svg)
This results in a situation in which some data is shared between A and B, thus modifying the one will alter the other. The original memory block of B is now no longer referred to from anywhere. If the language does not have automatic garbage collection (http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29) the original memory block of B has probably been leaked.
The advantage of shallow copies is that their execution speed is fast and does not depend on the size of the data.
Bitwise copies of objects which are not made up of a monolithic block are shallow copies.

[edit (http://en.wikipedia.org/w/index.php?title=Object_copy&action=edit&section=3)] Deep copy

An alternative are deep copies. Here the data is actually copied over.
http://upload.wikimedia.org/wikipedia/en/thumb/5/5f/Deep_copy_in_progress.svg/400px-Deep_copy_in_progress.svg.png (http://en.wikipedia.org/wiki/Image:Deep_copy_in_progress.svg)
http://upload.wikimedia.org/wikipedia/en/thumb/0/00/Deep_copy_done.svg/400px-Deep_copy_done.svg.png (http://en.wikipedia.org/wiki/Image:Deep_copy_done.svg)
The result is different from the result a shallow copy gives. The advantage is that A and B do not depend on each other but at the cost of a slower more expensive copy.

VisualStudio
دوشنبه 17 دی 1386, 12:22 عصر
1- از سریالیزشین استفاده کنید


میشه بگید چطوری

sinpin
دوشنبه 17 دی 1386, 12:39 عصر
1- از سریالیزشین استفاده کنید
میشه بگید چطوری
با سریالیزشین شما میتونید یک گراف (یا یک انشعاب از یک آبجکت) رو بسته بندی کنید و اون رو در فایل یا حافظه - برای استفاده بعدی - ذخیره کنید.
برای مثال :

Public Function Clone() As clsEmployeeSimple
Dim ms As Stream = New MemoryStream()
Dim bf As BinaryFormatter = New BinaryFormatter()

bf.Serialize(ms, Me)
ms.Position = 0
Return CType(bf.Deserialize(ms), clsEmployeeSimple)
End Function
he clone method hereاین لینک خوبه : http://www.emoreau.com/Entries/Articles/2003/07/Object-Serialization-in-VBNet.aspx


Types of serialization
The .Net Framework offers two types of serialization:

Shallow serialization: this type of serialization persists only public read-write property values of a class. The XMLSerializer and the WebServices use this technique.
Deep serialization: this type of serialization persists all variables of an object. Deep serialization also serializes the object graph (the complete hierarchy). The BinaryFormatter, the SoapFormatter, and the .Net Remoting use this technique.