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.




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




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



Twitter and LinkedIn »



Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:





News »

Logitech introduces new Made for Google keyboard and mouse devices
Posted 16-Oct-2019 13:36


MATTR launches to accelerate decentralised identity
Posted 16-Oct-2019 10:28


Vodafone X-Squad powers up for customers
Posted 16-Oct-2019 08:15


D Link ANZ launches EXO Smart Mesh Wi Fi Routers with McAfee protection
Posted 15-Oct-2019 11:31


Major Japanese retailer partners with smart New Zealand technology IMAGR
Posted 14-Oct-2019 10:29


Ola pioneers one-time passcode feature to fight rideshare fraud
Posted 14-Oct-2019 10:24


Spark Sport new home of NZC matches from 2020
Posted 10-Oct-2019 09:59


Meet Nola, Noel Leeming's new digital employee
Posted 4-Oct-2019 08:07


Registrations for Sprout Accelerator open for 2020 season
Posted 4-Oct-2019 08:02


Teletrac Navman welcomes AI tech leader Jens Meggers as new President
Posted 4-Oct-2019 07:41


Vodafone makes voice of 4G (VoLTE) official
Posted 4-Oct-2019 07:36


2degrees Reaches Milestone of 100,000 Broadband Customers
Posted 1-Oct-2019 09:17


Nokia 1 Plus available in New Zealand from 2nd October
Posted 30-Sep-2019 17:46


Ola integrates Apple Pay as payment method in New Zealand
Posted 25-Sep-2019 09:51


Facebook Portal to land in New Zealand
Posted 19-Sep-2019 18:35



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.