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 and reviews »

Air New Zealand Starts AI adoption with OpenAI
Posted 24-Jul-2025 16:00


eero Pro 7 Review
Posted 23-Jul-2025 12:07


BeeStation Plus Review
Posted 21-Jul-2025 14:21


eero Unveils New Wi-Fi 7 Products in New Zealand
Posted 21-Jul-2025 00:01


WiZ Introduces HDMI Sync Box and other Light Devices
Posted 20-Jul-2025 17:32


RedShield Enhances DDoS and Bot Attack Protection
Posted 20-Jul-2025 17:26


Seagate Ships 30TB Drives
Posted 17-Jul-2025 11:24


Oclean AirPump A10 Water Flosser Review
Posted 13-Jul-2025 11:05


Samsung Galaxy Z Fold7: Raising the Bar for Smartphones
Posted 10-Jul-2025 02:01


Samsung Galaxy Z Flip7 Brings New Edge-To-Edge FlexWindow
Posted 10-Jul-2025 02:01


Epson Launches New AM-C550Z WorkForce Enterprise printer
Posted 9-Jul-2025 18:22


Samsung Releases Smart Monitor M9
Posted 9-Jul-2025 17:46


Nearly Half of Older Kiwis Still Write their Passwords on Paper
Posted 9-Jul-2025 08:42


D-Link 4G+ Cat6 Wi-Fi 6 DWR-933M Mobile Hotspot Review
Posted 1-Jul-2025 11:34


Oppo A5 Series Launches With New Levels of Durability
Posted 30-Jun-2025 10:15









Geekzone Live »

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



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.