Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello to all,
Previously I had a question about abs math function and how can I use it in an array.

Everything is ok ,now I want to make abs in an dimension array (I know I had opened the previously question about dimension array, but ...)

So, I want to make the abs calculation and store it to a new [4x2] array. What am I doing wrong?

Thank you

What I have tried:

Dim spalings As Double(,) = {{-1, 2.0}, {-3, 4}, {-5, 6}, {-0.7, 8}}
Dim spalins2 As Double(,) = New Double((spalings.Length - 1), (spalings.Length - 1)) {}
    For x As Integer = 0 To spalings.Length - 1
         For j As Integer = 0 To spalings.Length - 1
              spalins2(x, j) = Math.Abs(spalings(x, j))
         Next j
    Next x
Posted
Updated 17-May-17 8:32am
Comments
[no name] 17-May-17 14:37pm    
"What am I doing wrong?", well the first thing you are doing wrong is still not telling us the information that we would need to help you. That you are getting an array out of bounds exception is kind of really, really, really important information.
The second thing is using the length property.
Dim spalings As Double(,) = {{-1, 2.0}, {-3, 4}, {-5, 6}, {-0.7, 8}}
Dim spalins2 As Double(,) = New Double((spalings.GetUpperBound(0)), (spalings.GetUpperBound(1))) {}
For x As Integer = 0 To spalings.GetUpperBound(0)
For j As Integer = 0 To spalings.GetUpperBound(1)
spalins2(x, j) = Math.Abs(spalings(x, j))
Next j
Next x
You should get a book on VB.NET programming and work through it.
x-ios 17-May-17 14:47pm    
Yes I know that I have to read and work on a book, but its complicated. Because I m working on a project and I need to develop an existed program that it was developed on 99' in TestPoint in a deadline of 2 months. And I have never used VB.NET before. And I have done a really good job right now. But in that stage,it has calculations, lots of calculations between arrays.And everything starts from my 1st post, about the txt file that u answered yesterday. And its very confused. =/
Thank you very much for your time.

1 solution

Um...you do know what Length returns for a multidimension array, yes?
And that it will return the same value each time you call it?
Dim spalins2 As Double(,) = New Double((spalings.Length - 1), (spalings.Length - 1)) {}
And
For x As Integer = 0 To spalings.Length - 1
     For j As Integer = 0 To spalings.Length - 1
So how many elements do you think it's going to investigate?
The answer is: it returns the total number of elements - in your case "8".
So your loop will execute 8 * 8 times, and try to look at 64 elements. So your program will crash.
You need to look at the GetUpperBound method: Array.GetUpperBound Method (Int32) (System)[^]
 
Share this answer
 
Comments
x-ios 17-May-17 14:49pm    
I tried this, because I didn't know how to solve it with the lengths...
And yes the program crashed at 64 times :p

 Dim spalings As Double(,) = {{-1, 2.0}, {-3, 4}, {-5, 6}, {-0.7, 8}}
        Dim spalins2(3, 1) As Double
        For x As Integer = 0 To 3
            For j As Integer = 0 To 1

                spalins2(x, j) = Math.Abs(spalings(x, j))
            Next j
        Next x
OriginalGriff 17-May-17 15:06pm    
So follow the link, and see what it says...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900