-
Jul 28th, 2015, 02:56 PM
#1
Thread Starter
Junior Member
Changing values at 2-dimensional array
Hi,
It looks like I cannot change the the values at 2-dimensional code.
Code:
Sub main_code
check_name(techs_desc) 'techs_desc is 2-dimensional array
End sub
Sub check_emblem_name(ByRef techs_desc)
MsgBox(techs_desc(2)(4) & " - " & techs_desc(3)(4))
techs_desc(2)(4) = "test"
MsgBox(techs_desc(2)(4) & " - " & techs_desc(3)(4))
End Sub
Result is "test - test" instead "test - any"
Not sure what it is going on here...
-
Jul 28th, 2015, 03:21 PM
#2
Re: Changing values at 2-dimensional array
You're never calling the check_emblem_name method, you're calling check_name. But to set values of a two dimensional array you set the first and second index within the same parenthesis:
Code:
Dim foo(1, 1) As String
foo(0, 0) = "Test"
foo(0, 1) = "Test"
foo(1, 0) = "Test"
foo(1, 1) = "Any"
Console.WriteLine(String.Join("-", {foo(0, 0), foo(0, 1)}))
Console.WriteLine(String.Join("-", {foo(1, 0), foo(1, 1)}))
-
Jul 28th, 2015, 03:47 PM
#3
Thread Starter
Junior Member
Re: Changing values at 2-dimensional array
Hmm..
Maybe the problem is with declaring...
Why are you calling array adress for (x,y) when I call (x)(y) ???
Code:
dim array(5) as string
dim tech_desc(6) as object
for i = 0 to 5
[set array parameters]
tech_desc(i) = array
next i
-
Jul 28th, 2015, 03:53 PM
#4
Re: Changing values at 2-dimensional array
-
Jul 28th, 2015, 05:53 PM
#5
Re: Changing values at 2-dimensional array
You aren't creating a two dimensional array, you are creating an array of arrays. Those are different animals.
My usual boring signature: Nothing
-
Jul 29th, 2015, 01:41 AM
#6
Thread Starter
Junior Member
Re: Changing values at 2-dimensional array
So this is the problem...
Maybe my code is incorrect... I'm beginner and 1st time working with set of arrays.
I plan to build a set of data (set of arrays), and keep them in memory together to next operations, f.e. check the data values like:
Is array1(3) = array2(3) ? before save as a file.
array1 = {a1, a2, a3, a4...}
array2 = {b1, b2, b3, b4...}
Code:
dim array(10) as string
dim array_of_arrays(5) as object
For i=0 to 5
For j = 0 to 10
array(j) = <some value>
Next
array_of_arrays(i) = array
Next
Now I'm surprised that all array(i) data (at debugger) looks identical (same values) and when I changed array(i)(j) parameters all arrays(j) parameters are changed too. So maybe my idea is incorrect and gives false results:
Originally Posted by PL_Andrev
Code:
Sub main_code
check_name(techs_desc) 'techs_desc is array_of_arrays
End sub
Sub check_name(ByRef techs_desc)
MsgBox(techs_desc(2)(4) & " - " & techs_desc(3)(4))
techs_desc(2)(4) = "test"
MsgBox(techs_desc(2)(4) & " - " & techs_desc(3)(4))
End Sub
Result is " test - test" instead "test - any"
Not sure what it is going on here...
Last edited by PL_Andrev; Jul 29th, 2015 at 01:42 AM.
Reason: claryfing
-
Jul 29th, 2015, 10:04 AM
#7
Re: Changing values at 2-dimensional array
Yeah, things aren't happening the way you are expecting. Ultimately, you probably want to change your design quite a bit, but if you are starting out, it's a bit hard to know how much of the elephant you want to try to swallow at one time. The best solution would probably be to create a class, because it is pretty rare that the best solution is a multidimensional array.
However, the first question you should consider is whether those inner arrays will all be the same size. If they are, then a 2 dimensional array would be better than an array of arrays. It would be slightly more efficient. A 2-dimensional array would be declared as:
Dim someArray(10,5) As string
That's what DDay was showing in his first post. What you are using is an array of arrays, which is typically done when the inner arrays will be of all different sizes. Even in that case, you would be well advised to look at the List(of T), if only for your education. Arrays are a fundamental part of programming, but they have a drawback in that they are not efficient to resize. The List(of T) would be much better if you will be changing the sizes of any of the arrays.
As to what is happening, I was going to say that you are seeing the results of a reference type (all classes, which includes arrays). That's still the case, but it's a bit more interesting. After all, you explicitly create array(10) of string, then set the values of it in the inner loop. Finally, you assign array to a slot in the outer array in the outer loop. However, you never create a new array in that outer loop, so you just have the one array, you fill it one time and put it into slot 0 of the outer array. You then fill it a second time and put it into slot 1 of the outer array, you then fill it it a third time and put it into slot 2, and so on. It's always the same array, though. You appear to be expecting that every time you assign it to a slot in the outer array that it magically becomes a different array. That's not the case. You only have one array in that outer array. In fact, that outer array doesn't hold the array, it holds the address of the first element of the array (or something close to that), so all you have in the outer array is 6 copies of the address of array, you don't have six different arrays. So, when you change that array, it looks like you are changing all of them, but that's only because "all of them" is nothing more than six copies of a reference to the same object.
What you would need to do is create a new array as the first step of the outer loop, fill that in the inner loop, then assign it to the outer array.
My usual boring signature: Nothing
-
Jul 29th, 2015, 10:35 AM
#8
Re: Changing values at 2-dimensional array
While a class would certainly work in this situation, I think that a DatatTable would be more appropriate:
Code:
Dim dt As DataTable = New DataTable
dt.Columns.AddRange({New DataColumn("Column1"), New DataColumn("Column2")})
dt.Rows.Add({"test", "test"})
dt.Rows.Add({"test", "any"})
This way you iterate through the rows testing the columns.
Last edited by dday9; Jul 29th, 2015 at 11:14 AM.
Reason: Misspelled DataTable
-
Jul 30th, 2015, 06:29 AM
#9
Re: Changing values at 2-dimensional array
Originally Posted by Shaggy Hiker
...
However, the first question you should consider is whether those inner arrays will all be the same size. If they are, then a 2 dimensional array would be better than an array of arrays....
For completeness:
An array of arrays is (often) called a Jagged Array, because, as noted, the length of the contained arrays can have different lengths (or even nothing). When visually represented it would look like it had a jagged 'edge' whereas a 2 dimensional array would be, visually, rectangular.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Jul 30th, 2015, 06:37 AM
#10
Re: Changing values at 2-dimensional array
Originally Posted by PL_Andrev
...
Code:
...
Sub check_emblem_name(ByRef techs_desc)
...
End Sub
...
There is also no reason to use ByRef.
Secondly, there is no 'type' assigned to the passed 'thing': probably because Option Strict is Off. This really should be ON for all but the most specific cases. While this doesn't matter too much, here, it will matter very soon trying to chase obvious bugs hidden by having it off.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Jul 30th, 2015, 10:28 AM
#11
Re: Changing values at 2-dimensional array
Originally Posted by SJWhiteley
For completeness:
An array of arrays is (often) called a Jagged Array, because, as noted, the length of the contained arrays can have different lengths (or even nothing). When visually represented it would look like it had a jagged 'edge' whereas a 2 dimensional array would be, visually, rectangular.
I was going to point that out, too, except that since his inner arrays were all the same size, a visual representation would be that of a saw blade where all the teeth have been worn flat.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|