Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / VB / VB.net - Dynamic Array Problems

Author
Message
Insanity Complex
19
Years of Service
User Offline
Joined: 16th Sep 2005
Location: Home
Posted: 16th Oct 2010 03:04
On a project I'm working on, I'm using a dynamic array. There's a lot going on but the problem is fairly simple, I think. I just don't know how to solve it.

So in an earlier write of the program, I had declared my dynamic array within a Public Sub. Later on I found that I needed it elsewhere(in another sub), so I moved the initial declaration to being a public variable, outside of the sub, as a (1 , 1 , 1) array. I then proceeded to use ReDim inside of the Sub it was previously declared in, to fit the user defined dimensions. Quick mockup:



This has no problem until runtime. The grid is properly generated in Setup. But when grid is referenced in Use(which isn't called until after Setup), it throws an error "Object reference not set to an instance of an object."

Another potentially useful piece of information is that the X, Y, and Z variables are created in a separate form, which shows up before the form that grid is used in. After x, y, and z are obtained, the first form goes away, and the second form is brought up, where grid is manipulated.

Any help appreciated

knxrb
FPSC Tool Maker
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: United Kingdom
Posted: 16th Oct 2010 13:47 Edited at: 16th Oct 2010 13:55
Hi, I would do it like this:

Make the grid array:


Function to add a new block:


The BlockGrid class:


Then you only add blocks to the array in the area that the user wants them, therefore not having data for all empty spaces as well (meaning you can read through the array faster and it uses less of the memory).
For example:
A user places a block at X:5, Y:6, Z:10.
You would add that block to the array using: AddBlock(5, 6, 10)


Then to read through all blocks that have been placed you use:


I've attached my 'GridTest.vb' code file for you to have a look at as well.

knxrb

Attachments

Login to view attachments
Insanity Complex
19
Years of Service
User Offline
Joined: 16th Sep 2005
Location: Home
Posted: 16th Oct 2010 14:04 Edited at: 16th Oct 2010 14:16
Quote: "Then you only add blocks to the array in the area that the user wants them, therefore not having data for all empty spaces as well (meaning you can read through the array faster and it uses less of the memory)."


Right now the way it is set up, is that the user declares a height and width of their planning grid, as well as layers. This pre-determines the size they want/need. It then displays their generated grid as blank "air" blocks. I don't know that I'd want to approach it that way o.O

If you'd like to see the full source to get a better idea, email me at contact(dot)fractality(at)gmail(dot)com

It's my fractal email but it has the least traffic so I'm most likely to see the email in a timely fashion.

[Edit] Also, the z value is used to represent layers, all but one layer of z being shown at a time, displaying an x by y grid. o.O

Current code sections dealing with the grid:

Mainform:
Blockgrid struct:


Declaration in Mainform class:


Passing information for grid dimensions from Startscreen to mainform:


Setting up the grid with info from the Startscr:


Attempting to re-access grid in another Sub relating to a listbox of layers(The exception is thrown here during runtime, on trying to read from grid):


RalphY
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: 404 (UK)
Posted: 16th Oct 2010 17:05
Just throwing this out there but based on your Setup sub, shouldn't the for loops in lbLay_SelectedIndexChanged be to Me.x - 1, Me.y - 1, and Me.z - 1 or do you take that into account elsewhere?

Go banana! | Super Nintendo Chalmers! | When I grow up I'm going to Bovine University!
Insanity Complex
19
Years of Service
User Offline
Joined: 16th Sep 2005
Location: Home
Posted: 17th Oct 2010 00:49 Edited at: 17th Oct 2010 01:06
Quote: "Just throwing this out there but based on your Setup sub, shouldn't the for loops in lbLay_SelectedIndexChanged be to Me.x - 1, Me.y - 1, and Me.z - 1 or do you take that into account elsewhere?"


Yes...yes they should in fact...

[edit]
Fixed the for loops like you said, and the code works flawlessly now. Thank you, I can't believe I didn't see that

RalphY
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: 404 (UK)
Posted: 17th Oct 2010 02:12
No problem, it happens to us all at some point .

Go banana! | Super Nintendo Chalmers! | When I grow up I'm going to Bovine University!
Insanity Complex
19
Years of Service
User Offline
Joined: 16th Sep 2005
Location: Home
Posted: 19th Oct 2010 00:30
Quote: "Hi, I would do it like this:
"


I take back what I said. While I'm not strictly implementing what you wrote...the way I'm going to be optimizing some things is heavily reliant on what you showed me

Figured I should recant my saying that I didn't want to do it that way.

musclenatural
User Banned
Posted: 3rd Nov 2010 07:35
well i was looking for that thanks

Login to post a reply

Server time is: 2025-05-27 23:36:44
Your offset time is: 2025-05-27 23:36:44