# Easy way to add objects to a 2D Array?

• (2 Pages)
• 1
• 2

## 29 Replies - 20445 Views - Last Post: 25 July 2012 - 09:24 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=286295&amp;s=2d56c2edbbd41fe5c62a1b248ace3a19&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bammurdo

Reputation: 3
• Posts: 62
• Joined: 03-October 09

# Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 06:42 AM

Basically, I have a grid of existing objects on the form in this sort of fashion:

They all have the name of "Point" followed by the number in the image (Its coordinates)
Now I want to add these to a 2D Array, but the only way I can think of doing it is like:
dim points(3,3) ' the 2D array
points(0,0) = point0000
points(1,0) = point0100
points(2,0) = point0200
points(3,0) = point0300
points(0,1) = point0001

Is there anyway to do this with some sort of loop?
Thanks
bammurdo

This post has been edited by bammurdo: 19 July 2012 - 09:44 AM

Is This A Good Question/Topic? 0

## Replies To: Easy way to add objects to a 2D Array?

### #2 sela007

Reputation: 139
• Posts: 841
• Joined: 21-December 11

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 06:59 AM

Is there anyway to do this with some sort of loop?

Yes, you can do this with two for next loop.
You can get number of rows and columns by GetUpperBounF function.
Dim Points(4, 5) As Integer
'Points.GetUpperBound(0)) - returns 4
'Points.GetUpperBound(1)) - returns 5

This post has been edited by sela007: 19 July 2012 - 07:04 AM

### #3 bammurdo

Reputation: 3
• Posts: 62
• Joined: 03-October 09

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 07:09 AM

I believe you've miss understood me
I can create a loop like:
for c = 1 to 3
for d = 1 to 3
points(c,d) =
next
next

but I want to know if there is anyway to use a loop like the one above to add the object to the array.
I don't see how returning the upper bounds helps me in this way

### #4 sela007

Reputation: 139
• Posts: 841
• Joined: 21-December 11

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 07:30 AM

Quote

I believe you've miss understood me

you want add those values using loops?
points(0,0) = point0000
points(1,0) = point0100
points(2,0) = point0200
points(3,0) = point0300
points(0,1) = point0001

### #5 bammurdo

Reputation: 3
• Posts: 62
• Joined: 03-October 09

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 07:39 AM

sela007, on 19 July 2012 - 08:30 AM, said:

Quote

I believe you've miss understood me

you want add those values using loops?
points(0,0) = point0000
points(1,0) = point0100
points(2,0) = point0200
points(3,0) = point0300
points(0,1) = point0001

indeed

### #6 sela007

Reputation: 139
• Posts: 841
• Joined: 21-December 11

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 07:54 AM

like I said you will need two loops. Also you will need one variable for calculating values.
First loop will be from 0 to Points.GetUpperBound(0).
Second will be from 0 to Points.GetUpperBound(1).
In first loop, you will set variable ,x1 = first loop value * 100
In second loop:
you will add second loop value to x1.
and then set points(FLV,SLV)=X1.

This is all from my head, I don't know does it work, but I hope you get the picture

This post has been edited by sela007: 19 July 2012 - 07:55 AM

### #7 bammurdo

Reputation: 3
• Posts: 62
• Joined: 03-October 09

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 07:59 AM

sela007, on 19 July 2012 - 08:54 AM, said:

like I said you will need two loops. Also you will need one variable for calculating values.
First loop will be from 0 to Points.GetUpperBound(0).
Second will be from 0 to Points.GetUpperBound(1).
In first loop, you will set variable ,x1 = first loop value * 100
In second loop:
you will add second loop value to x1.
and then set points(FLV,SLV)=X1.

This is all from my head, I don't know does it work, but I hope you get the picture

Unfortunately, this would give:

points(0,0) = "0000"
points(1,0) = "0100"
points(2,0) = "0200"
points(3,0) = "0300"
points(0,1) = "0001"
and just adding ("Point" & number) to it would just give you the literal as well("Point0000" instead of the object Point0000).

### #8 sela007

Reputation: 139
• Posts: 841
• Joined: 21-December 11

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 08:08 AM

Quote

