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 »

OPPO Find X2 Lite brings flagship features to mid-range 5G smartphone
Posted 29-May-2020 12:52


Sony introduces the digital camera ZV-1 for content creators
Posted 27-May-2020 12:47


Samsung Announces 2020 QLED TV Range
Posted 20-May-2020 16:29


D-Link A/NZ launches AI-Powered body temperature measuring system
Posted 20-May-2020 16:22


NortonLifeLock Online Banking Protection now available for New Zealand banks
Posted 20-May-2020 16:14


SD Express delivers new gigabyte speeds for SD memory cards
Posted 20-May-2020 15:00


D-Link A/NZ launches Nuclias cloud managed network solution hosted in Australia
Posted 11-May-2020 17:53


Logitech introduces new video streaming solution for home studios
Posted 11-May-2020 17:48


Next generation Volvo cars to be powered by Luminar LiDAR technology
Posted 7-May-2020 13:56


D-Link A/NZ launches Wi-Fi Certified EasyMesh system
Posted 7-May-2020 13:51


Spark teams up with Microsoft to bring Xbox All Access to New Zealand
Posted 7-May-2020 13:01


Microsoft plans to establish its first datacenter region in New Zealand
Posted 6-May-2020 11:35


Genesis School-gen has joined forces with Mind Lab Kids
Posted 1-May-2020 12:53


Malwarebytes expands into privacy with fast, frictionless VPN
Posted 30-Apr-2020 16:06


Kordia to donate TV airtime on Channel 200 to community groups
Posted 30-Apr-2020 16:00



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.