Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2007

Tip: Looking for answers? Try searching our database.

Help with classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jared - 12 Jun 2007 13:20 GMT
Hi,

Problem is when I update a copy of a object variable it updates the object
variable it was copied from.  see code below..

Sub Main()
Dim cat1 As New cat
Dim cat2 As cat
cat1.Color = ConsoleColor.Black
cat2 = cat1
cat2.Color = ConsoleColor.Blue
Trace.WriteLine(cat1.Color.ToString)
End Sub

Public Class cat
Dim mColor As ConsoleColor
Public Property Color() As ConsoleColor
Get
Return mColor
End Get
Set(ByVal value As ConsoleColor)
mColor = value
End Set
End Property
End Class

All the dam cats are now blue!!!  I only want cat 2 to be blue.

TIA
Jared
PvdG42 - 12 Jun 2007 16:26 GMT
> Hi,
>
[quoted text clipped - 6 lines]
> cat1.Color = ConsoleColor.Black
> cat2 = cat1
<snip>

Right here. cat2 = cat1
You are dealing with object variables, which are reference types (i.e. they
hold the address of the actual object).
The assignment statement stores the *address* of the object referenced by
cat1 in cat2, thus making cat2 a second reference to the same object.
There is no second object.
Jared - 13 Jun 2007 05:38 GMT
TY, is there a easy workaround?
Mr. Arnold - 13 Jun 2007 06:55 GMT
> TY, is there a easy workaround?

Dim cat1 as new cat    // It is its own cat. It is its own object
Dim cat2 As new cat   // It is its own cat. It is its own  object

You deal with each cat/object separately. And you don't cat2 = cat1.

Your way of

Dim cat1 as new cat   ' Cat1 is its own cat. It is its own object.
dim cat2 as cat            ' Cat2 is not its own cat. It was not born with
the (new) keyword.
                                    ' Cat2 only has
characteristics/properties of  being a (cat).

and then setting cat2 = cat1 set reference to cat1 they are the same cat at
that point.

You do something to cat2, you're doing it to cat1. You use cat2, you're just
getting cat1, because the pointer for cat2 is pointing to cat1. Cat1 is the
only cat that's there and alive.
Jack Jackson - 13 Jun 2007 18:23 GMT
>TY, is there a easy workaround?

If your class is really as simple as your example, you could add a
constructor that takes a reference to an existing instance and sets
the properties of the new instance look like the existing instance:

Sub Main()
Dim cat1 As New cat
Dim cat2 As cat
cat1.Color = ConsoleColor.Black
cat2 = New cat(cat1)
cat2.Color = ConsoleColor.Blue
Trace.WriteLine(cat1.Color.ToString)
End Sub

Public Class cat
Dim mColor As ConsoleColor
Public Property Color() As ConsoleColor
Get
Return mColor
End Get
Set(ByVal value As ConsoleColor)
mColor = value
End Set
End Property

Sub New(_cat as cat)
 Me.Color = _cat.Color
End Sub

End Class
Jared - 17 Jun 2007 23:02 GMT
Thanks, your answer was the best solution.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.