Unfortunately, this would give:

points(0,0) = "0000"
points(1,0) = "0100"
points(2,0) = "0200"
points(3,0) = "0300"
points(0,1) = "0001"
and just adding ("Point" & number) to it would just give you the literal as well("Point0000" instead of the object Point0000).

I believe you've miss understood me .
It can be done, and it is just matter of logic.
Try something, and post your code.

Quote

and just adding ("Point" & number) to it would just give you the literal as well("Point0000" instead of the object Point0000).

Yes it would add object as string with value "Point0000".

### #9 sela007

Reputation: 139
• Posts: 841
• Joined: 21-December 11

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 08:20 AM

It seems like you want to create new objects called
point0100
point0200
etc...
?

### #10 bammurdo

Reputation: 3
• Posts: 62
• Joined: 03-October 09

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 08:25 AM

sela007, on 19 July 2012 - 09:20 AM, said:

It seems like you want to create new objects called
point0100
point0200
etc...
?

No, the objects already exist, as in the image i posted, (the boxes).
I just want to add them to a 2D array

### #11 trevster344

• The Peasant

Reputation: 225
• Posts: 1,526
• Joined: 16-March 11

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 08:26 AM

Just use a flippin' list(of *objects name*). You don't need a 2d array.. Also the reason adding those objects to point don't work is because they are probably POINT objects... meaning you have to insert your objects.point variable.

Public Class Box

Public MyPoint as Point

Public Sub New(Byval X as Integer, Byval Y as integer)
MyPoint = new Point(X,Y)
End Sub

End Class

Public Class HowILearnedToBox

Public Sub CollectTheBoxes

Dim T as new list(of box)

for each coord as box in T

messagebox.show(coord.MyPoint.X.tostring & " " & coord.MyPoint.Y.tostring)

next

End Sub

End Class

This post has been edited by trevster344: 19 July 2012 - 08:30 AM

### #12 bammurdo

Reputation: 3
• Posts: 62
• Joined: 03-October 09

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 08:30 AM

trevster344, on 19 July 2012 - 09:26 AM, said:

Just use a flippin' list(of *objects name*). You don't need a 2d array.. Also the reason adding those objects to point don't work is because they are probably POINT objects... meaning you have to insert your objects.point variable.

I'm not making the 2D array for the sake of it... Using a 2D Array allows me to do other things that you can't do with lists.
Also
>Also the reason adding those objects to point don't work is because they are probably POINT objects... meaning you have to insert your objects.point variable.

No, the reason this didn't work is because it adds the literal "point0000" (as in, the actual text) instead of the object called 'point0000'

This post has been edited by bammurdo: 19 July 2012 - 08:33 AM

### #13 trevster344

• The Peasant

Reputation: 225
• Posts: 1,526
• Joined: 16-March 11

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 08:32 AM

Tell me what can't you do with them... You can create 2d lists.. at this point in time with the tools available in .NET arrays are a bit obsolete. There are so many different encapsulations of them that produce far more advanced results. So as I said, tell me what you can't do, and I'll show you otherwise.

### #14 bammurdo

Reputation: 3
• Posts: 62
• Joined: 03-October 09

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 08:48 AM

trevster344, on 19 July 2012 - 09:32 AM, said:

Tell me what can't you do with them... You can create 2d lists.. at this point in time with the tools available in .NET arrays are a bit obsolete. There are so many different encapsulations of them that produce far more advanced results. So as I said, tell me what you can't do, and I'll show you otherwise.

2D Lists?

### #15 trevster344

• The Peasant

Reputation: 225
• Posts: 1,526
• Joined: 16-March 11

## Re: Easy way to add objects to a 2D Array?

Posted 19 July 2012 - 08:57 AM

Public Box(2, 2) As List(Of Box)

Forget this crap. Are you trolling or what? Spit out what you want help with or stop coming back. We've given you suggestion after suggestion, and you just shoot it down and provide zero feedback that can help us help you. My next suggestion to you if you want to be a successful programmer is to not carry such a defeatist attitude around, anything can be done you just need to use your brain.

This post has been edited by trevster344: 19 July 2012 - 08:59 AM