Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


jase250

90 posts

Master Geek


#21724 5-May-2008 12:22
Send private message

Hi all,

I am relatively new to programming and am having trouble wrapping my head around how a class holds its data.

I have a simple class (Boardstate) which holds an array of blocks (another class) and a collection of possible moves.

I declare a new instance of Boardstate, initialise it with some data and then add it to a collection of BoardStates.

Later in the code I retrieve this Boardstate and assign it to a temporary board. I then call a public function (from the temp board) which applies one of the the possible moves to the board and returns the result.

I then add this result to my collection of Boardstates.

The issue i have is that when I manipulate data in the temporary board (a new instance of the class), every previous instance is being changed. I believe I must be using incorrect syntax to copy the class, which is resulting in some kind of link.

Here is my code:



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim thisBoard As New BoardState

thisBoard.Init()
thisBoard.AddBlock(1, 1, 1, 1, 2)
thisBoard.AddBlock(2, 2, 2, 2, 2)
thisBoard.AddBlock(3, 1, 3, 1, 3)
thisBoard.AddBlock(4, 2, 3, 3, 3)
thisBoard.AddBlock(5, 3, 1, 3, 2)
thisBoard.AddBlock(6, 3, 5, 4, 5)
thisBoard.AddBlock(7, 1, 4, 2, 5)
thisBoard.AddBlock(8, 4, 1, 4, 1)

BoardTrail.Add(thisBoard)

End Sub

This part is fine - initiates the board and adds it to the global(!) collection, BoardTrail.

Later in my code I have:

Dim tempBoard As New BoardState
Dim compBoard As New BoardState

tempBoard = BoardTrail(1)

compBoard = tempBoard.Move()

BoardTrail.Add(compBoard)



If I explore the BoardTrail collection, it shows that BoardTrail(1) has had the Move function applied to it.

I have experimented, and even adding another block to a different instance (compBoard.AddBlock(someblockdata)) is reflected in BoardTrail(1).

The Move function *does* change the contents of the boardstate it is called from, ie after tempboard.move(), tempboard has had a move applied to it. (I know, not good practise...)  But why does this change BoardTrail(1)?

Am I missing the point of the '=' assignment when it is applied to an instance of a class - it seems to form a rather strong bond between the two instances?

Is there a way I can copy the elements stored in a class to a new instance of it, without having to resort to making everything in it public and copying it over, or declaring a pile of read only properties?

Any help is greatly appreciated!

Cheers, Jason.

Create new topic
slipmat
26 posts

Geek


  #128579 5-May-2008 13:06
Send private message


As you suspect, even though you think you have copied the instance of the object to the temp variable - it is in fact still pointing to the original instance. In VB you can't copy an instance of any Object as simply as you have tried.

Does the BoardState class have a Clone method? If not then you may need to implement ICloneable
http://msdn.microsoft.com/en-us/library/system.icloneable.aspx


jase250

90 posts

Master Geek


  #130725 13-May-2008 21:42
Send private message


Thanks very much for your reply, slipmat.  I was advised not to bother with learning about pointers unless I wanted to get into programming seriously, but it has become clear to me that not much makes sense in object oriented code without a grasp of them.

So now it has become profoundly clear why my code wouldn't do as I expected.  Thank you very much for pointing (sic) me in the right direction!

Cheers, Jason.

Create new topic





News »

Huawei launches IdeaHub Pro in New Zealand
Posted 27-Oct-2020 16:41


Southland-based IT specialist providing virtual services worldwide
Posted 27-Oct-2020 15:55


NASA discovers water on sunlit surface of Moon
Posted 27-Oct-2020 08:30


Huawei introduces new features to Petal Search, Maps and Docs
Posted 26-Oct-2020 18:05


Nokia selected by NASA to build first ever cellular network on the Moon
Posted 21-Oct-2020 08:34


Nanoleaf enhances lighting line with launch of Triangles and Mini Triangles
Posted 17-Oct-2020 20:18


Synology unveils DS16211+
Posted 17-Oct-2020 20:12


Ingram Micro introduces FootfallCam to New Zealand channel
Posted 17-Oct-2020 20:06


Dropbox adopts Virtual First working policy
Posted 17-Oct-2020 19:47


OPPO announces Reno4 Series 5G line-up in NZ
Posted 16-Oct-2020 08:52


Microsoft Highway to a Hundred expands to Asia Pacific
Posted 14-Oct-2020 09:34


Spark turns on 5G in Auckland
Posted 14-Oct-2020 09:29


AMD Launches AMD Ryzen 5000 Series Desktop Processors
Posted 9-Oct-2020 10:13


Teletrac Navman launches integrated multi-camera solution for transport and logistics industry
Posted 8-Oct-2020 10:57


Farmside hits 10,000 RBI customers
Posted 7-Oct-2020 15:32









Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.


Support Geekzone »

Our community of supporters help make Geekzone possible. Click the button below to join them.

Support Geezone on PressPatron



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.