Geekzone: technology news, blogs, forums
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.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)


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


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


  #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


You will find anything you want at MightyApe (affiliate link).

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 »

Synology Introduces BeeStation
Posted 23-Feb-2024 14:14

New One UI 6.1 Update Brings Galaxy AI to More Galaxy Devices
Posted 23-Feb-2024 10:50

Amazon Echo Hub Available in New Zealand
Posted 23-Feb-2024 10:40

InternetNZ Releases Internet Insights 2023
Posted 20-Feb-2024 10:31

Seagate Adds 24TB IronWolf Pro Hard Drives for Multi-user Commercial and Enterprise RAID Storage Solutions
Posted 19-Feb-2024 16:54

Seagate Skyhawk AI 24TB Elevates Edge Security Capacity and Performance
Posted 9-Feb-2024 17:18

GoPro Releases Quik Desktop App for macOS and Introduces Premium+ Subscription Tier
Posted 9-Feb-2024 17:14

Ring Introduces New Ring Battery Video Doorbell Pro
Posted 9-Feb-2024 16:51

Galaxy AI Transforms the new Galaxy S24 Series
Posted 18-Jan-2024 07:00

D-Link launches AI-Powered Aquila Pro M30 Wi-Fi 6 Mesh Systems
Posted 17-Jan-2024 20:02

Newest LG 4K Lifestyle Projector Doubles as Art Objet
Posted 9-Jan-2024 15:50

More LG Smart TV Owners Set To Enjoy the Latest webOS Upgrade
Posted 9-Jan-2024 15:45

Panasonic Announces the Z95A and Z93A With Fire TV Built In
Posted 9-Jan-2024 15:30

Amazon Echo Pop Review
Posted 8-Jan-2024 14:22

Samsung Tab S9 FE Review
Posted 17-Dec-2023 08:26

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.